-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
384 lines (319 loc) · 12.1 KB
/
app.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
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
import os
import uuid
import boto3
import dynamo
import json
import requests
from flask import Flask, jsonify, make_response, request
from urllib import parse
from botocore.exceptions import ClientError
ERROR_HELP_STRINGS = {
# Common Errors
'InternalServerError': 'Internal Server Error, generally safe to retry with exponential back-off',
'ProvisionedThroughputExceededException': 'Request rate is too high. If you\'re using a custom retry strategy make sure to retry with exponential back-off.' +
'Otherwise consider reducing frequency of requests or increasing provisioned capacity for your table or secondary index',
'ResourceNotFoundException': 'One of the tables was not found, verify table exists before retrying',
'ServiceUnavailable': 'Had trouble reaching DynamoDB. generally safe to retry with exponential back-off',
'ThrottlingException': 'Request denied due to throttling, generally safe to retry with exponential back-off',
'UnrecognizedClientException': 'The request signature is incorrect most likely due to an invalid AWS access key ID or secret key, fix before retrying',
'ValidationException': 'The input fails to satisfy the constraints specified by DynamoDB, fix input before retrying',
'RequestLimitExceeded': 'Throughput exceeds the current throughput limit for your account, increase account level throughput before retrying',
}
app = Flask(__name__)
dynamodb_client = boto3.client('dynamodb')
sqs_client = boto3.client('sqs')
roofing_api_url = "https://e6b1hc9rfg.execute-api.us-east-1.amazonaws.com"
if os.environ.get('IS_OFFLINE') == 'True':
dynamodb_client = boto3.client(
'dynamodb', region_name='localhost', endpoint_url='http://localhost:8000'
)
roofing_api_url = "http://localhost:2000"
PPL_TABLE = os.environ['PPL_TABLE']
@app.route('/roofer', methods=['POST'])
def create_roofer():
print(os.environ)
# print(request.environ)
pk = f"Roofer#{str(uuid.uuid4())}"
sk = "ROOFER"
dynamo_data = dynamo.to_item(request.json)
print(f'::REQUEST:JSON ==> {request.json}')
if not "Email" in request.json.keys():
return jsonify({'error': 'Please provide key value pair with key "Email"'}), 400
# check if email already exists in roofer db
get_roofer_by_email_url = f"{roofing_api_url}/roofer/email/{parse.quote(request.json['Email'])}"
response = requests.request("GET", get_roofer_by_email_url)
print(response.text)
roofer_record = json.loads(response.text)
if roofer_record:
roofer_record.update({'roofer_exists': True})
return jsonify(roofer_record), 200
dynamo_data['pk'] = {'S': pk}
dynamo_data['sk'] = {'S': sk}
dynamodb_client.put_item(
TableName=PPL_TABLE, Item=dynamo_data
)
name = None
if "First Name" in request.json.keys():
name = f"{request.json['First Name']} {request.json['Last Name']}"
email = request.json['Email']
output_object = {
'pk': pk,
'sk': sk,
'name': name,
'email': email,
'roofer_exists': False,
'phone': request.json['Phone']
}
return jsonify(output_object), 201
@app.route('/roofer/<string:pk>', methods=['GET'])
def get_roofer(pk):
sk = "ROOFER"
input = {
"TableName": PPL_TABLE,
"KeyConditionExpression": "#bef90 = :bef90 And #bef91 = :bef91",
"ExpressionAttributeNames": {"#bef90":"pk","#bef91":"sk"},
"ExpressionAttributeValues": {":bef90": {"S":pk},":bef91": {"S":sk}}
}
try:
response = dynamodb_client.query(**input)
print("Query successful.")
# Handle response
except ClientError as error:
handle_error(error)
except BaseException as error:
print("Unknown error while querying: " + error.response['Error']['Message'])
item = response.get('Items', [])
if not item:
return jsonify({'error': 'Could not find roofer with provided "pk"'})
item = item[0]
dict_data = dynamo.to_dict(item)
return jsonify(
dict_data
)
@app.route('/roofer/', methods=['GET'])
def get_all_roofers():
input = {
"TableName": PPL_TABLE,
"FilterExpression": "#766a0 = :766a0",
"ExpressionAttributeNames": {"#766a0":"sk"},
"ExpressionAttributeValues": {":766a0": {"S":"ROOFER"}}
}
try:
response = dynamodb_client.scan(**input)
print("Query successful.")
# Handle response
except ClientError as error:
handle_error(error)
except BaseException as error:
print("Unknown error while querying: " + error.response['Error']['Message'])
items = response.get('Items', [])
if not items:
return jsonify({'error': 'Could not find any roofers'})
dict_data = []
for item in items:
dict_data.append(dynamo.to_dict(item))
return jsonify(
dict_data
)
@app.route('/roofer/<string:pk>', methods=['PUT'])
def update_roofer(pk):
update_data = request.json
sk = "ROOFER"
# input = {
# "TableName": PPL_TABLE,
# "Key": {
# "pk": {"S": pk },
# "sk": {"S":sk}
# },
# "UpdateExpression": "SET #7b390 = :7b390",
# "ExpressionAttributeNames": {"#7b390":"StripeId"},
# "ExpressionAttributeValues": {":7b390": {"S":stripe_id}}
# }
# response = dynamodb_client.query(**input)
update_expression = "SET "
expression_attribute_values = {}
for k, v in update_data.items():
update_expression += f"{k} = :{k},"
expression_attribute_values[f":{k}"] = {"S": v}
# remove last comma from update expression
update_expression = update_expression[:-1]
print(expression_attribute_values)
response = dynamodb_client.update_item(
TableName=PPL_TABLE,
Key={'pk': {'S': pk}, 'sk': {'S': sk}},
UpdateExpression=update_expression,
ExpressionAttributeValues=expression_attribute_values,
ReturnValues="UPDATED_NEW"
)
print("Query successful.")
attributes = response.get('Attributes')
if not attributes:
return jsonify({'error': 'Could not find user with provided "pk"'}), 404
dict_data = dynamo.to_dict(attributes)
return jsonify(
dict_data
)
@app.route('/lead', methods=['POST'])
def create_lead():
pk = f"Lead#{str(uuid.uuid4())}"
sk = "LEAD"
dynamo_data = dynamo.to_item(request.json)
dynamo_data['pk'] = {'S': pk}
dynamo_data['sk'] = {'S': sk}
dynamodb_client.put_item(
TableName=PPL_TABLE, Item=dynamo_data
)
return jsonify({'pk': pk, 'sk':sk, 'data': request.json})
@app.route('/lead/<string:pk>', methods=['PUT'])
def update_lead(pk):
update_data = request.json
sk = "LEAD"
update_expression = "SET "
expression_attribute_values = {}
for k, v in update_data.items():
update_expression += f"{k} = :{k},"
expression_attribute_values[f":{k}"] = {"S": v}
# remove last comma from update expression
update_expression = update_expression[:-1]
print(expression_attribute_values)
response = dynamodb_client.update_item(
TableName=PPL_TABLE,
Key={'pk': {'S': pk}, 'sk': {'S': sk}},
UpdateExpression=update_expression,
ExpressionAttributeValues=expression_attribute_values,
ReturnValues="UPDATED_NEW"
)
print("Query successful.")
attributes = response.get('Attributes')
if not attributes:
return jsonify({'error': 'Could not find lead with provided "pk"'}), 404
dict_data = dynamo.to_dict(attributes)
return jsonify(
dict_data
)
@app.route('/lead/<string:pk>', methods=['GET'])
def get_lead(pk):
sk = "LEAD"
input = {
"TableName": PPL_TABLE,
"KeyConditionExpression": "#bef90 = :bef90 And #bef91 = :bef91",
"ExpressionAttributeNames": {"#bef90":"pk","#bef91":"sk"},
"ExpressionAttributeValues": {":bef90": {"S":pk},":bef91": {"S":sk}}
}
try:
response = dynamodb_client.query(**input)
print("Query successful.")
# Handle response
except ClientError as error:
handle_error(error)
except BaseException as error:
print("Unknown error while querying: " + error.response['Error']['Message'])
item = response.get('Items')[0]
if not item:
return jsonify({'error': 'Could not find user with provided "pk"'}), 404
dynamo_data = dynamo.to_dict(item)
return jsonify(
dynamo_data
)
@app.route('/lead_purchase', methods=['POST'])
def create_lead_purchase():
pk = request.json.pop('roofer')
sk = request.json.pop('lead')
dynamo_data = dynamo.to_item(request.json)
if not pk or not sk:
return jsonify({'error': 'Please provide both "pk" and "data"'}), 400
dynamo_data['pk'] = {'S': pk}
dynamo_data['sk'] = {'S': sk}
dynamodb_client.put_item(
TableName=PPL_TABLE, Item=dynamo_data
)
return jsonify({'pk': pk, 'sk':sk, 'success': True})
@app.route('/roofer/lead/<string:pk>', methods=['GET'])
def get__roofer_leads(pk):
pk = parse.unquote(pk)
input = {
"TableName": "ppl-table-3-dev",
"KeyConditionExpression": "#bef90 = :bef90 And begins_with(#bef91, :bef91)",
"ExpressionAttributeNames": {"#bef90":"pk","#bef91":"sk"},
"ExpressionAttributeValues": {":bef90": {"S":pk},":bef91": {"S":"Lead#"}}
}
try:
response = dynamodb_client.query(**input)
print("Query successful.")
# Handle response
except ClientError as error:
handle_error(error)
except BaseException as error:
print("Unknown error while querying: " + error.response['Error']['Message'])
items = response.get('Items')
if not items:
return jsonify([])
lead_purchases = list((x for x in items if x.get('sk').get('S') != 'ROOFER'))
leads = []
for i in lead_purchases:
leads.append(dynamo.to_dict(i))
return jsonify(
leads
)
@app.route('/roofer/email/<string:email>', methods=['GET'])
def get_roofer_by_email(email):
input = {
"TableName": PPL_TABLE,
"IndexName": "Email",
"KeyConditionExpression": "#84ad0 = :84ad0",
"ExpressionAttributeNames": {"#84ad0":"Email"},
"ExpressionAttributeValues": {":84ad0": {"S":email}}
}
try:
response = dynamodb_client.query(**input)
print("Query successful.")
# Handle response
except ClientError as error:
handle_error(error)
except BaseException as error:
print("Unknown error while querying: " + error.response['Error']['Message'])
try:
item = response.get('Items')[0]
except IndexError:
return jsonify([])
if not item:
return jsonify({'error': 'Could not find user with provided "email"'}), 404
dict_data = dynamo.to_dict(item)
return jsonify(
dict_data
), 200
@app.route('/roofer/stripe_id/<string:StripeId>', methods=['GET'])
def get_roofer_by_stripe_id(StripeId):
input = {
"TableName": PPL_TABLE,
"IndexName": "StripeId",
"KeyConditionExpression": "#84ad0 = :84ad0",
"ExpressionAttributeNames": {"#84ad0":"StripeId"},
"ExpressionAttributeValues": {":84ad0": {"S":StripeId}}
}
try:
response = dynamodb_client.query(**input)
print("Query successful.")
# Handle response
except ClientError as error:
handle_error(error)
except BaseException as error:
print("Unknown error while querying: " + error.response['Error']['Message'])
item = response.get('Items')[0]
if not item:
return jsonify({'error': 'Could not find user with provided "email"'}), 404
dict_data = dynamo.to_dict(item)
return jsonify(
dict_data
)
@app.errorhandler(404)
def resource_not_found(e):
return make_response(jsonify(error='Not found!'), 404)
def handle_error(error):
error_code = error.response['Error']['Code']
error_message = error.response['Error']['Message']
error_help_string = ERROR_HELP_STRINGS[error_code]
print('[{error_code}] {help_string}. Error message: {error_message}'
.format(error_code=error_code,
help_string=error_help_string,
error_message=error_message))