This is a bare-bones example of a Flask application providing a RESTful API for a campaign resource.
The entire application is contained within the app.py
file.
Must have python3.6+ installed.
- Choose one of the following methods:
- Installing in closed environment (Recommended):
-
Install pipenv through pip.
pip install pipenv
-
Open the task folder in terminal and install dependencies.
pipenv install
-
- Installing globally:
- Open the task folder in terminal and run:
pip install -r requirements.txt
- Open the task folder in terminal and run:
- Installing in closed environment (Recommended):
If you installed globally, remove pipenv run
from the commands
pipenv run python app.py
Or
pipenv run flask run
Note: you may need to set FLASK_APP enviroment variable equal to app.py if this option doesn't work
The RESTful API to for the flask app is described below.
GET /api/campaigns
curl -i -H 'Accept: application/json' http://localhost:5000/api/campaigns
HTTP/1.0 201 CREATED
Content-Type: application/json
Content-Length: 17
Server: Werkzeug/0.15.4 Python/3.7.2
Date: Sat, 18 May 2019 16:14:57 GMT
{"campaigns":[]}
Only accepts json format. Required keys:
- name
- country
- budget
- goal
if category isn't provided, it will use the dummy category extraction service to get one
POST /api/campaigns
curl -i -H 'Content-Type: application/json' -d '{
"name": "n1",
"country": "USA",
"budget": 149,
"goal": "Awareness",
"category": "Technology"
}' http://localhost:5000/api/campaigns
HTTP/1.0 201 CREATED
Content-Type: application/json
Content-Length: 148
Server: Werkzeug/0.15.4 Python/3.7.2
Date: Sat, 18 May 2019 16:20:32 GMT
{"campaigns":[{"budget":149,"category":"Technology","country":"USA","goal":"Awareness","name":"n1","uri":"http://localhost:5000/api/campaigns/1"}]}
Through using the uri key supplied by the GET
request
PUT /api/campaigns/id
curl -H 'Content-Type: application/json' -X PUT -d '{"country":"EGY"}' http://localhost:5000/api/campaigns/1
{"campaigns":[{"budget":149,"category":"Technology","country":"EGY","goal":"Awareness","name":"n1","uri":"http://localhost:5000/api/campaigns/1"}]}
PUT /api/campaigns/id
curl -H 'Content-Type: application/json' -X PUT -d '{"country":"EGY"}' http://localhost:5000/api/campaigns/2
{"error":"Campaign Not Found"}
DELETE /api/campaigns/id
curl -X DELETE -H 'Accept: application/json' http://localhost:5000/api/campaigns/1
{"campaigns":[]}
Optional params:
-
?dimensions
The dimensions for the Bar chart- Format:
key,key
i.e: country,budget - Default:
country, category
- Format:
-
?fields
The keys that will be shown in the Campaigns table- Format:
key,key,..
i.e: country,goal,budget - Default:
all fields
- Format:
GET /api/campaigns/analysis
curl -i http://localhost:5000/api/campaigns/analysis