-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambda_function_create_internship.py
More file actions
51 lines (44 loc) · 1.69 KB
/
Lambda_function_create_internship.py
File metadata and controls
51 lines (44 loc) · 1.69 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
# create_internship.py
import json, time, uuid
import boto3
from decimal import Decimal
REGION = 'ap-south-1'
dynamodb = boto3.resource('dynamodb', region_name=REGION)
table = dynamodb.Table('Internships')
CORS_HEADERS = {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token",
"Access-Control-Allow-Methods": "GET,POST,OPTIONS"
}
def decimal_default(obj):
if isinstance(obj, Decimal):
if obj % 1 == 0:
return int(obj)
return float(obj)
raise TypeError
def lambda_handler(event, context):
# POST /internships
try:
body = json.loads(event.get('body') or "{}")
except Exception:
return {"statusCode":400,"headers":CORS_HEADERS,"body":json.dumps({"error":"invalid JSON"})}
title = (body.get('title') or '').strip()
if not title:
return {"statusCode":400,"headers":CORS_HEADERS,"body":json.dumps({"error":"title required"})}
internship_id = str(uuid.uuid4())
item = {
"internshipId": internship_id,
"title": title,
"requiredSkills": body.get('requiredSkills', []),
"company": body.get('company',''),
"location": body.get('location',''),
"description": body.get('description',''),
"duration": body.get('duration',''),
"postedAt": int(time.time())
}
try:
table.put_item(Item=item)
except Exception as e:
return {"statusCode":500,"headers":CORS_HEADERS,"body":json.dumps({"error":"dynamodb error","detail":str(e)})}
return {"statusCode":201,"headers":CORS_HEADERS,"body":json.dumps(item, default=decimal_default)}