Created
November 10, 2020 21:01
-
-
Save jangia/ceee361fe4195832959982511b19a16a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| from fastapi import FastAPI | |
| from pydantic.main import BaseModel | |
| app = FastAPI() | |
| class CreateTodo(BaseModel): | |
| name: str | |
| done: bool | |
| class Todo(BaseModel): | |
| id: int | |
| name: str | |
| done: bool | |
| created_at: datetime.datetime | |
| todos = [ | |
| Todo(id=1, name='Wash the car.', done=True, created_at='2020-08-10T21:38:27.798106'), | |
| Todo(id=2, name='Deploy to production', done=False, created_at='2020-10-07T11:32:27.798134'), | |
| Todo(id=3, name='Kiss your wife', done=False, created_at='2020-10-31T13:01:11.123106'), | |
| ] | |
| @app.post('/todos/') | |
| def create_todo(todo_params: CreateTodo): | |
| todo = Todo( | |
| id=len(todos) + 1, | |
| name=todo_params.name, | |
| done=todo_params.done, | |
| created_at=datetime.datetime.now() | |
| ) | |
| todos.append(todo) | |
| return todo | |
| @app.get('/todos/{todo_id}') | |
| def get_todo_by_id(todo_id: int): | |
| todo = next( | |
| todo | |
| for todo in todos | |
| if todo.id == todo_id | |
| ) | |
| return todo | |
| @app.get('/todos/') | |
| def filter_todos_by_done(done: bool): | |
| matched_todos = [ | |
| todo | |
| for todo in todos | |
| if todo.done == done | |
| ] | |
| return matched_todos | |
| # http://localhost:8000/docs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment