Created
July 29, 2021 20:49
-
-
Save yirenlu92/d8fca01734bec2e3b7edfc5268b24be0 to your computer and use it in GitHub Desktop.
Fast API CRUD Demo
This file contains 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
from typing import Optional | |
from fastapi import FastAPI | |
from pydantic import BaseModel | |
class Entity(BaseModel): | |
name: str | |
description: Optional[str] = None | |
app = FastAPI() | |
@app.get('/basic_api/entities') | |
def entities(): | |
return { | |
'message': 'This endpoint should return a list of entities', | |
} | |
@app.post('/basic_api/entities') | |
def entities(): | |
return { | |
'message': 'This endpoint should create an entity', | |
'body': 'some body' | |
} | |
@app.get('/basic_api/entities/{entity_id}') | |
def entity(entity_id: int): | |
return { | |
'id': entity_id, | |
'message': 'This endpoint should return the entity {} details'.format(entity_id), | |
} | |
@app.put('/basic_api/entities/{entity_id}') | |
def entity(entity_id: int, body: Entity): | |
return { | |
'id': entity_id, | |
'message': 'This endpoint should update the entity {}'.format(entity_id), | |
'body name': body.name, | |
'body description': body.description | |
} | |
@app.delete('/basic_api/entities/{entity_id}') | |
def entity(entity_id: int): | |
return { | |
'id': entity_id, | |
'message': 'This endpoint should delete the entity {}'.format(entity_id), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment