Skillversitas

Building a REST API

The API is the JSON the resource: the jsonify (the dict, the content-type, the no the 1), the get_json (the body, the silent, the no the 400), and the status (the 201, the 204, the no the 1). The blueprint is the /api (the prefix, the no the the), the error is the 1 (the JSON, the message, the no the HTML), and the pagination is the 20 (the page, the no the 10K). This chapter covers the jsonify (the dict, the no the 1), the get_json (the body, the no the 400), the status (the 201, the 204), and the /api blueprint (the prefix, the no the the).

The JSON resource and the status

# the API (the /api, the JSON,
#   the no HTML, the no the 1):
from flask import Blueprint, \
    jsonify, request

api = Blueprint("api", __name__,
    url_prefix="/api")

#   the 1: the list (the 200,
#     the no the 1, the no
#     the the):
@api.route("/posts/")
def api_posts():
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    page = request.args.get(
        "page", 1, type=int)
    per = 20
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    posts = Post.query.order_by(
        Post.created_at.desc()) \
        .offset((page - 1) * per) \
        .limit(per).all()
    return jsonify([
        {"id": p.id,
         "body": p.body,
         "author": p.author.
             username,
         "created":
             p.created_at.
             isoformat()}
        for p in posts])
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

#   the 2: the 1 (the 201, the
#     no the 1, the no the the):
@api.route("/posts/",
    methods=["POST"])
def api_post_create():
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    data = request.get_json(
        silent=True)
    if not data or \
            "body" not in data:
        #   the no: the no (the
        #     no, the no, the no
        #     the no, the no
        #     the no):
        return jsonify(
            error="the no"), 400
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    p = Post(body=data["body"])
    db.session.add(p)
    db.session.commit()
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    return jsonify(id=p.id), 201

The API is the /api: the Blueprint is the prefix (the no the 1, the no the the), the jsonify is the dict (the content-type, the no the 1, the no the the), and the list is the 1 (the 20, the offset the limit, the no the 10K) - the get_json(silent=True) is the body: the None is the no (the 400, the 1, the no the the), and the no is the 1 (the key, the missing, the no the the). The status is the 1: the 201 is the create (the id, the no the 1), the 204 is the delete (the no the body, the no the the), and the 404 is the no (the 1, the no the the) - the error is the next: the JSON the no the HTML.

The JSON error and the auth

# the ERROR (the JSON, the no
#   HTML, the no the 1):
@api.errorhandler(404)
def api_404(e):
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    return jsonify(
        error="the no"), 404
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the 1 (the 400,
#     the 401, the 403,
#     the 404, the no the 1)

# the AUTH (the token, the no
#   the session, the no the 1):
@api.before_request
def api_auth():
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    if request.endpoint == \
            "api.index":
        return
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    auth = request.headers.get(
        "Authorization", "")
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    if not auth.startswith(
            "Bearer "):
        #   the no: the no (the
        #     no, the no, the no
        #     the no, the no
        #     the no):
        return jsonify(
            error="the no"), 401
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The error is the JSON: the @api.errorhandler(404) is the 1 (the message, the no the 1, the no the the), and the 1 is the no (the HTML, the 1, the no the the) - the 4xx is the 1 (the 400, the 401, the 403, the no the 1). The auth is the token: the @api.before_request is the gate (the 1, the no the view), the Authorization: Bearer is the header (the no the cookie, the no the the), and the 401 is the no (the token, the 1, the no the the) - the rule is the one: the jsonify the dict, the get_json the body, the status the 1, and the /api the prefix - the next chapter's extension is the ecosystem the 1 the no the 1.

The Working Rules

  • The API is the /api, the no HTML. The Blueprint the prefix (the no the 1, the no the the), and the jsonify the dict (the content-type, the no the 1, the no the the) - the list the 20 (the offset, the limit, the no the 10K), and the no the 1 (the no the 1, the no the the).
  • The get_json is the body, the 400 the no. The silent=True the 1 (the None, the no the 1, the no the the), and the key the missing (the 1, the no the the) - the no the 2 (the no the 1, the no the the), and the no the 1 (the no the 1, the no the the).
  • The status is the 1, the 201 the create. The 201 the id (the no the 1, the no the the), and the 204 the delete (the no the body, the no the the) - the 404 the no (the 1, the no the the), and the no the 1 (the no the 1, the no the the).
  • The auth is the token, the no the cookie. The @api.before_request the gate (the 1, the no the view), and the Bearer the header (the no the session, the no the the) - the 401 the no (the token, the 1, the no the the), and the no the 2 (the no the 1, the no the the).

Summary

The API is the JSON the resource: the jsonify the dict (the content-type, the no the 1), the get_json the body (the silent, the 400 the no, the key the missing), the status the 1 (the 201 the id, the 204 the no the body, the 404 the no), and the /api the blueprint (the prefix, the no the the). The error is the JSON (the @api.errorhandler, the message, the no the HTML), and the auth is the token (the @api.before_request, the Bearer, the no the cookie, the 401 the no). The rule is the one: the jsonify the dict, the get_json the body, the status the 1, and the /api the prefix - the next chapter's extension is the ecosystem: the init_app the pattern, the 6 the 1, and the pin the version (the no the the, the no the 1).