AnimeTV
This is a flask application used for browsing, searching and watching animes. The application is built on top of AniAPI and Beyonic API
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!
Now that we got that out of the way we can go to the real stuff.
We are going to build an anime streaming website with a subscription option.
We are going to consume 2 APIs
Beyonic API
Ani API
In order to follow along. I'd recommend using this repository
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
this can be installed using
pip install flask beyonic requests python-dotenv
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 functionsmain
- 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
we can then use the configured application instance to instantiate our application in the main.py like so:
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:
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
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.
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.
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:
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:
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:
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