-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathviews.py
95 lines (74 loc) · 2.63 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import json
import os
import sys
from flask import abort, jsonify, make_response, render_template, request
from app import app
from software import Software
from steps.core import step_configs
def json_dumper(obj):
"""
if the obj has a to_dict() function we've implemented, uses it to get dict.
from http://stackoverflow.com/a/28174796
"""
try:
return obj.to_dict()
except AttributeError:
return obj.__dict__
def json_resp(thing):
json_str = json.dumps(thing, sort_keys=True, default=json_dumper, indent=4)
if request.path.endswith(".json") and (os.getenv("FLASK_DEBUG", False) == "True"):
print("rendering output through debug_api.html template")
resp = make_response(render_template("debug_api.html", data=json_str))
resp.mimetype = "text/html"
else:
resp = make_response(json_str, 200)
resp.mimetype = "application/json"
return resp
def abort_json(status_code, msg):
body_dict = {"HTTP_status_code": status_code, "message": msg, "error": True}
resp_string = json.dumps(body_dict, sort_keys=True, indent=4)
resp = make_response(resp_string, status_code)
resp.mimetype = "application/json"
abort(resp)
@app.after_request
def after_request_stuff(resp):
# support CORS
resp.headers["Access-Control-Allow-Origin"] = "*"
resp.headers[
"Access-Control-Allow-Methods"
] = "POST, GET, OPTIONS, PUT, DELETE, PATCH"
resp.headers[
"Access-Control-Allow-Headers"
] = "origin, content-type, accept, x-requested-with"
# without this jason's heroku local buffers forever
sys.stdout.flush()
return resp
# ENDPOINTS
#
######################################################################################
@app.route("/", methods=["GET"])
def index_endpoint():
return jsonify(
{
"version": "0.1",
"documentation_url": "https://citeas.org/api",
"msg": "Don't panic",
}
)
@app.route("/product/<path:id>", methods=["GET"])
def citeas_product_get(id):
if id.endswith(".pdf"):
return jsonify({"error_message": "PDF documents are not supported."})
elif id.endswith((".doc", "docx")):
return jsonify({"error_message": "Word documents are not supported."})
else:
my_software = Software(id)
my_software.find_metadata()
return jsonify(my_software.to_dict())
@app.route("/steps", methods=["GET"])
@app.route("/steps/", methods=["GET"])
def citeas_step_configs():
return jsonify(step_configs())
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=port, threaded=True)