-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearning_notes.py
72 lines (53 loc) · 1.49 KB
/
learning_notes.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from typing import Optional
# import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
@app.get('/', include_in_schema=False) # path operation decorator, operation, path
def index(): # path operation function
return "WELCOME TO BLOG API SYSTEM"
# Query Parameters, required and not required
@app.get('/blogs')
def get_blogs_list(limit: int, published: bool = True, sort: Optional[str] = None):
if published:
return {
'data': f'{limit} published blogs from database'
}
return {
'data': f'{limit} blogs from database'
}
# Order matters when we have "blogs/unpublished" and "blogs/{blogs_id}"
@app.get('/blogs/unpublished')
def get_unpublished_blogs_list():
return {
'data': {
'detail': 'Unpublished list of blogs',
}
}
@app.get('/blogs/{id}')
def show_blog_info(id: int):
# fetch blog with id = id
return {
'data': id
}
@app.get('/blogs/{blog_id}/comments')
def show_comments_of_blog(id: int):
return {
'data': {
'blog_id': id,
'comments': ['1', '2'],
}
}
class Blog(BaseModel):
title: str
body: str
published_at: Optional[bool]
@app.post('/blogs/create', response_model=Blog)
def create_new_blog(blog: Blog):
return {
'title': blog.title,
'body': blog.body,
'published_at': blog.published_at,
}
# if __name__ == '__main__':
# uvicorn.run(app, host='127.0.0.1', port=8080)