diff --git a/README.md b/README.md index 18a1414..54ab5f9 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ This project will eventually allow consuming a Hadith API built on python's flas python 3+ is required to run the project. ## Package Structure + ``` ├── README.md ├── main.py @@ -24,18 +25,20 @@ pip install -r requirements.txt python main.py ``` -# Build +# Build Build using: ```bash $ docker build -t sunnah-com/api . ``` + Run with: ```bash $ docker run --init -p 5000:5000 sunnah-com/api ``` + You can then visit [localhost:5000](http://localhost:5000) to verify that it's running on your machine. Or, alternatively: ```bash @@ -43,13 +46,17 @@ $ curl http://localhost:5000 ``` ## Routes -### `GET /collections` + +### `GET /v1/collections` Retrieves list of all collections -### `GET /collections/{collection_id}` +### `GET /v1/collections/{collection_id}` Retrieves a single collection by id -### `GET /collections/{collection_id}/books/{book_id}/hadiths` +### `GET /v1/collections/{collection_id}/books` +Retrieves list of all books in a given collection + +### `GET /v1/collections/{collection_id}/books/{book_id}/hadiths` Retrieves list of all hadith in a given collection and book ### TODO diff --git a/main.py b/main.py index a47e325..5b214c9 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ import flask from flask import jsonify -app = flask.Flask(__name__) +app = flask.Flask(__name__) app.config["DEBUG"] = True hadiths = [ @@ -28,6 +28,11 @@ def api_collection(collection_id): result = [collection for collection in collections() if collection['collection_id'] == collection_id ] return jsonify(result) +@app.route('/v1/collections//books', methods=['GET']) +def api_books(collection_id): + result = [book for book in books() if book['collection_id'] == collection_id] + return jsonify(result) + @app.route('/v1/collections//books//hadiths', methods=['GET']) def api_hadiths(collection_id, book_id): result = [hadith for hadith in hadiths if hadith['collection_id'] == collection_id and hadith['book_id'] == book_id] @@ -39,6 +44,16 @@ def collections(): def collection(hadith): return {'collection': hadith['collection'], 'collection_id': hadith['collection_id']} +def books(): + return [book(hadith) for hadith in hadiths] + +def book(hadith): + return { + 'book': hadith['book'], + 'book_id': hadith['book_id'], + 'collection_id': hadith['collection_id'] + } + app.run( host='0.0.0.0' )