Built custom API using Python and Flask. A RESTful web service.
In your terminal, use this command to download all files
git clone https://github.com/10manjunath/PlanetAssetsAPI
We use Anaconda development environment to run this application. If you do not have it installed on your system, kindly visit https://conda.io/docs/install/full.html. It has complete details for Windows, MacOS and Ubuntu. After installing, follow the below instructions to setup the environment and downloading dependency packages.
conda env create -f environment.yml
source activate assetsAPI
Make sure you can see the environment name assetsAPI
on the left of your prompt.
In this application, we use Python version 3.6.
In this application, we use Flask version 0.12. Flask is a microframework for Python.
pytest is a mature full-featured Python testing tool that helps you write better programs.
We use curl, a command line tool and library for transferring data with URLs.
We also use Postman, A powerful GUI platform to make API development faster & easier, from building API requests through testing, documentation and sharing.
We have 2 main files in this project. planet.py
and test_planet.py
. We start Flask server by running planet.py
file. It will initialize the asset store with an empty list. We consider this as our database.
Open a new terminal window and set the source to assetsAPI
and then run the planet.py
file
source activate assetsAPI
python planet.py
Server is running at http://127.0.0.1:5000/
Planet_API_URL = "http://127.0.0.1:5000/"
Welcome Screen
requests.get(f'{Planet_API_URL}/',headers={'Content-type: application/json'})
All assets
requests.get(f'{Planet_API_URL}/planet',headers={'Content-type: application/json'})
Specific asset
requests.get(f'{Planet_API_URL}/planet/<asset name>',headers={'Content-type: application/json'})
Filter asset (Filter word can be satellite
, antenna
, dove
, rapideye
, dish
, yagi
)
requests.get(f'{Planet_API_URL}/planet/filter/<filter word>',headers={'Content-type: application/json'})
This format is used to POST a request. Asset details can be removed if required.
requests.post(f'{Planet_API_URL}/planet',
headers={'Content-type: application/json', 'X-User':<your username>}
json={'asset name':<asset name>,'asset type':<asset type>,'asset class':<asset class>,'asset details':<asset details>
)
To see if this setup is working lets POST 2 Assets. Execute below two commands one after the other.
curl -X POST -H 'Content-Type: application/json' -H 'X-User: admin' -d '{"asset name":"ANT_01","asset type":"antenna", "asset class":"dish","asset details":{"diameter":"23.0","radome":"True"}}' http://127.0.0.1:5000/planet
curl -X POST -H 'Content-Type: application/json' -H 'X-User: admin' -d '{"asset name":"SAT_01","asset type":"satellite", "asset class":"rapideye"}' http://127.0.0.1:5000/planet
Output: Successfully Posted 2 assets
To verify if they were posted, we call GET request using Postman extension. Open the tool, set the header to 'Content-Type': application/json
. Enter correct URL. Hit send button. You will see all the assets displayed in json format.
All request are being logged in this terminal window.
It is always good to write detailed test cases during API development. The file test_planet.py
has 24 test cases. Lets execute this file.
pytest test_planet.py
pytest -v test_planet.py
Executing test_planet.py
file in Python will give detailed insight of all the test cases and edge cases.
python test_planet.py
Thank you