-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
52 lines (44 loc) · 1.68 KB
/
main.py
File metadata and controls
52 lines (44 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from fastapi import FastAPI
from typing import Optional
from pydantic import BaseModel
# create an instance of the app using FASTAPI
app=FastAPI()
# give path operation decorator, to create a route
@app.get('/')
def index():
return {'data':{'name':'dev'}}
@app.get('/about')
def about():
return {'data':'about page'}
# query parameters are defined in the function parameters and not given in path
# also to define their type, we can use type hints :int, :str, :bool, etc
# Optional parameters can be defined using Optional from typing module
@app.get('/blog')
def blog_list(limit=10, published:bool=True, sort: Optional[str]=None):
if published:
return {'data':f'{limit} published blogs from the db'}
else:
return {'data':f'{limit} blogs from the db'}
# we need to write this above the dynamic route, else it will be treated as a dynamic route
# since fastapi matches routes from top to bottom, it can match blog/xyz to blog/{id}
@app.get('/blog/unpublished')
def unpublished():
return {'data':'all unpublished blogs'}
# create a dynamic route with path parameter
@app.get('/blog/{id}')
# id: int means id is an integer
def blog(id: int):
return {'data': f'Blog post with id {id}'}
# if a param is accepted and is in path then FASTAPI knows it's a path param
# if a param is not in path then it's a query param
@app.get('/blog/{id}/comments')
def comments(id):
return {'data': f'Comments for blog post with id {id}'}
# to create a blog post with request body
class Blog(BaseModel):
title: str
body: str
published: Optional[bool]
@app.post('/blog')
def create_blog(blog: Blog):
return {'data': f'Blog titled "{blog.title}" created successfully!'}