- Install
- Notes
- Build and run
- HTTP Methods
- Testing post requests
- Using Async
- Swagger view
- Pydantic and Data Models
- Flow
- install fastapi Fastapi Website
- install uvicorn uvicorn Website
- Pydantic for Model (datatype) validation pydantic
pip install fastapi "uvicorn[standard"
pip install uvicorn
Fast API is production ready.
FAST API IS JUST A FRAMEWORK
you also need a Uvicorn server which is a ASGI
server (asynchronous service gateway)
Uvicorn is similar to express js
Using data types: name:type
or name:type=value
class userUpdateRequest():
first_name: str
last_name: str = 'banana'
- Simply adds hints for the list
format is:
varName: List[object] = [obj, obj...]
example:
db: List[User] = [
User(
id= UUID("4142061b-3062-4206-9de6-28f9f516db29"),
first_name="Adam",
last_name='mcminge',
gender = Gender.male,
roles = [Role.admin,Role.user]
)
]
- Make a main.py
- create a root
- run below:
uvicorn main:app --reload
- main is module name
- app is the instance of our application
Head Request Resource representation
Head Same as Get but does not return body
Post Submits an entity to a specified resource , often changing state in server
Put Put replaces all representations of target resource with current payload.
Delete Delete a specified resource
Connect Establishes a connection or tunnel to server.
Use thunder client
in vs code or postman
Switch to post
add the json body of the user and send
use async lets us use await
@app.get('/')
async def read_results():
results = await some_library()
return results
simply go to localhost:8000/docs
- Automatically groups your API endpoints
- Assuming swagger looks at the functions under the defined endpoints
Non-interactive documentation
You can download it.
- Use typing, pydantic (basemodel) and others in the models.py
Examples:
Note the name:dataType
format.
class User(BaseModel):
# gives a uuid if not provided
id: Optional[UUID] = uuid4()
first_name: str
last_name: str
middle_name: Optional[str]
gender: Gender
roles: List[Role]