INFO 153B/253B: Backend Web Architecture
Kay Ashaolu
from flask import Flask
import json
app = Flask(__name__,static_url_path="/static")
from flask import Flask, request
import json
app = Flask(__name__)
quote_db = {
'sunday': "Life is about making an impact, not making an income. \
–Kevin Kruse",
'monday': "Whatever the mind of man can conceive and believe, it can achieve. \
–Napoleon Hill",
'tuesday': "Strive not to be a success, but rather to be of value. \
–Albert Einstein",
'wednesday': "You miss 100% of the shots you don’t take. \
–Wayne Gretzky",
'thursday': "Every strike brings me closer to the next home run. \
–Babe Ruth",
'friday': "We become what we think about. \
–Earl Nightingale",
'saturday': "Life is what happens to you while you’re busy making other plans. \
–John Lennon",
}
def gen_response(day_of_week):
if not day_of_week:
response = {"message": "We need the day_of_week in order to send a quote"}
response_code = 400
elif day_of_week.lower() not in quote_db:
response = {"message": "Sorry we don't know that day of the week" }
response_code = 404
else:
response = {"day": day_of_week, "quote": quote_db[day_of_week.lower()]}
response_code = 200
return json.dumps(response), response_code
@app.route('/quote/<day_of_week>')
def quote_of_the_day(day_of_week):
return gen_response(day_of_week)
@app.route('/quote', methods = ['GET'])
@app.route('/quote/', methods = ['GET'])
def quote_of_the_day_get():
day_of_week = request.args.get("day_of_week")
return gen_response(day_of_week)
@app.route('/quote', methods = ['POST'])
def quote_of_the_day_post():
data = request.get_json()
day_of_week = data.get("day_of_week")
return gen_response(day_of_week)
@app.errorhandler(400)
def page_not_found(e):
response = {"message": "We could not understand your request. \
Please fix and try again"}
return json.dumps(response), 404
@app.errorhandler(404)
def page_not_found(e):
response = {"message": "This endpoint is not supported"}
return json.dumps(response), 404