An API's app based on Flask and Flask Rest Framework.
A REST API is an architectural style for an API that uses HTTP requests to access and use data. That data can be used to GET, PUT, POST and DELETE data types, commonly refers as CRUD operations.
This project is a REST API server that ingests metrics from its clients and
generates on-demand stats reports. This utilizes Flask, SQLAlchemy and Marshmallow. When running the application, it creates a db.sqlite file in the project's root directory which is database file containing all the data. You can delete this file when the testing completes.
- Python (Recommended 3.6+)
- Pip3
- Virtualenv
Follow these instructions to setup the project locally on your machine.
- Clone the project repository.
$ git clone https://github.com/marshmallo/rest-api-assignment.git
- Change to the project's root directory.
$ cd flask-rest-api/ - Create a virtual environment of name
.venvin project's root directory. Use python version that you are using.$ virtualenv --python=python3.6 .venv
- Activate the virtual environment.
$ source .venv/bin/activate - Install the project requirements.
$ pip3 install -r requirements.txt
- Run the application using the python version that you are using (Recommended only in Development.)
$ python3.6 app.py
- Deactivate the virtual environment.
$ deactivate
To run the app in Production use Gunicorn, a production-grade WSGI server.
- Repeat the setup instructions from [1-5].
- Run the application using gunicorn in Production.
$ gunicorn --bind 0.0.0.0:8080 wsgi:app
- Deactivate the virtual environment.
$ deactivate
Install Docker, then build the image and run the application container from the project's root directory as follows:
- Build the image.
$ docker build --tag flask-rest-api:latest . - Run the container and publish the container’s port to the host machine.
$ docker run -it -p 8080:8080 --name <any-name> flask-rest-api:latest
- Run container in background. (Optional)
$ docker run -dit -p 8080:8080 --name <any-name> flask-rest-api:latest
To test the API you can run the application locally or launch a docker container as described above.
The API exposes the following endpoints:
-
Ingestion
Method: POST Path: /metricsHeaders:
content-type: application/jsonJSON body structure:
{ "percentage_cpu_used": <integer between 0-100 >, "percentage_memory_used": <integer between 0-100 > }Sample curl requests:
$ curl \ -XPOST \ -H "Content-Type: application/json" \ --data '{"percentage_cpu_used": 55, "percentage_memory_used": 90}' \ http://127.0.0.1:8080/metrics$ curl \ -XPOST \ -H "Content-Type: application/json" \ --data '{"percentage_cpu_used": 210, "percentage_memory_used": 35}' \ http://127.0.0.1:8080/metricsResponses:
200: OK (If data ingested successfully).500: Internal Server Error (If anything wrong such as Integer value out of defined range).
-
Report Generation
Method: GET Path: /reportHeaders:
content-type: application/jsonJSON body structure:
[ { "ip": <IP address of machine>, "percentage_cpu_used": <maximum cpu %>, "percentage_memory_used": <maximum memory %> }, ... ]Sample curl request:
$ curl \ -XGET \ -H "Content-Type: application/json" \ http://127.0.0.1:8080/report [ { "ip": "172.17.0.1", "percentage_cpu_used": 55, "percentage_memory_used": 90 } ]Responses:
200: OK (Success).500: Internal Server Error (Something's Wrong).
- Avoid running the application in Development mode, rather use Gunicorn to run the application in Production.
- Use Postman, an API client to test the API's instead of curl. Use it when running the application with Gunicorn and to see the response codes.