-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
41 lines (30 loc) · 1.11 KB
/
api.py
File metadata and controls
41 lines (30 loc) · 1.11 KB
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
import json
import falcon
from wsgiref import simple_server
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
from models import Recipient, RecipientStore, InvalidUKMobileNumberException
rs = RecipientStore()
class RecipientResource:
def on_post(self, req, resp):
"""Handles POST requests"""
payload = json.loads(req.stream.read())
logger.debug('Recieved {} '.format(payload))
try:
r = Recipient(payload)
r.validate()
key = rs.add(r)
logger.debug('Added recipient at {}'.format(key))
except InvalidUKMobileNumberException, e:
logger.error(e)
raise falcon.HTTPBadRequest(
'You need to supply a valid UK mobile number.',
'See docs for more details.')
api = falcon.API()
recipient_end_point = RecipientResource()
api.add_route('/recipient', recipient_end_point)
if __name__ == '__main__':
logger.debug('Starting server on http://127.0.0.1:8000...')
httpd = simple_server.make_server('127.0.0.1', 8000, api)
httpd.serve_forever()