-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_api.py
71 lines (60 loc) · 2.04 KB
/
web_api.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
"""
JSON Web API
/books : get books with their authors
/books?author=Author Name : filter books by author name
/books?title=Book Title : filter books by book title
/authors : get authors with books they written
"""
import json
import sys
from flask import Flask, request, jsonify
from tools import get_author_id, get_books_from_author_id, get_book_from_title
app = Flask(__name__)
# Get data from json file
try:
with open("books.json") as f:
data = json.load(f)
except FileNotFoundError as err:
print("books.json not found")
print("{}".format(err))
sys.exit(1)
AUTHORS_LIST = data.get("authors")
BOOKS_LIST = data.get("books")
@app.route("/books")
def books():
""" Endpoint /books """
# Get URL parameters
book_title = request.args.get("title")
author_name = request.args.get("author")
# If all parameters are there, we choose to print warning message
if book_title and author_name:
return "Cannot filter by author and book title together"
# Create HTML
if not book_title and not author_name:
# Default return : all books
resp = BOOKS_LIST
elif book_title and not author_name:
# return book for a given book title
book = get_book_from_title(book_title, BOOKS_LIST)
resp = book
else:
# return for a given author name
# Search author id of author author_name
filter_author_id = get_author_id(author_name, AUTHORS_LIST)
resp = get_books_from_author_id(filter_author_id, BOOKS_LIST)
return jsonify(resp)
@app.route("/authors")
def authors():
""" Endpoint /authors """
# return all authors with books they written
resp = list()
for author in AUTHORS_LIST:
# Get written books by author
books_author_list = get_books_from_author_id(author.get("author_id"), BOOKS_LIST)
author['books'] = books_author_list
resp.append(author)
return jsonify(resp)
@app.route("/")
def main():
""" main entry point / """
return "<a href='/books'>Books</a><br/><a href='/authors'>Authors</a>"