Created
August 9, 2018 00:35
-
-
Save pathunstrom/f30ec42d37da26fb2dab7b51bf01f12d to your computer and use it in GitHub Desktop.
API Demo
This file contains hidden or 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 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