simple.py
1.72 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
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
import logging
import json
import uuid
import boto3
LOG = logging.getLogger()
LOG.setLevel(logging.INFO)
# The kappa deploy command will make sure that the right config file
# for this environment is available in the local directory.
config = json.load(open('config.json'))
session = boto3.Session(region_name=config['region_name'])
ddb_client = session.resource('dynamodb')
table = ddb_client.Table(config['sample_table'])
def foobar():
return 42
def _get(event, context):
customer_id = event.get('id')
if customer_id is None:
raise Exception('No id provided for GET operation')
response = table.get_item(Key={'id': customer_id})
item = response.get('Item')
if item is None:
raise Exception('id: {} not found'.format(customer_id))
return response['Item']
def _post(event, context):
item = event['json_body']
if item is None:
raise Exception('No json_body found in event')
item['id'] = str(uuid.uuid4())
table.put_item(Item=item)
return item
def _put(event, context):
data = _get(event, context)
id_ = data.get('id')
data.update(event['json_body'])
# don't allow the id to be changed
data['id'] = id_
table.put_item(Item=data)
return data
def handler(event, context):
LOG.info(event)
http_method = event.get('http_method')
if not http_method:
return 'NoHttpMethodSupplied'
if http_method == 'GET':
return _get(event, context)
elif http_method == 'POST':
return _post(event, context)
elif http_method == 'PUT':
return _put(event, context)
elif http_method == 'DELETE':
return _put(event, context)
else:
raise Exception('UnsupportedMethod: {}'.format(http_method))