221124 - TIL

qnm83·2022년 11월 24일
0

TIL

목록 보기
13/16
  • 백엔드, db에 대해 이해해 보고싶어서 FastApi 간단하게 공부해보았다.
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: {published} blogs from the db'}
  else: 
    return { 'data': f'{limit} published: {published} 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}"}

YouTube-tutorial

0개의 댓글