1 |
46770
|
sandro.lab
|
#!/usr/bin/python
|
2 |
|
|
|
3 |
|
|
from flask import Flask, jsonify, request
|
4 |
|
|
from flask.json import JSONEncoder
|
5 |
|
|
from eu.dnetlib.es_connector import DLIESConnector
|
6 |
|
|
from flask_prometheus import monitor
|
7 |
|
|
|
8 |
|
|
from logger import dlilogger
|
9 |
|
|
|
10 |
49818
|
sandro.lab
|
DLI_INDEX = None
|
11 |
|
|
ES_HOST = None
|
12 |
46770
|
sandro.lab
|
|
13 |
|
|
|
14 |
49818
|
sandro.lab
|
def read_properies():
|
15 |
|
|
f = open('api.properties')
|
16 |
|
|
for line in f:
|
17 |
|
|
s = line.strip().split('=')
|
18 |
|
|
if s[0] == 'api.index':
|
19 |
|
|
DLI_INDEX = s[1]
|
20 |
|
|
if s[1] == 'es_index':
|
21 |
|
|
ES_HOST = [x.strip() for x in s[1].split(',')]
|
22 |
46770
|
sandro.lab
|
|
23 |
49818
|
sandro.lab
|
|
24 |
46770
|
sandro.lab
|
class MyJSONEncoder(JSONEncoder):
|
25 |
|
|
def default(self, obj):
|
26 |
|
|
return obj.__dict__
|
27 |
|
|
|
28 |
|
|
|
29 |
|
|
app = Flask(__name__)
|
30 |
|
|
app.debug = False
|
31 |
|
|
app.json_encoder = MyJSONEncoder
|
32 |
|
|
|
33 |
49818
|
sandro.lab
|
base_dnet_url = "http://aggregator-dli.openaire.eu/dli/"
|
34 |
46770
|
sandro.lab
|
|
35 |
50035
|
sandro.lab
|
pid_resolver = {
|
36 |
|
|
"pdb": "http://www.rcsb.org/pdb/explore/explore.do?structureId=%s",
|
37 |
|
|
"ncbi-n": "http://www.ncbi.nlm.nih.gov/gquery/?term=%s",
|
38 |
|
|
"pmid": "http://www.ncbi.nlm.nih.gov/pubmed/%s",
|
39 |
|
|
"pmcid": "http://www.ncbi.nlm.nih.gov/pmc/articles/%s",
|
40 |
|
|
"pubmedid": "http://www.ncbi.nlm.nih.gov/pubmed/%s",
|
41 |
|
|
"doi": "http://dx.doi.org/%s",
|
42 |
|
|
"genbank": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
43 |
|
|
"nuccore": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
44 |
|
|
"swiss-prot": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
45 |
|
|
"arrayexpress": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
46 |
|
|
"biomodels": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
47 |
|
|
"bmrb": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
48 |
|
|
"ena": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
49 |
|
|
"geo": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
50 |
|
|
"ensembl": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
51 |
|
|
"mgi": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
52 |
|
|
"bind": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
53 |
|
|
"pride": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
54 |
|
|
"ddbj": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
55 |
|
|
"bioproject": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
56 |
|
|
"embl": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
57 |
|
|
"sra": "http://www.ncbi.nlm.nih.gov/nucest/%s?report=genbank",
|
58 |
|
|
}
|
59 |
46770
|
sandro.lab
|
|
60 |
50035
|
sandro.lab
|
|
61 |
46770
|
sandro.lab
|
class InvalidUsage(Exception):
|
62 |
|
|
status_code = 400
|
63 |
|
|
|
64 |
|
|
def __init__(self, message, status_code=None, payload=None):
|
65 |
|
|
Exception.__init__(self)
|
66 |
|
|
self.message = message
|
67 |
|
|
if status_code is not None:
|
68 |
|
|
self.status_code = status_code
|
69 |
|
|
self.payload = payload
|
70 |
|
|
|
71 |
|
|
def to_dict(self):
|
72 |
|
|
rv = dict(self.payload or ())
|
73 |
|
|
rv['message'] = self.message
|
74 |
|
|
return rv
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
# Routes
|
78 |
|
|
@app.route('/')
|
79 |
|
|
def root():
|
80 |
|
|
return app.send_static_file('index.html')
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
@app.route('/<path:path>')
|
84 |
|
|
def static_proxy(path):
|
85 |
|
|
# send_static_file will guess the correct MIME type
|
86 |
|
|
return app.send_static_file(path)
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
@app.route('/api/item/<path:identifier>', methods=['post', 'get'])
|
90 |
|
|
def get_item(identifier):
|
91 |
|
|
if identifier:
|
92 |
|
|
object_type = request.form.get('type')
|
93 |
|
|
start = request.form.get('from')
|
94 |
51572
|
sandro.lab
|
if object_type and len(object_type) == 0:
|
95 |
46770
|
sandro.lab
|
object_type = None
|
96 |
51573
|
sandro.lab
|
if start and len(start) == 0:
|
97 |
46770
|
sandro.lab
|
start = None
|
98 |
51574
|
sandro.lab
|
elif start:
|
99 |
46770
|
sandro.lab
|
start = int(start)
|
100 |
49829
|
sandro.lab
|
connector = DLIESConnector()
|
101 |
46770
|
sandro.lab
|
return jsonify(result=connector.item_by_id(identifier, object_type, start))
|
102 |
|
|
else:
|
103 |
|
|
raise InvalidUsage('This view is gone', status_code=410)
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
@app.route('/api/stats/', methods=['post', 'get'])
|
107 |
|
|
def stats():
|
108 |
|
|
raise Exception("Method to be implemented")
|
109 |
|
|
# q = QueryResolver(host, base_dnet_url)
|
110 |
|
|
# return jsonify(stats=q.get_stats())
|
111 |
|
|
|
112 |
|
|
|
113 |
|
|
@app.route('/api/ds_info/', methods=['post', 'get'])
|
114 |
|
|
def info():
|
115 |
|
|
raise Exception("Method to be implemented")
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
@app.route('/api/stats_detail/', methods=['post', 'get'])
|
119 |
|
|
def stats_detail():
|
120 |
|
|
return ""
|
121 |
|
|
|
122 |
|
|
|
123 |
50035
|
sandro.lab
|
@app.route('/api/resolveId', methods=['get'])
|
124 |
|
|
def resolveIdentifier():
|
125 |
|
|
pid = request.form.get('pid')
|
126 |
|
|
pid_type = request.form.get('pid_type')
|
127 |
|
|
if pid_type!= None:
|
128 |
|
|
if pid_type.lower() in pid_resolver:
|
129 |
|
|
return pid_resolver[pid_type.lower()] % pid
|
130 |
51702
|
sandro.lab
|
elif pid_type.lower().strip() == 'url':
|
131 |
|
|
return pid
|
132 |
50035
|
sandro.lab
|
else:
|
133 |
|
|
return "http://identifiers.org/%s:%s" % (pid_type, pid)
|
134 |
|
|
return ""
|
135 |
|
|
|
136 |
|
|
|
137 |
|
|
|
138 |
50064
|
sandro.lab
|
@app.route('/api/queryPid/', methods=['post'])
|
139 |
|
|
def query_pid():
|
140 |
|
|
if 'pid' in request.form:
|
141 |
|
|
print "pid Request for %s "%request.form['pid']
|
142 |
|
|
connector = DLIESConnector()
|
143 |
|
|
result = connector.query_by_id(request.form['pid'])
|
144 |
|
|
return jsonify(result=result)
|
145 |
|
|
|
146 |
|
|
|
147 |
46770
|
sandro.lab
|
@app.route('/api/post/', methods=['post', 'get'])
|
148 |
|
|
def query_post():
|
149 |
|
|
action = None
|
150 |
|
|
query = None
|
151 |
|
|
filter_key = None
|
152 |
|
|
result = {}
|
153 |
|
|
|
154 |
|
|
start = 0
|
155 |
|
|
if 'action' in request.form:
|
156 |
|
|
action = request.form['action']
|
157 |
|
|
if 'query' in request.form:
|
158 |
|
|
query = request.form['query']
|
159 |
|
|
if 'start' in request.form:
|
160 |
50035
|
sandro.lab
|
start = 0
|
161 |
|
|
try:
|
162 |
|
|
start = int(request.form.get('start',0))
|
163 |
|
|
except:
|
164 |
|
|
pass
|
165 |
46770
|
sandro.lab
|
if 'filter' in request.form:
|
166 |
|
|
filter_key = request.form['filter']
|
167 |
|
|
|
168 |
|
|
if action == 'query' and query is not None:
|
169 |
49818
|
sandro.lab
|
connector = DLIESConnector()
|
170 |
46770
|
sandro.lab
|
try:
|
171 |
|
|
result = connector.simple_query(query, start=start, user_filter=filter_key)
|
172 |
|
|
except Exception as e:
|
173 |
|
|
print e
|
174 |
|
|
return jsonify(result=result)
|
175 |
|
|
else:
|
176 |
|
|
return jsonify(error={'error_code': 'an error occur during query on server'})
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
if __name__ == '__main__':
|
180 |
49818
|
sandro.lab
|
|
181 |
46770
|
sandro.lab
|
app.logger.addHandler(dlilogger)
|
182 |
|
|
monitor(app, port=8000)
|
183 |
|
|
app.run()
|
184 |
50035
|
sandro.lab
|
# app.run(host="0.0.0.0")
|