-
Notifications
You must be signed in to change notification settings - Fork 860
/
Copy pathPost_API_Pet_Python.py
96 lines (87 loc) · 2.87 KB
/
Post_API_Pet_Python.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
import boto3
class PetShop:
def __init__(self):
client = boto3.resource('dynamodb')
self.table = client.Table('Pets')
def Create_data(self , event):
response = self.table.put_item(
Item={
'id': event['id'],
'name': event['name'],
'breed': event['breed'],
'gender': event['gender'],
'owner': event['owner'],
'birthday': event['birthday']
}
)
return {
'statusCode': response['ResponseMetadata']['HTTPStatusCode'],
'body': 'Record ' + event['id'] + ' added'
}
def Read_data(self , event):
response = self.table.get_item(
Key={
'id': event['id']
}
)
if 'Item' in response:
return response['Item']
else:
return {
'statusCode': '404',
'body': 'Not found'
}
def Update_data(self , event):
response = self.table.update_item(
Key={'id': event['id']},
ExpressionAttributeNames={
'#N': 'name',
'#B': 'breed',
'#G': 'gender',
'#O': 'owner',
'#K': 'birthday'
},
ExpressionAttributeValues={
':n': event['name'],
':b':event['breed'],
':g': event['gender'],
':o': event['owner'],
':k': event['birthday'],
},
UpdateExpression='SET #N = :n, #B = :b, #G = :g, #O = :o, #K = :k',
ReturnValues="UPDATED_NEW"
)
return {
'statusCode': response['ResponseMetadata']['HTTPStatusCode'],
'body': 'Record ' + event['id'] + ' updated'
}
def Delete_data(self , event):
response = self.table.delete_item(
Key={
'id': event['id']
}
)
return {
'statusCode': '200',
'body': 'Deleted the item with id :' + event['id']
}
def lambda_handler(event, context):
if event:
pet_Object = PetShop()
if event['tasktype'] == "create":
create_result = pet_Object.Create_data(event['data'])
return create_result
elif event['tasktype'] == "read":
read_result = pet_Object.Read_data(event['data'])
return read_result
elif event['tasktype'] == "update":
update_result = pet_Object.Update_data(event['data'])
return update_result
elif event['tasktype'] == "delete":
delete_result = pet_Object.Delete_data(event['data'])
return delete_result
else :
return {
'statusCode': '404',
'body': 'Not found'
}