-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
94 lines (73 loc) · 2.62 KB
/
api.py
File metadata and controls
94 lines (73 loc) · 2.62 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
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
from __future__ import print_function
from flask import Flask, request, abort, Response, jsonify
import json
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False
@app.route("/read_items", methods=['POST'])
def read_items():
from botocore.config import Config
from boto3.dynamodb.conditions import Key
import boto3
import json
import decimal
#event = json.loads(event['body'])
body = request.get_data(as_text=True)
event = json.loads(body)
#declare table instance
dynamodb = boto3.resource('dynamodb', region_name='ap-northeast-1')
TableName = event['table']
table = dynamodb.Table(TableName)
key1 = event['payload']['key1']
if(TableName == 'UserActivity'):
primary_key1 = 'DeviceID_UserID'
primary_key2 = 'OrderTimestamp'
key2range = event['payload']['key2range']
response = table.query(
KeyConditionExpression= Key(primary_key1).eq(key1)\
& Key(primary_key2).between(key2range[0], key2range[1]))
elif(TableName == 'UserInfo'):
primary_key1 = 'DeviceID_UserID'
response = table.query(KeyConditionExpression= Key(primary_key1).eq(key1))
else:
responese = {
'statusCode': 777,
'headers': {'Content-Type': 'application/json'},
'body': "no such table"
}
return jsonify(responese)
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
return int(o)
return super(DecimalEncoder, self).default(o)
response = {
'statusCode': 200,
'headers': {'Content-Type': 'application/json'},
#'body': json.dumps(response['Items'], cls=DecimalEncoder)
'body': response['Items']
}
return jsonify(response)
@app.route("/write_items", methods=['POST'])
def write_items():
from botocore.config import Config
from boto3.dynamodb.conditions import Key
import boto3
import json
body = request.get_data(as_text=True)
event = json.loads(body)
#event = json.loads(event['body'])
#declare table instance
dynamodb = boto3.resource('dynamodb', region_name='ap-northeast-1')
TableName = event['table']
table = dynamodb.Table(TableName)
with table.batch_writer() as batch:
for item in event['payload']:
batch.put_item(Item = item)
response = {
'statusCode': 200,
'headers': {'Content-Type': 'application/json'},
'body': 'create success'
}
return jsonify(response)
if __name__ == '__main__':
app.run(debug=True)