Git Product home page Git Product logo

rest-fut21's Introduction

REST-FUT21

Install on Virtualenv

After cloning, a virtual environment must be created. On Linux:

$ python3 -m venv virtualenv name 
$ source env/bin/activate
(virtualenv name)$ pip3 install requirements.txt

On Windows:

$ python -m venv virtualenv name
$ venv\Scripts\activate
(virtualenv name)$ pip install requirements.txt

Running

Note

The database server can be MySQL/MariaDB. The database must be created first of all.

To run the server, you have to set the environment variables. On Windows:

$ set MYSQL_HOST=hostname (localhost)
$ set MYSQL_USER=server username
$ set MYSQL_PASSWORD=server password
$ set MYSQL_DATABASE=database name
$ set API_KEY= header x-api-key value
$ set FLASK_APP=run.py
$ set FLASK_ENV=development

On Linux:

$ export MYSQL_HOST=hostname (localhost)
$ export MYSQL_USER=server username
$ export MYSQL_PASSWORD=server password
$ export MYSQL_DATABASE=database name
$ export API_KEY= header x-api-key value
$ export FLASK_APP=run.py
$ export FLASK_ENV=development

Finally, the following instruction must be used, on Linux and Windows:

$ flask init-db 
$ flask run

Docker deployment:

A .env file is needed for the environment variables

MYSQL_HOST=db (the services name is the host, so you'll need exactly this name)
MYSQL_USER=your_username
MYSQL_PASSWORD=server_password
MYSQL_DATABASE=database_name
API_KEY=your_x_api_key
FLASK_RUN_HOST=0.0.0.0
FLASK_APP=run.py
FLASK_ENV=development

After that just do the following:

$ docker-compose build
# docker-compose up

NOTE

Once the deployment is complete, we can make use of the datagetter script datagetter to collect the data and perform the respective tests on the different endpoints of the REST API.

API Documentation

Show Players

Returns all the players that belong to a team. The right x-api-key header must be sent.

  • URL

    /api/v1/team/:team_name

  • Methods:

    GET

  • URL Parameters

    Required:

    name=[string]

  • Success Response:

    • Code: 200
      Content: {club: Icons, id: 9, name: Diego Armando Maradona, nation: Argentina, position: CAM}
  • Error Response:

    • Code: 404 NOT FOUND
      Content: {Error: Not Found}

    Or

    • Code: 401 UNAUTHORIZED
      Content: {message: Not Authorized}

Search Coincidences:

Returns the players data searched by partial or full name, returning all the coincidences.
Sort the results in a ascending or descending way. By default, the order is ascending. The right x-api-key header must be sent.

  • URL

    /api/v1/players?search=name&order=

  • Methods:

    GET

  • URL Parameters

    Required:

    name=[string o char]

    Optional:

    order=[asc o desc]

  • Success Response:

    • Code: 200
      Content: {club: FC Barcelona, id: 14, name: Luis Suárez, nation: Uruguay, position: ST}, {club: Real Madrid, id: 2, name: Luka Modric, nation: Croatia, position: CM}
  • Error Response:

    • Code: 404 NOT FOUND
      Content: {Error: Not Found}

    Or

    • Code: 401 UNAUTHORIZED
      Content: {message: Not Authorized}

Example

With a tool like curl we can make a request to the endpoint /api/v1/team/ sending the x-api-key header:

$ curl -i -H "x-api-key: 1234" -X GET http://localhost:5000/api/v1/team/Icons
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 675
Server: Werkzeug/2.0.1 Python/3.7.11
Date: Thu, 05 Aug 2021 06:16:27 GMT

[
{
    "club": "Icons",
"id": 9,
"name": "Edson Arantes Nascimento",
"nation": "Brazil",
"position": "CAM"
		  
},
{
    "club": "Icons",
"id": 23,
"name": "Diego Maradona",
"nation": "Argentina",
"position": "CAM"
		  
},
{
    "club": "Icons",
"id": 36,
"name": "Ronaldo Luis Nazário de Lima",
"nation": "Brazil",
"position": "ST"
		  
},
{
    "club": "Icons",
"id": 81,
"name": "Edson Arantes Nascimento",
"nation": "Brazil",
"position": "CAM"
		  
},
{
    "club": "Icons",
"id": 95,
"name": "Diego Maradona",
"nation": "Argentina",
"position": "CAM"
		  
  }
]

If we do the request without the x-api-key header:

$ curl -i -X GET http://localhost:5000/api/v1/team/Icons
HTTP/1.0 401 UNAUTHORIZED
Content-Type: application/json
Content-Length: 34
Server: Werkzeug/2.0.1 Python/3.7.11
Date: Thu, 05 Aug 2021 06:21:28 GMT

{
	"message": "Not Authorized"
}

If we do the request with a wrong x-api-key header:

$ curl -i -H "x-api-key: 1234i" -X GET http://localhost:5000/api/v1/team/Icons
HTTP/1.0 401 UNAUTHORIZED
Content-Type: application/json
Content-Length: 34
Server: Werkzeug/2.0.1 Python/3.7.11
Date: Thu, 05 Aug 2021 06:24:42 GMT
{
  "message": "Not Authorized"
}

The endpoint /api/v1/players works like this:

$ curl -i -H "x-api-key: 1234" -X GET http://localhost:5000/api/v1/players?search=d

[
 {
  "club": "Manchester United",
  "id": 27,
  "name": "David De Gea Quintana",
  "nation": "Spain",
  "position": "GK"
	      
},
{
 "club": "Atlético Madrid",
 "id": 40,
 "name": "Diego Godín",
 "nation": "Uruguay",
 "position": "CB"
	      
},
{
 "club": "Icons",
 "id": 23,
 "name": "Diego Maradona",
 "nation": "Argentina",
 "position": "CAM"
	      
},
{
 "club": "Icons",
 "id": 95,
 "name": "Diego Maradona",
 "nation": "Argentina",
 "position": "CAM"
	      
  }
]

The result is to return all the players whose names begin with the letter d. If an additional querystring is sent order
we can reverse the order in which the players are displayed:

$ curl -i -H "x-api-key: 1234" -X GET "http://localhost:5000/api/v1/players?search=d&order=desc"

[
 {
 "club": "Icons",
 "id": 23,
 "name": "Diego Maradona",
 "nation": "Argentina",
 "position": "CAM"
 },
 {
 "club": "Icons",
 "id": 95,
 "name": "Diego Maradona",
 "nation": "Argentina",
 "position": "CAM"
 },
 {
 "club": "Atlético Madrid",
 "id": 40,
 "name": "Diego Godín",
 "nation": "Uruguay",
 "position": "CB"
 },
 {
 "club": "Manchester United",
 "id": 27,
 "name": "David De Gea Quintana",
 "nation": "Spain",
 "position": "GK"		      
 }
]

Credits

rest-fut21's People

Contributors

dependabot[bot] avatar edmartt avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.