Consuming API Using Flask and Containerization with Docker
Tags: python, webdev, docker
Published: 2021-10-01 22:09:40

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!
<hr />
### What is an API ? 
- The acronym ***API*** stands for Application Programming Interface. This allows 2 or more computers or softwares to communicate for data manipulation. It can be used to consume, update or delete data. [To find out more about API I recommend using the following link](https://www.altexsoft.com/blog/engineering/what-is-api-definition-types-specifications-documentation/).

Now that we got that out of the way we can go to the real stuff. 
<hr />
## <u> Getting Started </u>
We are going to build an anime streaming website with a subscription option.
We are going to consume 2 APIs
- [Beyonic API](https://payments.beyonic.com/users/login/?next=/)
- [Ani API](https://aniapi.com/docs/resources/anime/)

```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 SamoraMachel/AnimeTV %}


### 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 
```bash 
pip install virtualenv
```
we can then instantiate our environment using

```bash
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
```bash
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](https://github.com/SamoraMachel/AnimeTV)

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: 

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

first we configure our application :
![flask configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a12jznd3turdkfhuc3v4.png)

we can then use the configured application instance to instantiate our application in the main.py like so:
![Flask instantiation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/upjiedvr1g965r83rtze.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ifxb6dvkcfn25urth4d1.png)

In our `utils.py` we can get the environment variables and use them. 
![getting environment variables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nih4mfdaits8b1pqfnek.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f09dk9dfq8wjxwpyeiiy.png)

The requests library has various methods but we are majorly going to focus on the get method.
which can be used like so 
```python
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1onz1sdjl0fqoe8g49dd.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u8pnreg8tpd70xsahs8c.png)

We can also get the currencies and transaction from the library like so:
![Beyonic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7drl0v4cepvjq3c2t86u.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0yvh9g9dj9oshedevaw.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdbfbmv2wu1aarcnwv4u.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6s71zrukbso4f3s63mgo.png)

#### Dockerize our application
we can then dockerize our application using the following docker file :
![Docker](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d6lnrgymaz7c4847qyqh.png)

You can then build and run your application using 
```bash
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](https://dev.to/machel/getting-started-with-docker-flask-5bm7)