-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
58 lines (43 loc) · 1.8 KB
/
api.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
import sys
sys.path.insert(0, 'lib')
import webapp2
import json
import os
from creds import ELASTIC_ENDPOINT
from google.appengine.api import urlfetch
def _search(q, limit, index='ois', doc_type='incident'):
"""Construct the search query from the supplied, full-text query `q`
search term and the limit for the results.
TODO: add ability to filter by specified fields.
"""
url = os.path.join(ELASTIC_ENDPOINT, index, doc_type, '_search')
raw = urlfetch.fetch(url + '?q=%s&size=%s' % (q, limit))
es_res = json.loads(raw.content)
res = [x['_source'] for x in es_res['hits']['hits']]
return dict(results=res, count=len(res))
class OISSearchHandler(webapp2.RequestHandler):
def get(self):
self.response.headers.add_header("Access-Control-Allow-Origin", "*")
"""Writes search results to endpoint"""
query = self.request.get('query', '*')
limit = self.request.get('limit', 10)
self.response.headers['Content-Type'] = 'application/json'
self.response.write(
json.dumps(_search(query, limit))
)
class OISCountHandler(webapp2.RequestHandler):
def get(self, index='ois', doc_type='incident'):
self.response.headers.add_header("Access-Control-Allow-Origin", "*")
"""Writes search results to endpoint"""
url = os.path.join(ELASTIC_ENDPOINT, index, doc_type, '_count')
res = urlfetch.fetch(url)
count = json.loads(res.content)['count']
self.response.headers['Content-Type'] = 'application/json'
self.response.write(
# TODO: add support for specific categories
json.dumps(dict(category='all', count=count))
)
handlers = webapp2.WSGIApplication([
('/content', OISSearchHandler),
('/_count', OISCountHandler)
], debug=True)