FastAPI Backend
As a reminder, the FastAPI code will be discovered beneath app-api/api.
Step 1: Create the FastAPI utility, the place we configured the docs, the CORS middleware and the endpoints root API router.
Step 2: Outline the Settings class. The scope of this class is to carry all of the constants and configurations you want throughout your API code, corresponding to:
- generic configurations: the port, log degree or model,
- GCP credentials: bucket identify or path to the JSON service account keys.
You’ll use the Settings object throughout the undertaking utilizing the get_settings() operate.
Additionally, contained in the Config class, we programmed FastAPI to search for a .env file within the present listing and cargo all of the variables prefixed with APP_API_.
As you’ll be able to see within the .env.default file, all of the variables begin with APP_API_.
Step 3: Outline the schemas of the API information utilizing Pydantic. These schemas encode or decode information from JSON to a Python object or vice versa. Additionally, they validate the kind and construction of your JSON object primarily based in your outlined information mannequin.
When defining a Pydantic BaseModel, it’s important so as to add a kind to each variable, which can be used on the validation step.
Step 4: Outline your endpoints, in internet lingo, often called views. Often, a view has entry to some information storage and primarily based on a question, it returns a subset of the info supply to the requester.
Thus, a regular movement for retrieving (aka GET request) information appears like this:
“consumer → request information → endpoint → entry information storage → encode to a Pydantic schema → decode to JSON → reply with requested information”
Let’s examine how we outlined an endpoint to GET all the buyer varieties:
We used « gcsfs.GCSFileSystem » to entry the GCS bucket as a regular filesystem.
We hooked up the endpoint to the api_router.
Utilizing the api_router.get() Python decorator, we hooked up a primary operate to the /consumer_type_values endpoint.
Within the instance above, when calling « https://<some_ip>:8001/api/v1/consumer_type_values » the consumer_type_values() operate can be triggered, and the response of the endpoint can be strictly primarily based on what the operate return.
One other necessary factor is to spotlight that by defining the response_model (aka the schema) within the Python decorator, you do not have to create the Pydantic schema explicitly.
In case you return a dictionary that’s 1:1, respecting the schema construction, FastAPI will robotically create the Pydantic object for you.
That is it. Now we’ll repeat the identical logic to outline the remainder of the endpoints. FastAPI makes every little thing really easy and intuitive for you.
Now, let’s check out the entire views.py file, the place we outlined endpoints for the next:
- /well being → well being test
- /consumer_type_values → GET all doable client varieties
- /area_values → GET all doable space varieties
- /predictions/{space}/{consumer_type} → GET the predictions for a given space and client kind. Word that utilizing the {<some_variable>} syntax, you’ll be able to add parameters to your endpoint — FastAPI docs [2].
- /monitoring/metrics → GET the aggregated monitoring metrics
- /monitoring/values/{space}/{consumer_type} → GET the monitoring values for a given space and client kind
I wish to spotlight once more that the FastAPI backend solely reads the GCS bucket’s predictions. The inference step is completed solely within the batch prediction pipeline.
You may also go to « http://<your-ip>:8001/api/v1/docs« to entry the Swagger docs of the API, the place you’ll be able to simply see and take a look at all of your endpoints:
Thats it! Now you understand how to construct a FastAPI backend. Issues may get extra sophisticated when including a database layer and person periods, however you realized all the primary ideas that may get you began!