[ad_1]
FastAPI Backend
As a reminder, the FastAPI code can be found below app-api/api.
Step 1: Create a FastAPI application where we have configured documents, CORS middleware and root API router for endpoints.
Step 2: Define the parameter class. The scope of this class is to store all the constants and configurations you need in your API code, such as:
- General configurations: port, log level or version,
- GCP Certificates: The bucket name or path to the JSON service account keys.
You will use the Settings object in the project using get_settings() function.
Also, inside configuration In class, we programmed FastAPI to find a .Envy Enter the current directory and load all variables with the prefix APP_API_.
as you can see .env.default file, all variables are initialized APP_API_.
Step 3: Define API data schemas using Pydantic. These schemes encode or decode data from JSON to Python objects or vice versa. Also, they check the type and structure of your JSON object based on your defined data model.
When defining a Pydantic BaseModel, it is necessary to add a type to all variables that will be used in the validation step.
Step 4: Define your endpoints with a web language known as views. Typically, a view has access to some data store and, upon request, returns a subset of the data source to the requester.
Thus, the standard flow for retrieving data (aka GET request) looks like this:
“Client → Request data → Endpoint → Access data store → Encrypt to pedant schema → Decrypt to JSON → Respond with requested data”
Let’s see how we define an endpoint to get all user types:
we used “gcsfs.GCSFileSystem” To access the GCS bucket as in a standard file system.
We attached the last point api_router.
by using api_router.get() Python decorator, we attached the main function /user_type_values final point.
In the above example, when calling “https://
Another important thing to emphasize is that by definition answer_model (aka schema) in the Python decorator, You don’t need to create a pedantic diagram outright.
If you return a dictionary that is 1:1 given the schema structure, FastAPI will automatically create a pedant object for you.
He is. Now we will repeat the same logic to define the rest of the endpoints. FastAPI makes everything so easy and intuitive for you.
Now let’s look at the whole views.py The file where we defined the endpoints for:
- /health → Health check
- /user_type_values → Get all possible user types
- /area_values → Get all possible terrain types
- /predictions/area/consumer_type → Get forecasts for a given area and user type. Note that
syntax, you can add parameters to your endpoint — FastAPI Docs [2]. - /monitoring/metrics → Get overall monitoring metrics
- /monitoring/values/area/consumer_type → Get monitoring values for a given area and user type
I want to emphasize again that the FastAPI backend only reads GCS bucket predictions. The inference step is done only in the image prediction pipeline.
you can also go”http://
He is! Now you know how to build a FastAPI backend. Things can get more complicated when you add a database layer and user sessions, but you’ve learned all the basic concepts that will help!
[ad_2]
Source link