Skip to content

Instantly share code, notes, and snippets.

@pathunstrom
Created August 9, 2018 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pathunstrom/f30ec42d37da26fb2dab7b51bf01f12d to your computer and use it in GitHub Desktop.
Save pathunstrom/f30ec42d37da26fb2dab7b51bf01f12d to your computer and use it in GitHub Desktop.
API Demo
from functools import partial
from flask import Flask
from flask import jsonify
from flask import render_template
from flask import request
app = Flask(__name__)
MESSAGE = 'Hello World!'
@app.route('/', methods=["POST", "GET"])
def hello_world():
if request.method == "GET":
if request.headers.get("accept") == "application/json":
response_function = jsonify
else:
response_function = lambda x: render_template("index.html", **x)
elif request.method == "POST":
global MESSAGE
MESSAGE = request.json["message"]
response_function = jsonify
else:
raise Exception
context = {"message": MESSAGE}
return response_function(context)
@app.route("/clone", methods=["POST", "GET"])
def clone_the_world():
if request.method == "POST":
global MESSAGE
MESSAGE = request.json["message"]
context = {"message": MESSAGE}
if request.headers.get("accept") == "application/json":
response_function = jsonify
else:
response_function = partial(render_template, "index.html")
return response_function(context)
if __name__ == '__main__':
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment