Skip to content

Instantly share code, notes, and snippets.

@yirenlu92
Created July 29, 2021 20:49
Show Gist options
  • Save yirenlu92/d8fca01734bec2e3b7edfc5268b24be0 to your computer and use it in GitHub Desktop.
Save yirenlu92/d8fca01734bec2e3b7edfc5268b24be0 to your computer and use it in GitHub Desktop.
Fast API CRUD Demo
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