Chapter 18

What is the REST API?

REST, or REpresentational State Transfer, is a general architecture for APIs interaction that uses the HTTP protocol. The main features of REST-compliant systems are being stateless and their separation of concerns between the client and the server.

What Python packages can be used to build a REST API?

At this point, there are quite a lot of frameworks that can be used to build a REST API in Python. The most popular ones are Flask, Django REST, Hug, Falcon, CherryPy, Quart, and many others. In this book, we're using the FastAPI framework.

What are the key features of the FastAPI framework?

FastAPI has a few unique characteristics. First, it is designed specifically with API in mind, which is different to many others. Second, it fully supports asynchronous execution and can work with a Uvicorn-Gunicorn inspired asynchronous server. Third, it makes use of type annotations, using them to generate interactive documentation (OpenAPI) and, most importantly, to validate passed data automatically. Finally, it provides simple tools for the validation and conversion of data—all that's left is to write the business logic.

Why OpenAPI (Swagger)?

OpenAPI, previously known as Swagger, is an API description specification that allows you to define an API with a simple file and then generate web-based interactive documentation from that file. In fact, while the documentation is autogenerated by FastAPI, this specification can also be used to do the following:

  • Autogenerate client libraries in multiple languages, including Python
  • Autogenerate API stubs (API-serving server code templates) from the file, also in multiple languages

Why do we need Uvicorn or Gunicorn servers?

Both Uvicorn and Gunicorn are Web Server Gateway Interface (WSGI) tools. Their job is to deploy multiple instances of an application, restart them if needed, and pass requests to them and responses back to the client. WSGI servers usually run under a web server, such as nginx, which takes care of requests, returns files, and directs only correct requests to the WSGI (which then passes them to the application). Uvicorn is specifically focused on asynchronous execution and has a slightly different implementation of asynchronous work.

What metrics does the Locust package measure?

Locust is a package that's designed to test the traffic loads of a service (for example, an API). Its main metrics are requests per second, average response time, and the number of errors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.80.123