Last active
July 3, 2018 03:55
-
-
Save titipata/0d676481b5ad7ab0bafe8f661c98de23 to your computer and use it in GitHub Desktop.
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
# -*- coding: utf-8 -*- | |
""" | |
A backend service for Penn event calendar | |
Run the app: | |
$ python api.py | |
This will serve on localhost:5000. | |
""" | |
import os | |
import json | |
from datetime import datetime, timedelta | |
import dateutil.parser | |
from fetch_events import read_json, save_json | |
from flask import Flask | |
from flask_restful import Resource, Api | |
from webargs import fields, validate | |
from webargs.flaskparser import use_args, use_kwargs, parser | |
app = Flask(__name__) | |
api = Api(app, catch_all_404s=True) | |
class AddResource(Resource): | |
"""An addition endpoint.""" | |
add_args = { | |
'x': fields.Float(required=True), | |
'y': fields.Float(required=True), | |
} | |
@use_kwargs(add_args) | |
def post(self, x, y): | |
"""An addition endpoint.""" | |
return {'result': x + y} | |
# error handler | |
@parser.error_handler | |
def handle_request_parsing_error(err): | |
""" | |
webargs error handler that uses Flask-RESTful's abort function to return | |
a JSON error response to the client. | |
""" | |
abort(422, errors=err.messages) | |
if __name__ == '__main__': | |
api.add_resource(AddResource, '/add') | |
app.run(port=5000, debug=True) |
kittinan
commented
Jul 3, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment