-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddressDB.py
77 lines (68 loc) · 1.73 KB
/
addressDB.py
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
import boto3
client = boto3.client('dynamodb')
tablename = 'AddressMappings'
def storeMapping(user_id, mapping, address):
print(mapping, address)
exists = True
current = client.get_item(
TableName = tablename,
Key={
'user_id': {
'S': user_id
}
}
)
if 'Item' not in current:
response = client.put_item(
TableName = tablename,
Item={
'user_id': {
'S': user_id
},
'mappings': {
'L': [
{"M": {mapping: {"S": address}}}
]
}
}
)
else:
response = client.update_item(
TableName = tablename,
Key={
'user_id': {
'S': user_id
}
},
UpdateExpression='SET mappings = list_append(mappings, :address_obj)',
ExpressionAttributeValues={
':address_obj':{
'L':[
{"M": {mapping: {"S": address}}}
]
}
}
)
def getMapping(user_id, mapping):
response = client.get_item(
TableName = tablename,
Key={
'user_id': {
'S': user_id
}
}
)
if 'Item' not in response:
return 1
mappings = response['Item']['mappings']['L']
try:
address = None
for i in mappings:
if mapping in i['M']:
address = i['M'][mapping]
if address is not None:
return address['S']
else:
return 2
except:
return 3