Skip to content

Instantly share code, notes, and snippets.

@pcote
Created December 20, 2015 23:25
Show Gist options
  • Select an option

  • Save pcote/3c37f6fc4d5b6353d9f1 to your computer and use it in GitHub Desktop.

Select an option

Save pcote/3c37f6fc4d5b6353d9f1 to your computer and use it in GitHub Desktop.
from functools import wraps
def validate_json(func):
@wraps(func)
def wrapper(*args, **kwargs):
json_data = request.get_json()
if "exercise_id" not in json_data or "score" not in json_data:
abort(400)
return func(*args, **kwargs)
return wrapper
@app.route("/addscore", methods=["POST"])
@validate_json
def add_score():
json_data = request.get_json()
exercise_id = json_data.get("exercise_id")
score = json_data.get("score")
model.add_attempt(exercise_id, score)
return jsonify(dict(result="success"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment