In this section, we will explore how to build RESTful APIs using the Flask framework. REST (Representational State Transfer) is an architectural style that uses HTTP requests to access and manipulate data. Flask is a lightweight WSGI web application framework in Python that is easy to use and well-suited for building APIs.

Objectives

By the end of this section, you will be able to:

  • Understand the basics of RESTful APIs.
  • Set up a Flask application for building APIs.
  • Create endpoints for CRUD (Create, Read, Update, Delete) operations.
  • Handle JSON data in Flask.
  • Implement error handling in your API.

Prerequisites

Before starting this section, ensure you have:

  • Basic knowledge of Flask (covered in the previous section).
  • Python installed on your machine.
  • Flask installed (pip install Flask).

  1. Introduction to RESTful APIs

Key Concepts

  • Resource: Any piece of data that can be named and manipulated, such as a user, a blog post, or a product.
  • Endpoint: A specific URL where a resource can be accessed.
  • HTTP Methods: The actions you can perform on a resource (GET, POST, PUT, DELETE).
  • Status Codes: Standardized codes that indicate the result of an HTTP request (e.g., 200 OK, 404 Not Found).

Example

HTTP Method Endpoint Description
GET /api/users Retrieve a list of users
POST /api/users Create a new user
GET /api/users/{id} Retrieve a specific user
PUT /api/users/{id} Update a specific user
DELETE /api/users/{id} Delete a specific user

  1. Setting Up the Flask Application

Step-by-Step Guide

  1. Create a Project Directory:

    mkdir flask_api
    cd flask_api
    
  2. Set Up a Virtual Environment:

    python -m venv venv
    source venv/bin/activate  # On Windows use `venv\Scripts\activate`
    
  3. Install Flask:

    pip install Flask
    
  4. Create the Flask Application:

    # app.py
    from flask import Flask, jsonify, request
    
    app = Flask(__name__)
    
    @app.route('/')
    def home():
        return "Welcome to the Flask REST API!"
    
    if __name__ == '__main__':
        app.run(debug=True)
    
  5. Run the Application:

    python app.py
    

  1. Creating Endpoints for CRUD Operations

Example: User Management API

  1. Define the User Resource:

    users = [
        {'id': 1, 'name': 'John Doe', 'email': '[email protected]'},
        {'id': 2, 'name': 'Jane Doe', 'email': '[email protected]'}
    ]
    
  2. Create Endpoints:

    • GET /api/users: Retrieve all users

      @app.route('/api/users', methods=['GET'])
      def get_users():
          return jsonify(users)
      
    • GET /api/users/{id}: Retrieve a specific user

      @app.route('/api/users/<int:user_id>', methods=['GET'])
      def get_user(user_id):
          user = next((user for user in users if user['id'] == user_id), None)
          if user:
              return jsonify(user)
          return jsonify({'message': 'User not found'}), 404
      
    • POST /api/users: Create a new user

      @app.route('/api/users', methods=['POST'])
      def create_user():
          new_user = request.get_json()
          new_user['id'] = len(users) + 1
          users.append(new_user)
          return jsonify(new_user), 201
      
    • PUT /api/users/{id}: Update a specific user

      @app.route('/api/users/<int:user_id>', methods=['PUT'])
      def update_user(user_id):
          user = next((user for user in users if user['id'] == user_id), None)
          if user:
              data = request.get_json()
              user.update(data)
              return jsonify(user)
          return jsonify({'message': 'User not found'}), 404
      
    • DELETE /api/users/{id}: Delete a specific user

      @app.route('/api/users/<int:user_id>', methods=['DELETE'])
      def delete_user(user_id):
          global users
          users = [user for user in users if user['id'] != user_id]
          return jsonify({'message': 'User deleted'})
      

  1. Handling JSON Data

Example

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route('/api/data', methods=['POST'])
def handle_data():
    data = request.get_json()
    return jsonify(data), 201

if __name__ == '__main__':
    app.run(debug=True)

  1. Implementing Error Handling

Example

@app.errorhandler(404)
def not_found(error):
    return jsonify({'message': 'Resource not found'}), 404

@app.errorhandler(400)
def bad_request(error):
    return jsonify({'message': 'Bad request'}), 400

Practical Exercise

Task

Create a Flask API for managing a list of books. Each book should have an id, title, author, and published_date.

  1. Set up the Flask application.
  2. Create endpoints for CRUD operations:
    • GET /api/books
    • GET /api/books/{id}
    • POST /api/books
    • PUT /api/books/{id}
    • DELETE /api/books/{id}

Solution

from flask import Flask, jsonify, request

app = Flask(__name__)

books = [
    {'id': 1, 'title': '1984', 'author': 'George Orwell', 'published_date': '1949-06-08'},
    {'id': 2, 'title': 'To Kill a Mockingbird', 'author': 'Harper Lee', 'published_date': '1960-07-11'}
]

@app.route('/api/books', methods=['GET'])
def get_books():
    return jsonify(books)

@app.route('/api/books/<int:book_id>', methods=['GET'])
def get_book(book_id):
    book = next((book for book in books if book['id'] == book_id), None)
    if book:
        return jsonify(book)
    return jsonify({'message': 'Book not found'}), 404

@app.route('/api/books', methods=['POST'])
def create_book():
    new_book = request.get_json()
    new_book['id'] = len(books) + 1
    books.append(new_book)
    return jsonify(new_book), 201

@app.route('/api/books/<int:book_id>', methods=['PUT'])
def update_book(book_id):
    book = next((book for book in books if book['id'] == book_id), None)
    if book:
        data = request.get_json()
        book.update(data)
        return jsonify(book)
    return jsonify({'message': 'Book not found'}), 404

@app.route('/api/books/<int:book_id>', methods=['DELETE'])
def delete_book(book_id):
    global books
    books = [book for book in books if book['id'] != book_id]
    return jsonify({'message': 'Book deleted'})

if __name__ == '__main__':
    app.run(debug=True)

Conclusion

In this section, you learned how to build RESTful APIs using Flask. You now know how to:

  • Set up a Flask application for API development.
  • Create endpoints for CRUD operations.
  • Handle JSON data in Flask.
  • Implement error handling in your API.

In the next section, we will explore how to integrate databases with Flask to persist data.

Python Programming Course

Module 1: Introduction to Python

Module 2: Control Structures

Module 3: Functions and Modules

Module 4: Data Structures

Module 5: Object-Oriented Programming

Module 6: File Handling

Module 7: Error Handling and Exceptions

Module 8: Advanced Topics

Module 9: Testing and Debugging

Module 10: Web Development with Python

Module 11: Data Science with Python

Module 12: Final Project

© Copyright 2024. All rights reserved