-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathmain.py
124 lines (86 loc) · 4.02 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import functools
from flask import Flask, jsonify, request, abort
from sqlalchemy import func, or_
from werkzeug.exceptions import HTTPException
app = Flask(__name__)
app.config.from_object("config.Config")
from models import HadithCollection, Book, Chapter, Hadith
@app.before_request
def verify_secret():
if not app.debug and request.headers.get("x-aws-secret") != app.config["AWS_SECRET"]:
abort(401)
@app.errorhandler(HTTPException)
def jsonify_http_error(error):
response = {"error": {"details": error.description, "code": error.code}}
return jsonify(response), error.code
def paginate_results(f):
@functools.wraps(f)
def decorated_function(*args, **kwargs):
limit = int(request.args.get("limit", 50))
page = int(request.args.get("page", 1))
queryset = f(*args, **kwargs).paginate(page=page, per_page=limit, max_per_page=100)
result = {
"data": [x.serialize() for x in queryset.items],
"total": queryset.total,
"limit": queryset.per_page,
"previous": queryset.prev_num,
"next": queryset.next_num,
}
return jsonify(result)
return decorated_function
def single_resource(f):
@functools.wraps(f)
def decorated_function(*args, **kwargs):
result = f(*args, **kwargs).first_or_404()
result = result.serialize()
return jsonify(result)
return decorated_function
@app.route("/", methods=["GET"])
def home():
return "<h1>Welcome to sunnah.com API.</h1>"
@app.route("/v1/collections", methods=["GET"])
@paginate_results
def api_collections():
return HadithCollection.query.order_by(HadithCollection.collectionID)
@app.route("/v1/collections/<string:name>", methods=["GET"])
@single_resource
def api_collection(name):
return HadithCollection.query.filter_by(name=name)
@app.route("/v1/collections/<string:name>/books", methods=["GET"])
@paginate_results
def api_collection_books(name):
return Book.query.filter_by(collection=name, status=4).order_by(func.abs(Book.ourBookID))
@app.route("/v1/collections/<string:name>/books/<string:bookNumber>", methods=["GET"])
@single_resource
def api_collection_book(name, bookNumber):
book_id = Book.get_id_from_number(bookNumber)
return Book.query.filter_by(collection=name, status=4, ourBookID=book_id)
@app.route("/v1/collections/<string:collection_name>/books/<string:bookNumber>/hadiths", methods=["GET"])
@paginate_results
def api_collection_book_hadiths(collection_name, bookNumber):
return Hadith.query.filter_by(collection=collection_name, bookNumber=bookNumber).order_by(Hadith.englishURN)
@app.route("/v1/collections/<string:collection_name>/hadiths/<string:hadithNumber>", methods=["GET"])
@single_resource
def api_collection_hadith(collection_name, hadithNumber):
return Hadith.query.filter_by(collection=collection_name, hadithNumber=hadithNumber)
@app.route("/v1/collections/<string:collection_name>/books/<string:bookNumber>/chapters", methods=["GET"])
@paginate_results
def api_collection_book_chapters(collection_name, bookNumber):
book_id = Book.get_id_from_number(bookNumber)
return Chapter.query.filter_by(collection=collection_name, arabicBookID=book_id).order_by(Chapter.babID)
@app.route("/v1/collections/<string:collection_name>/books/<string:bookNumber>/chapters/<float:chapterId>", methods=["GET"])
@single_resource
def api_collection_book_chapter(collection_name, bookNumber, chapterId):
book_id = Book.get_id_from_number(bookNumber)
return Chapter.query.filter_by(collection=collection_name, arabicBookID=book_id, babID=chapterId)
@app.route("/v1/hadiths/<int:urn>", methods=["GET"])
@single_resource
def api_hadith(urn):
return Hadith.query.filter(or_(Hadith.arabicURN == urn, Hadith.englishURN == urn))
@app.route("/v1/hadiths/random", methods=["GET"])
@single_resource
def api_hadiths_random():
# TODO Make this configurable instead of hardcoding
return Hadith.query.filter_by(collection="riyadussalihin").order_by(func.rand())
if __name__ == "__main__":
app.run(host="0.0.0.0")