AFL based API built using python flask with postgreSQL backend
A simple AFL (Australian Football League) themed REST API using Flask
This API utilises SQLAlchemy for an ORM with a PostgreSQL database and Pytest as a test framework.
It is recommended that the user first creates a python virtual environment.
$ python3 -m venv v_env
$ source v_env\bin\activate
The following command will install all dependencies including Flask related packages, SQLAlchemy, Pytest etc.
$ pip3 install -r requirements.txt
Some knowledge of PostgreSQL will be required as the reader will have to create two databases. PostgreSQL and a client, such as ‘psql’, will need to be installed.
To create the main ‘afl’ database:
$ createdb afl
To create the test database (used for API tests):
$ createdb afl_test
To initialise and/or update the database:
$ flask db init
$ flask db migrate
$ flask db upgrade
The app itself is a package and will be installed along with other dependencies.
To use the development server, a couple of environment variables are required:
$ export FLASK_APP=app
$ export FLASK_ENV=development
Start the server:
$ flask run
Response should be something like:
$flask run
* Serving Flask app "app" (lazy loading)
* Environment: development
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
A user must login first in order to get an access token. Once logged in, the user will be able to obtain information regarding teams and players. For both team and player models, the user is able to perform CRUD operations. For most endpoints, the access token must be passed in as a header.
Assuming the development server is running, the URL for the API is http://127.0.0.1:5000/api/v1
Creates a new user in the database
/user/create
POST
{
“username”: \
“password”: \
}
Status Code: 201
Message: {“message”: “user \
Log in user and obtain access token
/user/login
POST
{
“username”: \
“password”: \
}
Status Code: 200
Message: {“access_token”: \
Get all teams
/teams
GET
Content-Type: application/json
Authorization: Bearer \
None
Status Code: 200
Message: {“Count:
Example:
{
"Count: 1,
"Teams": [
{"id": 1,
"location": "Melbourne",
"name": "Essendon",
"premierships": 12,
"wooden_spoons": 7,
"years_in_afl": 104
}]
}
Get team by ID
/team/\
GET
Content-Type: application/json,
Authorization: Bearer <\access_token>
id: \
Status Code: 200
Message: {“message”: “success”, “team”: <{Team}>}
Get team by ID
/team/search
GET / POST
Content-Type: application/json,
Authorization: Bearer <\access_token>
id : \
name: \
location: \
premierships: \
wooden_spoons: \
years_in_afl: \
Status Code: 200
Message: {“count”: \
Get all players
/players
GET
Content-Type: application/json
Authorization: Bearer \
None
Status Code: 200
Message: {“Count:
Example:
{
"count": 3,
"players": [
{
"career_goals": 518,
"dob": "1978-05-14",
"first_name": "Brent",
"id": 1,
"last_name": "Harvey",
"matches_played": 432,
"team_id": 2
},
{
"career_goals": 518,
"dob": "1991-06-26",
"first_name": "Dustin",
"id": 2,
"last_name": "Martin",
"matches_played": 232,
"team_id": 3
},
{
"career_goals": 97,
"dob": "1992-05-14",
"first_name": "Dyson",
"id": 3,
"last_name": "Heppell",
"matches_played": 169,
"team_id": 1
}
]
}
Get player by ID
/player/\
GET
Content-Type: application/json,
Authorization: Bearer <\access_token>
id: \
Status Code: 200
Message: {“message”: “success”, “player”: <{Player}>}
Get player by ID
/player/search
GET / POST
Content-Type: application/json,
Authorization: Bearer <\access_token>
id : \
first_name: \
last_name: \
dob: \
matches_playes: \
career_goals: \
team_id: \
Status Code: 200
Message: {“count”: \