A Flask API that simulates the behavior of an audio file server while using a SQL database.
- Create a Python virtual environment
python3 -m venv phonic_env
- Activate Virtual Environment
source phonic_env/bin/activate
- Install Project requirements
pip install -r requirements.txt
OR
- Using Setup.py
pip install -e
- Setup Flask Configuration & Run.
export FLASK_APP=phonic
export FLASK_ENV=development
flask run
Check: http://127.0.0.1:5000/
- Create a Python virtual environment
python -m venv phonic_env
- Activate Virtual Environment
.\phonic_env\Scripts\activate
- Install Requirements
pip install -r requirements.txt
OR
- Using Setup.py
pip install -e
- Setup Flask Configuration & Run.
set FLASK_APP=phonic
set FLASK_ENV=development
flask run
Check: http://127.0.0.1:5000/
- API URI = "/api/audioFileType/audioFileID"
-
| └── Integer └── song, podcast or audiobook
Example 1: Add a Song to the Database using POST
import requests
import json
url = "http://127.0.0.1:5000/api/song/1"
payload = json.dumps({
"name": "Song 1",
"duration": 100
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
Example 2: Obtain all Songs stored in the Database using GET
import requests
import json
url = "http://127.0.0.1:5000/api/song"
payload = json.dumps({
"name": "Song 1",
"duration": 100
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
Example 3: Change Song 1 name using PUT
import requests
import json
url = "http://127.0.0.1:5000/api/song/1"
payload = json.dumps({
"name": "Song One",
"duration": 100
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("PUT", url, headers=headers, data=payload)
print(response.text)
Example 4: Delete Song 1 name using DELETE
import requests
import json
url = "http://127.0.0.1:5000/api/song/1"
payload = json.dumps({
"name": "Song One",
"duration": 100
})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("DELETE", url, headers=headers, data=payload)
print(response.text)
- Run all tests
python -m pytest -vv
- Run all tests with coverage
coverage -m pytest
- Generate Coverage Report
coverage report
Note: delete the phonic_test_db.sqlite file before Setup 1 always.
.
└── phonic/ # phonic application directory.
├── __init__.py # phonic Flask Application Factory.
├── audio.py # API Blueprint with views.
├── models.py # Backend Data Models.
└── schema.py # Marshmallow Schema Parser init.
├── tests/ # Automated Test Cases.
├── conftest.py # pytest testing fixtures configuration.
├── test_api_delete.py # Test Cases for DELETE (DELETE Operation) of the API.
├── test_api_get.py # Test Cases for GET (READ Operation) of the API.
├── test_api_post.py # Test Cases for POST (CREATE Operation) of the API.
├── test_api_put.py # Test Cases for PUT (UPDATE Operation) of the API.
├── test_database.py # Test Case for checking if Database file is created.
└── test_factory.py # Test Case for checking if Application Factory accepts external configuration.
├── setup.cfg # Setup Configuration File for testing & development.
├── setup.py # Setup file for distribution file generation.
└── requirements.txt # Project Requirements