Skip to content

Chapter 1

Chapter 1

  • type-hint, Pydantic
  • Asynchronous Processing
  • Python's ASGI (Asynchronous Server Gateway Interface)
    • different from WSGI (Web Server Gateway Interface)
    • An ASGI callable object is in fact a coroutine having three parameters:
      • scope: A dict containing details of a specific connection provided by the server.
      • send: An asynchronous callable, by which event messages can be sent to the client.
      • receive: Another asynchronous callable. The application can receive event messages from the client
    • python doesn't have built-in ASGI module (in contrast to wsgiref module for WSGI).
    • so we need to use a third-party ASGI server, here we use Uvicorn.
  • coroutine:
    • copoperative multitasking comes into play when one coroutine "awaits" another. when the await keyword is encountered in the path of execution of a subroutine, its opeartion is suspended till the other coroutine is completed.
  • REST
  • unlike RPC or SOAP, REST is not a protocol. It is a resource-based architecture rather than action-based as is the case in RPC or SOAP. Everything on the REST server is a resource, may it be a file, an image, or a row in a table of a database. The REST API provides a controlled access to its resources so that the client can retrieve and modify them.
    • uniform interface
    • statelessness
    • client-server
    • cacheability
    • layered system
    • code on demand
  • HTTP Verbs: (CRUD -> create, read, update, delete)
  • POST: indicates that a new resource is intended to be created on the server -> CREATE
    • since invoking two identical POST will result in two different resources containing the same information, it is not considered as idempotent.
    • 200 (OK), 204 (No Content)
  • GET: read resource
    • 200 (OK), 404 (Not Found), 400 (Bad Request)
  • PUT: update an existing resource
    • 200 (OK), 404 (Not Found)
    • v.s. POST: POST request are made on resource collections (http post http://example.com/users) vs PUT requests are made on a single resource (http put http://example.com/users/123)
  • DELETE
    • 200 (OK), 202(Accepted, when queued), 204(No Content)
    • calling DELETE on a reousrce a second time will return a 404 (Not Found) since it was already removed
  • FastAPI dependency:
  • Starlette: Python's ASGI toolkit, a lightweight ASGI-compliant web framework
    • websocket support
    • event handler
  • Uvicorn: a Python ASGI server
    • it has been designed as per the ASGI specifications defined in the asgiref package. It has an asgiref.server class as a baseclass
    • uses uvloop library to replace asyncio for scheduling async.
    • use httptools to handle the HTTP protocol
    • only support HTTP/1.1. for HTTP/2, needs to use Daphne or Hypercorn.
  • Pydantic: validation & type check