-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlambda_function.py
More file actions
140 lines (124 loc) · 4.66 KB
/
lambda_function.py
File metadata and controls
140 lines (124 loc) · 4.66 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
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
import json
import boto3
from botocore.exceptions import ClientError
from decimal import Decimal
from boto3.dynamodb.conditions import Key
# Initialize the DynamoDB client
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
dynamodb_table = dynamodb.Table('employee_info')
status_check_path = '/status'
employee_path = '/employee'
employees_path = '/employees'
def lambda_handler(event, context):
print('Request event: ', event)
response = None
try:
http_method = event.get('httpMethod')
path = event.get('path')
if http_method == 'GET' and path == status_check_path:
response = build_response(200, 'Service is operational')
elif http_method == 'GET' and path == employee_path:
employee_id = event['queryStringParameters']['employeeid']
response = get_employee(employee_id)
elif http_method == 'GET' and path == employees_path:
response = get_employees()
elif http_method == 'POST' and path == employee_path:
response = save_employee(json.loads(event['body']))
elif http_method == 'PATCH' and path == employee_path:
body = json.loads(event['body'])
response = modify_employee(body['employeeId'], body['updateKey'], body['updateValue'])
elif http_method == 'DELETE' and path == employee_path:
body = json.loads(event['body'])
response = delete_employee(body['employeeId'])
else:
response = build_response(404, '404 Not Found')
except Exception as e:
print('Error:', e)
response = build_response(400, 'Error processing request')
return response
def get_employee(employee_id):
try:
response = dynamodb_table.get_item(Key={'employeeid': employee_id})
return build_response(200, response.get('Item'))
except ClientError as e:
print('Error:', e)
return build_response(400, e.response['Error']['Message'])
def get_employees():
try:
scan_params = {
'TableName': dynamodb_table.name
}
return build_response(200, scan_dynamo_records(scan_params, []))
except ClientError as e:
print('Error:', e)
return build_response(400, e.response['Error']['Message'])
def scan_dynamo_records(scan_params, item_array):
response = dynamodb_table.scan(**scan_params)
item_array.extend(response.get('Items', []))
if 'LastEvaluatedKey' in response:
scan_params['ExclusiveStartKey'] = response['LastEvaluatedKey']
return scan_dynamo_records(scan_params, item_array)
else:
return {'employees': item_array}
def save_employee(request_body):
try:
dynamodb_table.put_item(Item=request_body)
body = {
'Operation': 'SAVE',
'Message': 'SUCCESS',
'Item': request_body
}
return build_response(200, body)
except ClientError as e:
print('Error:', e)
return build_response(400, e.response['Error']['Message'])
def modify_employee(employee_id, update_key, update_value):
try:
response = dynamodb_table.update_item(
Key={'employeeid': employee_id},
UpdateExpression=f'SET {update_key} = :value',
ExpressionAttributeValues={':value': update_value},
ReturnValues='UPDATED_NEW'
)
body = {
'Operation': 'UPDATE',
'Message': 'SUCCESS',
'UpdatedAttributes': response
}
return build_response(200, body)
except ClientError as e:
print('Error:', e)
return build_response(400, e.response['Error']['Message'])
def delete_employee(employee_id):
try:
response = dynamodb_table.delete_item(
Key={'employeeid': employee_id},
ReturnValues='ALL_OLD'
)
body = {
'Operation': 'DELETE',
'Message': 'SUCCESS',
'Item': response
}
return build_response(200, body)
except ClientError as e:
print('Error:', e)
return build_response(400, e.response['Error']['Message'])
class DecimalEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Decimal):
# Check if it's an int or a float
if obj % 1 == 0:
return int(obj)
else:
return float(obj)
# Let the base class default method raise the TypeError
return super(DecimalEncoder, self).default(obj)
def build_response(status_code, body):
return {
'statusCode': status_code,
'headers': {
'Content-Type': 'application/json'
},
'body': json.dumps(body, cls=DecimalEncoder)
}