Python Package Development: YouTube Channel API Client with RapidAPI

Python Package Development: YouTube Channel API Client with RapidAPI

API provider: dataocean see on RapidAPI


We're about to embark on an interesting journey: creating a Python 3 package to gather details from YouTube channels. I've whimsically named it 'The Better YouTube Channel Details'. Even if APIs are not usually your thing, this process might still pique your interest.

We'll break down this project into three parts for ease of digestion and to keep your attention focused.

🔹 First up: We're setting up the package and chatting with the API.
🔹 Next: We'll dive into testing our creation.
🔹 And finally: We'll release it to the world at

Join me on this adventure; I promise it'll be fun and educational!

Alright, let's start!

🧬 Project Blueprint

We're crafting this in Python 3. Remember, every package has a blueprint it needs to follow. Here's a quick overview, inspired by the official guidelines:

├── pyproject.toml
├── src/
│   └── example_package_YOUR_USERNAME_HERE/
│       ├──
│       └──
└── tests/

Looks straightforward, right?

Our API will be simple and efficient:

  1. Connect to the API.
  2. Fetch data in JSON format.
  3. Convert JSON into a usable format for other applications.

Now, let's set up our project files.

🧙 Magic in the Making

We've got our project structure ready. It's time to focus on what we love: coding! We'll get back to the metadata later.

⌨️ Coding Time!

Let's dive into programming. Our task now is to add a file named src/ This file will handle all interactions with the API. To start, you'll need a rapid_api_key. Here's how to get it:

  1. Sign up for free.
  2. Navigate to the API page and test the endpoint.
  3. Take note of the X-RapidAPI-Key displayed there.

Here's a sneak peek at our YouTubeChannelDetailsApiClient class:

class YouTubeChannelDetailsApiClient(object):
    def __init__(self, rapid_api_key: str):
        self.rapidapi_host = ''
        self.headers = {
            "X-RapidAPI-Key": rapid_api_key,
            "X-RapidAPI-Host": self.rapidapi_host,
Next, we'll add more files to our project and see it take shape:

🚀 API Communication Begins

We need a function to communicate with the API, and for that, we'll use the aiohttp package. But first, install it:

pip install aiohttp

Now, we're all set to send HTTP requests.

We'll create a private function named __get_request to handle our API requests. Here's what it needs to do:

  1. Create the URL for the API endpoint.
  2. Send a request to the API.
  3. Retry the request if there's a network issue.
  4. Handle any API errors by throwing an exception.

Let's start coding this function:

    async def __get_request(self, path: str, query: dict) -> str:
        url = URL(

        session_timeout = aiohttp.ClientTimeout(total=45.0)
        is_ok = False

        while True:
            async with aiohttp.ClientSession(headers=self.headers, timeout=session_timeout) as session:
                async with session.get(url=url) as response:
                        json_response = await response.json()  
                        if response.status == 200:  
                            is_ok = True
                    except aiohttp.client.ContentTypeError:
                    if is_ok:
            await asyncio.sleep(0.3)

        return json_response
Remember, we're using aiohttp for this. It's important to manage retries and handle potential errors.

📚 Checking Our Imports 

Do we have all the necessary imports? Let's make sure:
import asyncio
import aiohttp
from urllib.parse import urlencode
from purl import URL
And of course, ensure all packages are installed:
pip install asyncio
pip install aiohttp
pip install purl

So, we've got our code. But does it work as expected? We'll only find out by testing, which I'll cover in the next post.

I hope you're finding this journey enjoyable and informative. If you like what you're reading, feel free to show some love with a thumbs up or a comment. Your feedback keeps me going!

Thanks for joining me today. Stay tuned for the next installment! 🌟👋