Skip to content

Instantly share code, notes, and snippets.

@yirenlu92
Created July 29, 2021 20:52
Show Gist options
  • Save yirenlu92/552ca3fc78c1a5282e5dbdddfbeaa2b7 to your computer and use it in GitHub Desktop.
Save yirenlu92/552ca3fc78c1a5282e5dbdddfbeaa2b7 to your computer and use it in GitHub Desktop.
Flask demo
from flask import Flask, request
app = Flask(__name__)
@app.route('/basic_api/entities', methods=['GET', 'POST'])
def entities():
if request.method == "GET":
return {
'message': 'This endpoint should return a list of entities',
'method': request.method
}
if request.method == "POST":
return {
'message': 'This endpoint should create an entity',
'method': request.method,
'body': request.json
}
@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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment