This repository was archived by the owner on Feb 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathdeals_api_solution.py
More file actions
62 lines (59 loc) · 1.69 KB
/
deals_api_solution.py
File metadata and controls
62 lines (59 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
52
53
54
55
56
57
58
59
60
61
62
import boto3
import json
import base64
deals = [
{
'url': 'https://example.com/deals/001',
'name': 'Deal 001',
'description': 'Deal 001 description',
'price': '100',
'currency': 'USD',
},
{
'url': 'https://example.com/deals/002',
'name': 'Deal 002',
'description': 'Deal 002 description',
'price': '200',
'currency': 'USD'
},
{
'url': 'https://example.com/deals/003',
'name': 'Deal 003',
'description': 'Deal 003 description',
'price': '300',
'currency': 'USD'
}
]
# Function to convert json to xml
def json_to_xml(json_data):
xml = '<deals>'
for deal in json_data:
xml += '<deal>'
xml += '<url>' + deal['url'] + '</url>'
xml += '<name>' + deal['name'] + '</name>'
xml += '<description>' + deal['description'] + '</description>'
xml += '<price>' + deal['price'] + '</price>'
xml += '<currency>' + deal['currency'] + '</currency>'
xml += '</deal>'
xml += '</deals>'
return xml
# Lambda function to query deals info
def lambda_handler(event, context):
print(event)
try:
accepts = event['headers']['Accept']
if accepts == 'application/xml':
print(json_to_xml(deals))
return {
'statusCode': 200,
'headers': {'content-type': 'application/xml'},
'body': json_to_xml(deals)
}
return {
'statusCode': 200,
'headers': {'content-type': 'application/json'},
'body': json.dumps(deals)
}
except Exception as e:
print(e)
return {'statusCode': 500}