项目作者: aharrison-git

项目描述 :
AFL based API built using python flask with postgreSQL backend
高级语言: Python
项目地址: git://github.com/aharrison-git/Flask_afl_api.git
创建时间: 2020-06-09T11:43:06Z
项目社区:https://github.com/aharrison-git/Flask_afl_api

开源协议:

下载


Flask AFL API

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.

Getting Started

It is recommended that the user first creates a python virtual environment.

  1. $ python3 -m venv v_env
  2. $ source v_env\bin\activate

The following command will install all dependencies including Flask related packages, SQLAlchemy, Pytest etc.

  1. $ pip3 install -r requirements.txt

PostgreSQL

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.

Create PostgreSQL databases

To create the main ‘afl’ database:

  1. $ createdb afl

To create the test database (used for API tests):

  1. $ createdb afl_test

Flask

To initialise and/or update the database:

  1. $ flask db init
  2. $ flask db migrate
  3. $ flask db upgrade

app

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:

  1. $ export FLASK_APP=app
  2. $ export FLASK_ENV=development

Start the server:

  1. $ flask run

Response should be something like:

  1. $flask run
  2. * Serving Flask app "app" (lazy loading)
  3. * Environment: development
  4. * Debug mode: on
  5. * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
  6. * Restarting with stat
  7. * Debugger is active!

The API

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.

API Endpoints

Assuming the development server is running, the URL for the API is http://127.0.0.1:5000/api/v1

Create User

Creates a new user in the database

URL

/user/create

Method

POST

Params

{
“username”: \,
“password”: \
}

Success Response

Status Code: 201

Message: {“message”: “user \ has been created successfully.”}

User Login

Log in user and obtain access token

URL

/user/login

Method

POST

Params

{
“username”: \,
“password”: \
}

Success Response

Status Code: 200

Message: {“access_token”: \}


Teams

Get all teams

URL

/teams

Method

GET

Headers

Content-Type: application/json

Authorization: Bearer \

Params

None

Success Response

Status Code: 200

Message: {“Count: , “Teams”: <[{Teams}]>}

Example:

  1. {
  2. "Count: 1,
  3. "Teams": [
  4. {"id": 1,
  5. "location": "Melbourne",
  6. "name": "Essendon",
  7. "premierships": 12,
  8. "wooden_spoons": 7,
  9. "years_in_afl": 104
  10. }]
  11. }

Team by ID

Get team by ID

URL

/team/\

Method

GET

Headers

Content-Type: application/json,

Authorization: Bearer <\access_token>

Params

id: \

Success Response

Status Code: 200

Message: {“message”: “success”, “team”: <{Team}>}


Get team by ID

URL

/team/search

Method

GET / POST

Headers

Content-Type: application/json,

Authorization: Bearer <\access_token>

Params

id : \

name: \

location: \

premierships: \

wooden_spoons: \

years_in_afl: \

Success Response

Status Code: 200

Message: {“count”: \, “teams”: <{Team}>}


Players

Get all players

URL

/players

Method

GET

Headers

Content-Type: application/json

Authorization: Bearer \

Params

None

Success Response

Status Code: 200

Message: {“Count: , “Players”: <[{Players}]>}

Example:

  1. {
  2. "count": 3,
  3. "players": [
  4. {
  5. "career_goals": 518,
  6. "dob": "1978-05-14",
  7. "first_name": "Brent",
  8. "id": 1,
  9. "last_name": "Harvey",
  10. "matches_played": 432,
  11. "team_id": 2
  12. },
  13. {
  14. "career_goals": 518,
  15. "dob": "1991-06-26",
  16. "first_name": "Dustin",
  17. "id": 2,
  18. "last_name": "Martin",
  19. "matches_played": 232,
  20. "team_id": 3
  21. },
  22. {
  23. "career_goals": 97,
  24. "dob": "1992-05-14",
  25. "first_name": "Dyson",
  26. "id": 3,
  27. "last_name": "Heppell",
  28. "matches_played": 169,
  29. "team_id": 1
  30. }
  31. ]
  32. }

Player by ID

Get player by ID

URL

/player/\

Method

GET

Headers

Content-Type: application/json,

Authorization: Bearer <\access_token>

Params

id: \

Success Response

Status Code: 200

Message: {“message”: “success”, “player”: <{Player}>}


Get player by ID

URL

/player/search

Method

GET / POST

Headers

Content-Type: application/json,

Authorization: Bearer <\access_token>

Params

id : \

first_name: \

last_name: \

dob: \

matches_playes: \

career_goals: \

team_id: \

Success Response

Status Code: 200

Message: {“count”: \, “Players”: <{Player}>}