From 1435cecaa7113f931d36a8caa70ee21a3bca6d6a Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 17 Nov 2018 18:09:58 +0200 Subject: Change: use Muck to search, show reasources --- effiapi | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 118 insertions(+), 32 deletions(-) (limited to 'effiapi') diff --git a/effiapi b/effiapi index bc47a66..ac68677 100755 --- a/effiapi +++ b/effiapi @@ -22,75 +22,161 @@ import sys import uuid import bottle +import requests -class Muck: +class MuckError(Exception): - def __init__(self): - self._objs = {} + pass - def __len__(self): - return len(self._objs) - def new_id(self): - return str(uuid.uuid4()) +class MuckAPI: - def create(self, obj): - obj_id = self.new_id() - self._objs[obj_id] = obj + _copy_headers = [ + 'Authorization', + 'Muck-Id', + 'Muck-Revision', + 'Muck-User', + ] - def update(self, obj_id, obj): - self._objs[obj_id] = obj + def __init__(self, url): + self._url = url - def show(self, obj_id): - return self._objs.get(obj_id) + def _get_headers(self): + h = {} + for name in self._copy_headers: + value = bottle.request.get_header(name) + if value is not None: + h[name] = value + return h - def delete(self, obj_id): - if obj_id in self._objs: - del self._objs[obj_id] + def url(self, path): + return '{}{}'.format(self._url, path) - def search(self): - return copy.deepcopy(self._objs) + def status(self): + url = self.url('/status') + r = requests.get(url) + return r.json() + + def show(self, rid): + url = self.url('/res') + headers = self._get_headers() + for h in headers: + logging.debug('Request header: {}: {}'.format(h, headers[h])) + r = requests.get(url, headers=headers) + logging.info('Show result: %s %s', r.status_code, r.text) + if not r.ok: + raise MuckError('{} {}'.format(r.status_code, r.text)) + return r.json() + + def create(self, member): + url = self.url('/res') + headers = self._get_headers() + headers['Content-Type'] = 'application/json' + for h in headers: + logging.debug('Request header: {}: {}'.format(h, headers[h])) + r = requests.post(url, headers=headers, data=member) + logging.info('Show result: %s %s', r.status_code, r.text) + if not r.ok: + raise MuckError('{} {}'.format(r.status_code, r.text)) + return r.json() + + def search(self, cond): + url = self.url('/search') + headers = self._get_headers() + headers['Content-Type'] = 'application/json' + r = requests.get(url, data=json.dumps(cond), headers=headers) + logging.info('Search result: %s %s', r.status_code, r.text) + if not r.ok: + raise MuckError('{} {}'.format(r.status_code, r.text)) + return r.json() + + + + # def create(self, token, obj): + # r = requests.post(url, data=obj, headers=headers) + # obj_id = self.new_id + # self._objs[obj_id] = obj + + # def update(self, obj_id, obj): + # self._objs[obj_id] = obj + + # def delete(self, obj_id): + # if obj_id in self._objs: + # del self._objs[obj_id] class API: - def __init__(self, bottleapp): + def __init__(self, bottleapp, config): self._add_routes(bottleapp) - self._muck = Muck() + self._muck = MuckAPI(config['muck-url']) def _add_routes(self, bottleapp): routes = [ { 'method': 'GET', 'path': '/status', - 'callback': self._show_status, + 'callback': self._call(self._show_status), + }, + { + 'method': 'GET', + 'path': '/mem', + 'callback': self._call(self._show_member), }, { 'method': 'GET', 'path': '/search', - 'callback': self._search, + 'callback': self._call(self._search), }, { 'method': 'POST', 'path': '/mem', - 'callback': self._create, + 'callback': self._call(self._create), }, ] for route in routes: bottleapp.route(**route) + def _call(self, callback): + def helper(): + r = bottle.request + logging.info('Request: path=%s', r.path) + logging.info('Request: content type: %s', r.content_type) + logging.info('Request: body: %r', r.body.read()) + return callback() + return helper + + def _get_token(self): + r = bottle.request + authz = r.get_header('Authorization') + if not authz: + return None + w = authz.split() + if len(w) != 2: + return None + if w[0].lower() != 'bearer': + return None + return w[1] + def _show_status(self): - status = { - 'resources': len(self._muck), - } + status = self._muck.status() return response(200, status) + def _show_member(self): + rid = bottle.request.get_header('Muck-Id') + if rid is None: + return response(400) + try: + res = self._muck.show(rid) + return response(200, res) + except MuckError as e: + return response(404, str(e)) + def _search(self): - result = { - 'resources': self._muck.search(), - } + cond = bottle.request.json + result = self._muck.search(cond) return response(200, result) def _create(self): @@ -117,7 +203,7 @@ with open(sys.argv[1]) as f: logging.basicConfig( filename=config['log'], level=logging.DEBUG, - format='%(levelname)s %(message)s') + format='%(asctime)s %(levelname)s %(message)s') logging.info('Effi API starts') @@ -127,5 +213,5 @@ if config.get('pid'): f.write(str(pid)) app = bottle.default_app() -api = API(app) +api = API(app, config) app.run(host='127.0.0.1', port=8080) -- cgit v1.2.1