-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
73 lines (51 loc) · 1.54 KB
/
app.py
File metadata and controls
73 lines (51 loc) · 1.54 KB
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
import requests
from flask import Flask, abort, jsonify
from flask_caching import Cache
# Source api
Dcbtube_url = 'https://menu.dckube.scilifelab.se/api/'
# Instantiate the flask app
app = Flask(__name__)
# setup flask config, mainly for cache
config = {"CACHE_TYPE": "SimpleCache",
"CACHE_DEFAULT_TIMEOUT": 10
}
# tell Flask to use the above config
app.config.from_mapping(config)
cache = Cache(app)
def backend(content):
"""function for possible back end
processing currently does nothing
but log the cache-misses
Args:
content (dict):
Returns:
dict: currently same as input
"""
print(f'new call or cache expired! calling external api')
return (content)
@app.errorhandler(404)
def api_error(e):
# return error as json
return jsonify(error=str(e)), 404
@app.errorhandler(501)
def api_error(e):
# return error as json
return jsonify(error=str(e)), 501
# main route
# using 'path' type to allow '/' for subpaths
@app.route("/<path:uri>")
@cache.cached()
def relay(uri):
try:
response = requests.get(Dcbtube_url + uri)
response.raise_for_status()
try:
'application/json' in response.headers.get("Content-Type")
content = backend(response.json())
return content
except:
abort(501, descrption = "Not implmented")
except:
abort(404, description = "Resource not found")
if __name__ == "__main__":
app.run(debug=True, threaded=True)