Best Flask open-source libraries and packages

PlanetAssetsAPI

Built custom API using Python and Flask. A RESTful web service.
Updated 5 years ago

Manjunath Babu

PlanetAssetsAPI

Built custom API using Python and Flask. A RESTful web service.

Downloading files

In your terminal, use this command to download all files

git clone https://github.com/10manjunath/PlanetAssetsAPI

Development Environment

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


conda env create -f environment.yml
source activate assetsAPI

Make sure you can see the environment name assetsAPI on the left of your prompt.

conda source activate

Python

In this application, we use Python version 3.6.


Flask

In this application, we use Flask version 0.12. Flask is a microframework for Python.


pytest

pytest is a mature full-featured Python testing tool that helps you write better programs.


CURL

We use curl, a command line tool and library for transferring data with URLs.


Postman

We also use Postman, A powerful GUI platform to make API development faster & easier, from building API requests through testing, documentation and sharing.


Overview

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.

Starting Flask server

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/

start server

URL Formats

Planet_API_URL = "http://127.0.0.1:5000/"

  • GET Request

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'})
  • POST Request

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>
                    )

Lets get started by posting using CURL

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

curl 2 assets

Output: Successfully Posted 2 assets

Verify using Postman extension

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.

postman get

View Server Activity

All request are being logged in this terminal window.

verify server activity

Testing

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

test cases

Lets run the same test in verbose mode. It gives individual status of every testcase.


pytest -v test_planet.py

verbose test cases

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