Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ def decorated_function(*args, **kwargs):
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.</p>"
Expand All @@ -64,12 +72,12 @@ def api_collections():
return HadithCollection.query.order_by(HadithCollection.collectionID)

@app.route('/v1/collections/<string:name>', methods=['GET'])
@single_resource
def api_collection(name):
"""
swagger_from_file: specs/collection.yaml
"""
collection = HadithCollection.query.filter_by(name=name).first_or_404()
return jsonify(collection.serialize())
return HadithCollection.query.filter_by(name=name)

@app.route('/v1/collections/<string:name>/books', methods=['GET'])
@paginate_results
Expand All @@ -80,13 +88,13 @@ def api_collection_books(name):
return Book.query.filter_by(collection=name).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):
"""
swagger_from_file: specs/collection_book.yaml
"""
book_id = Book.get_id_from_number(bookNumber)
book = Book.query.filter_by(collection=name, ourBookID=book_id).first_or_404()
return jsonify(book.serialize())
return Book.query.filter_by(collection=name, ourBookID=book_id)

@app.route('/v1/collections/<string:collection_name>/books/<string:bookNumber>/hadiths', methods=['GET'])
@paginate_results
Expand All @@ -96,6 +104,14 @@ 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>/books/<string:bookNumber>/hadiths/<string:hadithNumber>', methods=['GET'])
@single_resource
def api_collection_book_hadith(collection_name, bookNumber, hadithNumber):
"""
swagger_from_file: specs/collection_book_hadith.yaml
"""
return Hadith.query.filter_by(collection=collection_name, bookNumber=bookNumber, 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):
Expand All @@ -106,10 +122,19 @@ def api_collection_book_chapters(collection_name, 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)
chapter = Chapter.query.filter_by(collection=collection_name, arabicBookID=book_id, babID=chapterId).first_or_404()
return jsonify(chapter.serialize())
return Chapter.query.filter_by(collection=collection_name, arabicBookID=book_id, babID=chapterId)

@app.route('/v1/hadiths/random', methods=['GET'])
@single_resource
def api_hadiths_random():
"""
swagger_from_file: specs/hadiths_random.yaml
"""
# TODO Make this configurable instead of hardcoding
return Hadith.query.filter_by(collection='riyadussaliheen').order_by(func.rand())

if __name__ == '__main__':
app.run(host='0.0.0.0')
56 changes: 56 additions & 0 deletions specs/collection_book_hadith.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Get a hadith of a book for a collection
---
definitions:
- schema:
id: Hadith
properties:
collection:
type: string
description: the Collection's name
bookNumber:
type: string
description: The number of the book this hadith belongs to
chapterId:
type: string
description: The id of the chapter this hadith belongs to
hadithNumber:
type: integer
hadith:
type: object
description: Language specific data of the hadith
parameters:
lang:
type: string
chapterNumber:
type: string
chapterTitle:
type: string
urn:
type: string
body:
type: string
grade:
type: string

parameters:
- in: path
name: collection_name
description: name of collection
required: true
type: string
- in: path
name: bookNumber
description: number of the book
required: true
type: string
- in: path
name: hadithNumber
description: number of the hadith
required: true
type: string

responses:
200:
description: Hadith of a book of a collection
schema:
$ref: "#/definitions/Hadith"
40 changes: 40 additions & 0 deletions specs/hadiths_random.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Get a randomly selected hadith
---
definitions:
- schema:
id: Hadith
properties:
collection:
type: string
description: the Collection's name
bookNumber:
type: string
description: The number of the book this hadith belongs to
chapterId:
type: string
description: The id of the chapter this hadith belongs to
hadithNumber:
type: integer
hadith:
type: object
description: Language specific data of the hadith
parameters:
lang:
type: string
chapterNumber:
type: string
chapterTitle:
type: string
urn:
type: string
body:
type: string
grade:
type: string


responses:
200:
description: A randomly selected hadith
schema:
$ref: "#/definitions/Hadith"