-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
105 lines (83 loc) · 3.2 KB
/
app.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
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
# imports
from flask import Flask, request, jsonify
from bookDetailed import BookDetailed
from bookSearch import BookSearch, defaultBookResult
app = Flask(__name__)
def defaultParams():
sortBy = request.args.get('sortBy')
orderBy = request.args.get('order')
page = request.args.get('page')
return {"sortBy": sortBy, "orderBy": orderBy, "page": page}
# routes
@app.route('/')
def home():
return "<h1>home</h1>"
@app.route('/api/default')
def search():
query = request.args.get("query")
other_params = defaultParams()
if query is None:
return jsonify({"result": "invalid query, at least should be 3 characters"})
books = BookSearch(str(query), None, other_params['sortBy'], other_params['orderBy'], other_params['page']).parse()
return jsonify(books)
@app.route('/api/title')
def titleSearch():
title = request.args.get('query')
other_params = defaultParams()
# validating
if title is None or len(title) < 2:
resultDict = defaultBookResult()
resultDict['result'] = "Title should be at-least 2 characters"
return resultDict
books = BookSearch(str(title), 'title', other_params['sortBy'], other_params['orderBy'], other_params['page']).parse()
return jsonify(books)
@app.route('/api/author')
def authorSearch():
author = request.args.get('query')
other_params = defaultParams()
# validating
if author is None or len(author) < 2:
resultDict = defaultBookResult()
resultDict['result'] = "Author name should be at-least 2 characters"
return resultDict
books = BookSearch(str(author), 'author', other_params['sortBy'], other_params['orderBy'],
other_params['page']).parse()
return jsonify(books)
@app.route('/api/publisher')
def publisherSearch():
publisher = request.args.get('query')
other_params = defaultParams()
# validating
if publisher is None or len(publisher) < 2:
resultDict = defaultBookResult()
resultDict['result'] = "Publisher name should be at-least 2 characters"
return resultDict
books = BookSearch(str(publisher), 'publisher', other_params['sortBy'], other_params['orderBy'],
other_params['page']).parse()
return jsonify(books)
@app.route('/api/isbn')
def isbnSearch():
isbn_number = request.args.get('query')
other_params = defaultParams()
if len(isbn_number) == 10 or len(isbn_number) == 13: # validating the ISBN number
books = BookSearch(str(isbn_number), 'identifier', other_params['sortBy'], other_params['orderBy'],
other_params['page']).parse()
return jsonify(books)
else:
resultDict = defaultBookResult()
resultDict['result'] = "Enter a Valid ISBN number"
return resultDict
@app.route('/api/book') # query parameter - id
def bookDetails():
id = request.args.get("id")
if id is None:
resultDict = {}
resultDict['result'] = 'Invalid id'
resultDict['description'] = 'none'
resultDict['download'] = 'none'
return jsonify(resultDict)
else:
book = BookDetailed(str(id)).parse()
return jsonify(book)
if __name__ == "__main__":
app.run(debug=True)