From a3ea3f20570202622f233e727e97738080f34f81 Mon Sep 17 00:00:00 2001 From: TalibY22 Date: Thu, 24 Oct 2024 12:33:17 +0300 Subject: [PATCH 1/3] Documentation_added --- sunnah-api-docs.md | 120 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 sunnah-api-docs.md diff --git a/sunnah-api-docs.md b/sunnah-api-docs.md new file mode 100644 index 0000000..164c88b --- /dev/null +++ b/sunnah-api-docs.md @@ -0,0 +1,120 @@ +# Sunnah.com API Documentation + +## Overview +This API provides programmatic access to Hadith collections from sunnah.com. The API follows RESTful principles and returns JSON responses. + +## Base URL +``` +https://api.sunnah.com/v1 +``` + +## Authentication +For non-debug environments, all requests must include the `x-aws-secret` header with the correct AWS secret value. + +## Pagination +Most endpoints return paginated results with the following structure: +```json +{ + "data": [], // Array of items + "total": 0, // Total number of items + "limit": 50, // Items per page + "previous": null, // Previous page number + "next": 2 // Next page number +} +``` + +Query parameters: +- `page`: Page number (default: 1) +- `limit`: Items per page (default: 50, max: 100) + +## Endpoints + +### Collections + +#### List Collections +``` +GET /collections +``` +Returns a paginated list of all Hadith collections. + +#### Get Collection +``` +GET /collections/{name} +``` +Returns details for a specific collection. + +### Books + +#### List Books in Collection +``` +GET /collections/{name}/books +``` +Returns a paginated list of books within a collection. + +#### Get Book +``` +GET /collections/{name}/books/{bookNumber} +``` +Returns details for a specific book within a collection. + +### Chapters + +#### List Chapters in Book +``` +GET /collections/{collection_name}/books/{bookNumber}/chapters +``` +Returns a paginated list of chapters within a book. + +#### Get Chapter +``` +GET /collections/{collection_name}/books/{bookNumber}/chapters/{chapterId} +``` +Returns details for a specific chapter. Note: `chapterId` is a float value. + +### Hadiths + +#### List Hadiths in Book +``` +GET /collections/{collection_name}/books/{bookNumber}/hadiths +``` +Returns a paginated list of hadiths within a book. + +#### Get Hadith by Collection +``` +GET /collections/{collection_name}/hadiths/{hadithNumber} +``` +Returns a specific hadith from a collection. + +#### Get Hadith by URN +``` +GET /hadiths/{urn} +``` +Returns a hadith by its URN (Uniform Resource Number). Works with both Arabic and English URNs. + +#### Get Random Hadith +``` +GET /hadiths/random +``` +Returns a random hadith from the Riyadus Salihin collection. + +## Error Handling +The API returns HTTP status codes along with JSON error responses in the following format: +```json +{ + "error": { + "details": "Error description", + "code": 401 + } +} +``` + +Common error codes: +- 401: Unauthorized (invalid or missing AWS secret) +- 404: Resource not found + +## Notes +- All successful responses will return either a single resource object or a paginated list of resources +- Book numbers should be passed as strings +- Chapter IDs are float values +- URNs are integer values +- The random hadith endpoint currently only returns hadiths from the Riyadus Salihin collection From d29ebca0fb75c28ccd85a2f1ec613e3ba0ed3658 Mon Sep 17 00:00:00 2001 From: TalibY22 Date: Mon, 2 Dec 2024 14:53:12 +0300 Subject: [PATCH 2/3] Way_to_export_data_in_the_api --- main.py | 48 +++++++++++++++++++++++++++++++++++++++++++++++- requirements.txt | 1 + 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 5ca97a6..d8a26bf 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,11 @@ import functools -from flask import Flask, jsonify, request, abort +from flask import Flask, jsonify, request, abort,Response, make_response, render_template_string from sqlalchemy import func, or_ from werkzeug.exceptions import HTTPException +import csv +from io import StringIO + +from weasyprint import HTML app = Flask(__name__) app.config.from_object("config.Config") @@ -120,5 +124,47 @@ def api_hadiths_random(): return Hadith.query.filter_by(collection="riyadussalihin").order_by(func.rand()) + +#CSV EXPORT +@app.route("/v1/collections/export/csv", methods=["GET"]) +def export_collections_csv(): + collections = HadithCollection.query.all() + si = StringIO() + writer = csv.writer(si) + writer.writerow(["Collection ID", "Name", "Description"]) + for collection in collections: + writer.writerow([collection.collectionID, collection.name, collection.description]) + + output = make_response(si.getvalue()) + output.headers["Content-Disposition"] = "attachment; filename=collections.csv" + output.headers["Content-Type"] = "text/csv" + return output + +#PDF export +@app.route("/v1/collections/export/pdf", methods=["GET"]) +def export_collections_pdf(): + collections = HadithCollection.query.all() + html_content = render_template_string( + """ + + + Collections + +

Hadith Collections

+ + + + """, collections=collections + ) + pdf = HTML(string=html_content).write_pdf() + response = make_response(pdf) + response.headers["Content-Type"] = "application/pdf" + response.headers["Content-Disposition"] = "inline; filename=collections.pdf" + return response + if __name__ == "__main__": app.run(host="0.0.0.0") diff --git a/requirements.txt b/requirements.txt index 4d2d323..7d08509 100644 --- a/requirements.txt +++ b/requirements.txt @@ -36,3 +36,4 @@ virtualenv==20.0.25 Werkzeug==3.0.6 wrapt==1.12.1 zipp==3.19.1 +weasyprint == 63.0 From 2236094504ad610afe03f556b81c5c816a0823cb Mon Sep 17 00:00:00 2001 From: TalibY22 Date: Mon, 2 Dec 2024 14:54:37 +0300 Subject: [PATCH 3/3] . --- sunnah-api-docs.md | 120 --------------------------------------------- 1 file changed, 120 deletions(-) delete mode 100644 sunnah-api-docs.md diff --git a/sunnah-api-docs.md b/sunnah-api-docs.md deleted file mode 100644 index 164c88b..0000000 --- a/sunnah-api-docs.md +++ /dev/null @@ -1,120 +0,0 @@ -# Sunnah.com API Documentation - -## Overview -This API provides programmatic access to Hadith collections from sunnah.com. The API follows RESTful principles and returns JSON responses. - -## Base URL -``` -https://api.sunnah.com/v1 -``` - -## Authentication -For non-debug environments, all requests must include the `x-aws-secret` header with the correct AWS secret value. - -## Pagination -Most endpoints return paginated results with the following structure: -```json -{ - "data": [], // Array of items - "total": 0, // Total number of items - "limit": 50, // Items per page - "previous": null, // Previous page number - "next": 2 // Next page number -} -``` - -Query parameters: -- `page`: Page number (default: 1) -- `limit`: Items per page (default: 50, max: 100) - -## Endpoints - -### Collections - -#### List Collections -``` -GET /collections -``` -Returns a paginated list of all Hadith collections. - -#### Get Collection -``` -GET /collections/{name} -``` -Returns details for a specific collection. - -### Books - -#### List Books in Collection -``` -GET /collections/{name}/books -``` -Returns a paginated list of books within a collection. - -#### Get Book -``` -GET /collections/{name}/books/{bookNumber} -``` -Returns details for a specific book within a collection. - -### Chapters - -#### List Chapters in Book -``` -GET /collections/{collection_name}/books/{bookNumber}/chapters -``` -Returns a paginated list of chapters within a book. - -#### Get Chapter -``` -GET /collections/{collection_name}/books/{bookNumber}/chapters/{chapterId} -``` -Returns details for a specific chapter. Note: `chapterId` is a float value. - -### Hadiths - -#### List Hadiths in Book -``` -GET /collections/{collection_name}/books/{bookNumber}/hadiths -``` -Returns a paginated list of hadiths within a book. - -#### Get Hadith by Collection -``` -GET /collections/{collection_name}/hadiths/{hadithNumber} -``` -Returns a specific hadith from a collection. - -#### Get Hadith by URN -``` -GET /hadiths/{urn} -``` -Returns a hadith by its URN (Uniform Resource Number). Works with both Arabic and English URNs. - -#### Get Random Hadith -``` -GET /hadiths/random -``` -Returns a random hadith from the Riyadus Salihin collection. - -## Error Handling -The API returns HTTP status codes along with JSON error responses in the following format: -```json -{ - "error": { - "details": "Error description", - "code": 401 - } -} -``` - -Common error codes: -- 401: Unauthorized (invalid or missing AWS secret) -- 404: Resource not found - -## Notes -- All successful responses will return either a single resource object or a paginated list of resources -- Book numbers should be passed as strings -- Chapter IDs are float values -- URNs are integer values -- The random hadith endpoint currently only returns hadiths from the Riyadus Salihin collection