Project

General

Profile

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
25
26
27 46770 sandro.lab
class MyJSONEncoder(JSONEncoder):
28
    def default(self, obj):
29
        return obj.__dict__
30
31
32
app = Flask(__name__)
33
app.debug = False
34
app.json_encoder = MyJSONEncoder
35
36 49818 sandro.lab
base_dnet_url = "http://aggregator-dli.openaire.eu/dli/"
37 46770 sandro.lab
38
39
class InvalidUsage(Exception):
40
    status_code = 400
41
42
    def __init__(self, message, status_code=None, payload=None):
43
        Exception.__init__(self)
44
        self.message = message
45
        if status_code is not None:
46
            self.status_code = status_code
47
        self.payload = payload
48
49
    def to_dict(self):
50
        rv = dict(self.payload or ())
51
        rv['message'] = self.message
52
        return rv
53
54
55
# Routes
56
@app.route('/')
57
def root():
58
    return app.send_static_file('index.html')
59
60
61
@app.route('/<path:path>')
62
def static_proxy(path):
63
    # send_static_file will guess the correct MIME type
64
    return app.send_static_file(path)
65
66
67
@app.route('/api/item/<path:identifier>', methods=['post', 'get'])
68
def get_item(identifier):
69
    if identifier:
70
        object_type = request.form.get('type')
71
        start = request.form.get('from')
72
        if len(object_type) == 0:
73
            object_type = None
74
        if len(start) == 0:
75
            start = None
76
        else:
77
            start = int(start)
78
        connector = DLIESConnector(ES_HOST, DLI_INDEX)
79
        return jsonify(result=connector.item_by_id(identifier, object_type, start))
80
    else:
81
        raise InvalidUsage('This view is gone', status_code=410)
82
83
84
@app.route('/api/stats/', methods=['post', 'get'])
85
def stats():
86
    raise Exception("Method to be implemented")
87
    # q = QueryResolver(host, base_dnet_url)
88
    # return jsonify(stats=q.get_stats())
89
90
91
@app.route('/api/ds_info/', methods=['post', 'get'])
92
def info():
93
    raise Exception("Method to be implemented")
94
95
96 49818 sandro.lab
97 46770 sandro.lab
@app.route('/api/stats_detail/', methods=['post', 'get'])
98
def stats_detail():
99
    return ""
100
101
102
@app.route('/api/post/', methods=['post', 'get'])
103
def query_post():
104
    action = None
105
    query = None
106
    filter_key = None
107
    result = {}
108
109
    start = 0
110
    if 'action' in request.form:
111
        action = request.form['action']
112
    if 'query' in request.form:
113
        query = request.form['query']
114
    if 'start' in request.form:
115
        start = int(request.form['start'])
116
    if 'filter' in request.form:
117
        filter_key = request.form['filter']
118
119
    if action == 'query' and query is not None:
120 49818 sandro.lab
        connector = DLIESConnector()
121 46770 sandro.lab
        try:
122
            result = connector.simple_query(query, start=start, user_filter=filter_key)
123
        except Exception as e:
124
            print e
125
        return jsonify(result=result)
126
    else:
127
        return jsonify(error={'error_code': 'an error occur during query on server'})
128
129
130
if __name__ == '__main__':
131 49818 sandro.lab
132 46770 sandro.lab
    app.logger.addHandler(dlilogger)
133
    monitor(app, port=8000)
134
    app.run()