diff --git a/main.py b/main.py index 8b4c0b8..c42c8a1 100644 --- a/main.py +++ b/main.py @@ -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 "

Welcome to sunnah.com API.

" @@ -64,12 +72,12 @@ def api_collections(): return HadithCollection.query.order_by(HadithCollection.collectionID) @app.route('/v1/collections/', 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//books', methods=['GET']) @paginate_results @@ -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//books/', 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//books//hadiths', methods=['GET']) @paginate_results @@ -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//books//hadiths/', 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//books//chapters', methods=['GET']) @paginate_results def api_collection_book_chapters(collection_name, bookNumber): @@ -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//books//chapters/', 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') diff --git a/specs/collection_book_hadith.yaml b/specs/collection_book_hadith.yaml new file mode 100644 index 0000000..47ece02 --- /dev/null +++ b/specs/collection_book_hadith.yaml @@ -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" diff --git a/specs/hadiths_random.yml b/specs/hadiths_random.yml new file mode 100644 index 0000000..3a14dc4 --- /dev/null +++ b/specs/hadiths_random.yml @@ -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"