summaryrefslogtreecommitdiff
path: root/effiapi
blob: 3e83fc0c3135441d7992f4b1f0e49bf597ae358e (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/usr/bin/python3
# Copyright (C) 2018  Lars Wirzenius
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import copy
import json
import logging
import os
import sys
import uuid

import bottle
import requests


class HTTPAPI:

    def POST(self, url, headers, body):
        raise NotImplementedError()

    def PUT(self, url, headers, body):
        raise NotImplementedError()

    def GET(self, url, headers=None, body=None):
        raise NotImplementedError()

    def DELETE(self, url, headers):
        raise NotImplementedError()


class RealHTTPAPI(HTTPAPI):

    def POST(self, url, headers, body):
        assert 0
        return requests.post(url, headers=headers, data=body)

    def PUT(self, url, headers, body):
        return requests.put(url, headers=headers, data=body)

    def GET(self, url, headers=None, body=None):
        return requests.get(url, headers=headers, data=body)

    def DELETE(self, url, headers):
        return requests.delete(url, headers=headers, data=body)


class FakeResponse:

    def __init__(self, status_code, headers, body):
        self.status_code = status_code
        self.headers = dict(headers)
        self.text = body
        logging.debug(
            'FakeREsponse: status=%d headers=%r body=%r',
            status_code, headers, body)

    @property
    def ok(self):
        return self.status_code in (200, 201)

    def json(self):
        return json.loads(self.text.strip())


class FakeHTTPAPI(HTTPAPI):

    def __init__(self):
        self._memb = {}

    def POST(self, url, headers, body):
        logging.debug(
            'FakeHTTPAPI.POST url=%r headers=%r body=%r',
            url, headers, body)

        rid = str(uuid.uuid4())
        self._memb[rid] = copy.deepcopy(body)
        headers = {
            'Muck-Id': rid,
        }
        return FakeResponse(201, headers, copy.deepcopy(body))

    def PUT(self, url, headers, body):
        logging.debug(
            'FakeHTTPAPI.PUT url=%r headers=%r body=%r',
            url, headers, body)

        rid = headers.get('Muck-Id')
        if not rid:
            return FakeResponse(400, {}, 'No Muck-Id in request')

        self._memb[rid] = copy.deepcopy(body)
        headers = {
            'Muck-Id': rid,
        }
        return FakeResponse(200, headers, copy.deepcopy(body))

    def GET(self, url, headers=None, body=None):
        logging.info(
            'FakeHTTPAPI.GET url=%r headers=%r body=%r',
            url, headers, body)

        if url.endswith('/status'):
            return self._get_status()

        if url.endswith('/search'):
            return self._get_search(body)

        if url.endswith('/res'):
            return self._get_resource(headers)

        logging.error('Cannot serve url')
        assert 0

    def _get_status(self):
        body = {
            'resources': len(self._memb),
        }
        return FakeResponse(200, {}, json.dumps(body))

    def _get_search(self, body):
        cond = json.loads(body)
        logging.debug('_get_search: cond: %r', cond)
        matches = [
            rid
            for rid in self._memb
            if self._matches_cond(rid, cond)
        ]

        headers = {
            'Content-Type': 'application/json',
        }
        result = {
            'resources': matches,
        }
        return FakeResponse(200, headers, json.dumps(result))

    def _matches_cond(self, rid, cond):
        assert cond == {
            'cond': [
                {
                    'where': 'meta',
                    'op': '>=',
                    'field': 'id',
                    'pattern': '',
                }
            ]
        }
        return True

    def _get_resource(self, headers):
        if headers is None:
            logging.warning('FakeHTTPAPI.GET: no resource id in headers')
            return FakeResponse(400, {}, 'Missing headers')

        rid = headers.get('Muck-Id')
        if not rid:
            logging.warning('FakeHTTPAPI.GET: empty resource id in headers')
            return FakeResponse(404, {}, 'No such member')

        if rid not in self._memb:
            logging.debug('Members: %r', self._memb)
            return FakeResponse(404, {}, 'Retrieving member that does not exist')

        memb = self._memb[rid]
        headers = {
            'Muck-Id': rid,
            'Content-Type': 'application/json',
        }
        return FakeResponse(200, headers, memb)

    def DELETE(self, url, headers):
        logging.debug('FakeHTTPAPI.DELETE url=%r headers=%r', url, headers)

        rid = headers.get('Muck-Id')
        if not rid:
            logging.warning('FakeHTTPAPI.DELETE: empty resource id in headers')
            return FakeResponse(404, {}, 'No such member')

        if rid not in self._memb:
            logging.debug('Members: %r', self._memb)
            return FakeResponse(404, {}, 'Deleting member that does not exist')

        del self._memb[rid]
        return FakeResponse(200, {}, {})


class MuckError(Exception):

    pass


class MuckAPI:

    _copy_headers = [
        'Authorization',
        'Muck-Id',
        'Muck-Revision',
        'Muck-Owner',
    ]

    def __init__(self, url, httpapi):
        self._url = url
        self._httpapi = httpapi

    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
                logging.debug('Copy header from request: %s: %s', name, value)
        return h

    def url(self, path):
        return '{}{}'.format(self._url, path)

    def status(self):
        url = self.url('/status')
        r = self._httpapi.GET(url)
        if r.ok:
            return r.json()
        return {'resources': 0}

    def show(self, rid):
        url = self.url('/res')
        headers = self._get_headers()
        headers['Muck-Id'] = rid
        logging.info('show copied headers')
        r = self._httpapi.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 _write(self, member, func):
        url = self.url('/res')
        headers = self._get_headers()
        headers['Content-Type'] = 'application/json'
        r = func(url, headers, json.dumps(member))
        logging.info('Write result: %s %s', r.status_code, r.text)
        if not r.ok:
            raise MuckError('{} {}'.format(r.status_code, r.text))
        rid = r.headers.get('Muck-Id')
        if not rid:
            raise MuckError('Muck did not return Muck-Id')
        return rid, r.json()

    def create(self, member):
        return self._write(member, self._httpapi.POST)

    def update(self, member):
        return self._write(member, self._httpapi.PUT)

    def delete(self):
        url = self.url('/res')
        headers = self._get_headers()
        r = self._httpapi.DELETE(url, headers)
        logging.info('Delete result: %s %s', r.status_code, r.text)
        if not r.ok:
            raise MuckError('{} {}'.format(r.status_code, r.text))

    def search(self, cond):
        url = self.url('/search')
        headers = self._get_headers()
        headers['Content-Type'] = 'application/json'
        r = self._httpapi.GET(url, headers=headers, body=json.dumps(cond))
        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()


class API:

    def __init__(self, httpapi, bottleapp, config):
        self._add_routes(bottleapp)
        self._muck = MuckAPI(config['muck-url'], httpapi)

    def _add_routes(self, bottleapp):
        routes = [
            {
                'method': 'GET',
                'path': '/status',
                'callback': self._call(self._show_status),
            },
            {
                'method': 'GET',
                'path': '/memb',
                'callback': self._call(self._show_member),
            },
            {
                'method': 'GET',
                'path': '/search',
                'callback': self._call(self._search),
            },
            {
                'method': 'POST',
                'path': '/memb',
                'callback': self._call(self._create),
            },
            {
                'method': 'PUT',
                'path': '/memb',
                'callback': self._call(self._update),
            },
            {
                'method': 'DELETE',
                'path': '/memb',
                'callback': self._call(self._delete),
            },
        ]

        for route in routes:
            logging.debug('Adding route', repr(route))
            bottleapp.route(**route)

    def _call(self, callback):
        def helper():
            logging.debug('_call called')
            try:
                r = bottle.request
                logging.info('Request: method=%s', r.method)
                logging.info('Request: path=%s', r.path)
                logging.info('Request: content type: %s', r.content_type)
                for h in r.headers:
                    logging.info('Request: headers: %s: %s', h, r.get_header(h))
                logging.info('Request: body: %r', r.body.read())
                ret = callback()
            except BaseException as e:
                logging.error(str(e), exc_info=True)
                raise
            else:
                return ret
        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 = self._muck.status()
        return response(200, None, status)

    def _show_member(self):
        rid = bottle.request.get_header('Muck-Id')
        if rid is None:
            return response(400, None, 'no muck-id')
        try:
            logging.debug('API _show_member rid=%r', rid)
            res = self._muck.show(rid)
            return response(200, rid, res)
        except MuckError as e:
            return response(404, None, str(e))

    def _search(self):
        logging.debug('_search callback called')
        cond = bottle.request.json
        logging.debug('_search callback: %r', cond)
        result = self._muck.search(cond)
        return response(200, None, result)

    def _create(self):
        r = bottle.request
        if r.content_type != 'application/json':
            return response(400, None, 'wrong content type')

        obj = bottle.request.json
        logging.info('CREATE %r', repr(obj))
        rid, newobj = self._muck.create(obj)
        return response(201, rid, newobj)

    def _update(self):
        r = bottle.request
        if r.content_type != 'application/json':
            return response(400, None, 'wrong content type')

        obj = bottle.request.json
        logging.info('UPDATE %r', repr(obj))
        rid, newobj = self._muck.update(obj)
        return response(200, rid, newobj)

    def _delete(self):
        r = bottle.request
        self._muck.delete()
        return response(200, None, None)


def response(status, rid, body):
    headers = {
        'Content-Type': 'application/json',
    }
    if rid is not None:
        headers['Muck-Id'] = rid
    return bottle.HTTPResponse(
        status=status, body=json.dumps(body), headers=headers)


with open(sys.argv[1]) as f:
    config = json.load(f)

logging.basicConfig(
    filename=config['log'], level=logging.DEBUG,
    format='%(asctime)s %(levelname)s %(message)s')

logging.info('Effi API starts')

if config.get('pid'):
    pid = os.getpid()
    with open(config['pid'], 'w') as f:
        f.write(str(pid))

if config.get('fake'):
    logging.info("Using FakeHTTPAPI")
    httpapi = FakeHTTPAPI()
else:
    logging.info("Using RealHTTPAPI")
    httpapi = RealHTTPAPI()

logging.debug('Creating API')
app = bottle.default_app()
api = API(httpapi, app, config)

logging.info('Starting application')
app.run(host='127.0.0.1', port=8080)
logglng.critical('Application ended')