Skip to content

This would detail the knowledge I acquire as I study fast api

Notifications You must be signed in to change notification settings

cliffordEmmanuel/exploring-fastapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

exploring-fastapi

This would detail the knowledge I acquire as I work through several POCs in a bid to understand the fastapi web framework.

POC 0: Hello world

Environment Setup

This seems fairly straight forward. Create a virtual environment and install fastapi and uvicorn. The uvicorn is an ASGI server.

Code

The bare bones code is here.

@app.get('/')

This is the route. This tells FastAPI that the succeeding method should be run when the user requests the / path.

async def root():

This is a method declaration. The async def means the method, in this case root, will be run as a Python3 coroutine.

    return {'message':'Hello World!'}

This statement sends data to the browser which is a JSON reponse matching the dictionary above.

The uvicorn server

Syntax is:

uvicorn <script-name>:<fastapi-object> --reload

For eg:
uvicorn hello_world:app --reload

This command starts the uvicorn server which generates a webpage on this url: (http://127.0.0.1:8000). Which generates the following view:

hello world

This is really cool, fast api also generates an interactive API documentation for the api using the /docs path.

interactive docs

Issues

Had a [Errno 98] error while attempting to bind on address ('127.0.0.1', 8000): address already in use error sometime. To fix I needed to kill the service that was using the same url.

lsof -i :8000
To view all the services using the 8000 port, this will show the process id (pid) of the services.

kill -9 pid
To terminate the guilty process

POC 1: CodingNomads location APIs

This tool enables users to submit best remote working locations for the perusal of others. Using this guide.

It achieves 2 things:

  • creates an API for users to submit locations.
  • save the app's data to a database using an ORM.

Models

Model are created using Pydantic. Pydantic is a data validation library that heavily relies on the python3 type hints, to validate incoming data and serialize outgoing data.

Configuring a database

Install sqlalchemy to help interact with a db.

Seems you have to create 2 models to represent a python object:

  • a Pydantic model essentially the "schema" representation. For receiving data from the user as well as sending data to the user.
  • a SQLAlchemy model which is the database representation. Used when fetching and inserting records into the database.

Not yet entirely sure why the decoupling is needed.

For methods that interact with the Database objects such as: insert, get etc,, the first parameter should be the SQL Alchemy session.

Final result

The final output contains, 3 endpoints:

  • a get all place objects from the db
  • a get single place object by id
  • a post new object to the db

As shown here: final api

Sources

About

This would detail the knowledge I acquire as I study fast api

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages