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
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,32 +25,38 @@ 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
$ 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
Expand Down
17 changes: 16 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import flask
from flask import jsonify

app = flask.Flask(__name__)
app = flask.Flask(__name__)
app.config["DEBUG"] = True

hadiths = [
Expand All @@ -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/<int:collection_id>/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/<int:collection_id>/books/<int:book_id>/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]
Expand All @@ -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'
)