Consuming API Using Flask and Containerization with Docker

Am sure you've heard about API and wondered what is this everyone is talking about. Say no more, by the end you'll have a high level understanding about APIs and be able to consume the in your back-end application.
Let's get started then!

What is an API ?

Now that we got that out of the way we can go to the real stuff.

Getting Started

We are going to build an anime streaming website with a subscription option.
We are going to consume 2 APIs

Beyonic API

  • Is used for payments thus we'll be helpful while implementing our subscription section

Ani API

  • We'll consume this API in order to retrieve the animes to display

In order to follow along. I'd recommend using this repository

GitHub logo SamoraMachel / AnimeTV

This is a flask application used for browsing, searching and watching animes. The application is built on top of AniAPI and Beyonic API

AnimeTV

Cover Image

This is a flask application used for browsing, searching and watching animes. The application is built on top of AniAPI and Beyonic API



Prequisites

First we set up our work area. We are going to use a virtual environment to avoid system and application dependencies collision
We first download the virtualenv library using

pip install virtualenv

we can then instantiate our environment using

python -m virtualenv AnimeTv

we are going to use the following libraries which will aid in the construction of our application and also consumption of our the APIs

  • Flask
  • beyonic
  • requests
  • python dotenv

this can be installed using

pip install flask beyonic requests python-dotenv

Consuming API

Since our main focus is how to work with API, you can get the project template in the repository above

We are going to use the concept of abstraction. Well divide our application into 4 parts

  • view - this will be responsible for rendering our templates
  • utils - this will be responsible for consumption of APIs and then abstracting it into functions
  • main - this will be responsible running the application
  • config - this will be responsible for configuration changes on the application

We can then create each file respectively like so:

src\
  view.py
  main.py
  utils.py
  config.py

first we configure our application :
flask configuration

we can then use the configured application instance to instantiate our application in the main.py like so:
Flask instantiation

Now we create the utility function which will be used to produced data which can be rendered by our views

First we have to create a .env file so that we can place our api token and base url there. Therefore it should look something like this:
flask env file

In our utils.py we can get the environment variables and use them.
getting environment variables

We are going to use the request library to consume the APIs as we pass in the API tokens in the header section. We first create a header dictionary for holding all the metadata for the header
API tokens

The requests library has various methods but we are majorly going to focus on the get method.
which can be used like so

requests.get(URL, headers=URL_HEADERS)

since at times you might have network problem while fetching data or something might just happen during fetching, if not checked, this might break your code. Therefore it is advisable to always place this request in a try and catch block in order to prevent the code from breaking.
To reduce the number of request constantly made to the server we'll have a global object holding the data fetched.
Consuming API

Our functions thus returns the data fetched

Since the beyonic API have been abstracted by the beyonic library we can then use the classes and functions created.
To create a new payment we use the Payment class and call the method create on it passing in the data captured from the user.
Beyonic API

We can also get the currencies and transaction from the library like so:
Beyonic

Since we have abstracted all the functionalities we require we can then get back to our view.py and use the functions.

First we need to display all the animes within the page. We first get all the animes and genres using the functions from the utils package and then pass it to our template like so:
flask template

When someone makes a subscription we are required to make a new payment thus the data will be posted to our application and be processed one one of the function in the utils library like so:
beyonic payment

once we are done creating our views, we then import then to our main.py otherwise the application will not find routes to go to:
flask

Dockerize our application

we can then dockerize our application using the following docker file :
Docker

You can then build and run your application using

docker image build . -t animetv
docker run animetv

Hooray!!! You now know how to consume API

To learn more about docker and flask use the following

19