30
loading...
This website collects cookies to deliver better user experience
from flask import Flask, request
app = Flask(__name__)
@app.route('/basic_api/entities/<int:entity_id>', methods=['GET', 'PUT', 'DELETE'])
def entity(entity_id):
if request.method == "GET":
return {
'id': entity_id,
'message': 'This endpoint should return the entity {} details'.format(entity_id),
'method': request.method
}
if request.method == "PUT":
return {
'id': entity_id,
'message': 'This endpoint should update the entity {}'.format(entity_id),
'method': request.method,
'body': request.json
}
if request.method == "DELETE":
return {
'id': entity_id,
'message': 'This endpoint should delete the entity {}'.format(entity_id),
'method': request.method
}
from flask import Flask, request
app = Flask(__name__)
from typing import Optional
from fastapi import FastAPI
app = FastAPI()
/basic_api/entities/<int:entity_id>
endpoint in FastAPI.@app.route('/basic_api/entities/<int:entity_id>', methods=['GET', 'PUT', 'DELETE'])
def entity(entity_id):
if request.method == "GET":
return {
'id': entity_id,
'message': 'This endpoint should return the entity {} details'.format(entity_id),
'method': request.method
}
if request.method == "PUT":
return {
'id': entity_id,
'message': 'This endpoint should update the entity {}'.format(entity_id),
'method': request.method,
'body': request.json
}
if request.method == "DELETE":
return {
'id': entity_id,
'message': 'This endpoint should delete the entity {}'.format(entity_id),
'method': request.method
}
@app.get, @app.put, @app.post
, etc rather than as a parameter. Also note that instead of stating the type of the url parameter entity_id
, within the route, it’s instead typed as a parameter in entity()
@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
}
@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),
}
body
, an Entity
object. To define this object, we create a new class that inherits from BaseModel
. from pydantic import BaseModel
class Entity(BaseModel):
name: str
description: Optional[str] = None
from pydantic import BaseModel
from typing import Optional
from fastapi import FastAPI
app = FastAPI()
class Entity(BaseModel):
name: str
description: Optional[str] = None
@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
}
@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),
}
main.py
file and run the server withuvicorn main:app --reload
http://localhost:8000
http://localhost:8000/docs