-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlambda_function.py
More file actions
79 lines (66 loc) · 2.79 KB
/
lambda_function.py
File metadata and controls
79 lines (66 loc) · 2.79 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
import json
from botocore.vendored import requests
def lambda_handler(event, context):
#parsing parameters
author = event['queryStringParameters'].get('inauthor', None)
genre = event['queryStringParameters'].get('subject', None)
#building api request string
reqlink='https://www.googleapis.com/books/v1/volumes?q='
if (author):
reqlink=reqlink+'inauthor:'+ author + '+'
if (genre):
reqlink=reqlink+'subject:'+ genre + '+'
reqlink=reqlink+'&maxResults=40'
print('reqlink: '+reqlink)
#making request to Books API
req=requests.get(reqlink)
reqjson=req.json()
print(reqjson)
reqResponse=[]
#checking to make sure the Books API returned results, else return empty response object
if(reqjson['totalItems']==0):
kind=reqjson.get('kind', None)
TI=reqjson.get('totalItems', None)
ret={}
ret['kind']=kind
ret['totalItems']=TI
return{
"statusCode": 200,
"headers": {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'application/json',
'Access-Control-Allow-Methods': 'OPTIONS,POST',
'Access-Control-Allow-Credentials': 'true'
},
"body": json.dumps(ret),
"isBase64Encoded": False,
}
#building our response body with the attributes we care about, looping through all the books and extracting the info we want
for book in reqjson['items']:
title=book['volumeInfo'].get('title', None)
author=book['volumeInfo'].get('authors', None)
if(author!=None):
authorjoin=", ".join(author) #did it update?
author=authorjoin
genre=book['volumeInfo'].get('categories', None)
if(genre!=None):
genrejoin=", ".join(genre)
genre=genrejoin
pageCount=book['volumeInfo'].get('pageCount', None)
id=book.get('id', None)
thumbnail = None
if(book['volumeInfo'].get('imageLinks')):
thumbnail=book['volumeInfo']['imageLinks'].get('thumbnail', None)
MR=book['volumeInfo'].get('maturityRating', None)
reqResponse.append({'type': 'book', 'id': id, 'title': title, 'author': author, 'genre': genre, 'pageCount': pageCount, 'thumbnail': thumbnail, 'maturityRating': MR})
return{
"statusCode": 200,
"headers": {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'application/json',
'Access-Control-Allow-Methods': 'OPTIONS,POST',
'Access-Control-Allow-Credentials': 'true'
},
"body": json.dumps(reqResponse),
"isBase64Encoded": False,
}