FastAPI - A python framework | Full Course
https://www.youtube.com/watch?v=7t2alSnE2-I
https://github.com/bitfumes/fastapi-course
from fastapi import FastAPI
from typing import Optional
from pydantic import BaseModel
app = FastAPI()
@app.get('/blog')
def index(limit=10, published: bool = True, sort: Optional[str] = None):
# only get 10 published blogs
if published:
return {'data': f'{limit} published blogs from the db'}
else:
return {'data': f'{limit} blogs from the db'}
@app.get('/blog/unpublished')
def unpublished():
return {'data': 'all unpublished blogs'}
@app.get('/blog/{id}')
def show(id: int):
# fetch blog with id = id
return {'data': id}
@app.get('/blog/{id}/comments')
def comments(id, limit=10):
# fetch comments of blog with id = id
return {'data': {'1', '2'}}
class Blog(BaseModel):
title: str
body: str
published: Optional[bool]
@app.post('/blog')
def create_blog(blog: Blog):
return {'data': f"Blog is created with title as {blog.title}"}
'FastAPI' 카테고리의 다른 글
FastAPI, 422 ValidationError 리턴 값 구조 변경 예제 (0) | 2022.02.08 |
---|---|
FastAPI, 워커(worker) 개수 자동 계산하기 (0) | 2022.02.04 |
FastAPI 백그라운드 작업 (이메일 전송, 파일 저장 등 백그라운드 태스크) (0) | 2022.01.27 |
Nginx로 FastAPI 배포하기 (0) | 2022.01.25 |
FastAPI 시작, 중지, 재시작 (2) | 2022.01.25 |