Connecting to APIs with Python: Synchronous and Asynchronous Approaches

Mangabo Kolawole - Jul 28 - - Dev Community

Working with APIs is a common task for developers, and Python provides a great environment with the right tools to make your code as simple and comprehensible as possible. As Python evolves, you can perform the same tasks in two different ways: synchronously and asynchronously. In this article, we will discuss connecting to web APIs with Python using both approaches, with examples from the public Dog API.

If you are interested in more content covering topics like this, subscribe to my newsletter for regular updates on software programming, architecture, and tech-related insights.

Synchronous Approach: Using requests

The Python ecosystem includes a well-matured and simple package requests, which is an elegant and extensible HTTP library.

Example Program

Here's a basic example of making a GET request using requests and handling the response:

import requests

def fetch_sync(url):
    response = requests.get(url)
    if response.status_code == 200:
        data = response.json()
        print("Sync Response Data:", data)
    else:
        print(f"Sync Error: {response.status_code}")

url = "https://dogapi.dog/api/v2/breeds"
fetch_sync(url)
Enter fullscreen mode Exit fullscreen mode

In the example above, we:

  • Define thefetch_sync function: This function makes a synchronous GET request to the provided URL.

  • Send a GET request: To the given URL using requests.get.

  • Check the response status: If the status is 200, it processes and prints the JSON data; otherwise, it prints an error message.

  • Callfetch_sync with a URL: Specifically, the URL for the Dog API to fetch information about dog breeds.

In this example, the entire program waits for the request to complete. This can lead to inefficiencies, especially when making multiple requests, and is not suitable for high-concurrency scenarios where simultaneous requests are needed.

Real-World Application

The synchronous approach with requests is suitable for simple scripts or applications where you don't need to handle many I/O-bound operations concurrently. For instance, a quick script to fetch data from an API and process it on your local machine would work perfectly with this method.

Asynchronous Approach: Using aiohttp

One of the main advantages of asynchronous programming is the ability to run multiple I/O-bound operations concurrently. aiohttp is a Python library that facilitates making asynchronous HTTP requests, leveraging Python's asyncio module to enable non-blocking network operations. This can significantly improve performance in applications that need to perform multiple I/O-bound tasks, such as interacting with APIs.

Example Program

Let's implement this with an example:

import aiohttp
import asyncio

async def fetch_async(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            if response.status == 200:
                data = await response.json()
                print("Async Response Data:", data)
            else:
                print(f"Async Error: {response.status}")

url = "https://dogapi.dog/api/v2/breeds"
asyncio.run(fetch_async(url))
Enter fullscreen mode Exit fullscreen mode

In the example above, we:

  • Define an asynchronous functionfetch_async: This function fetches data from a given URL.

  • Create a client session: Using async with aiohttp.ClientSession() to create an asynchronous client session.

  • Make an asynchronous GET request: Using async with session.get(url) to send a GET request and handle the response asynchronously.

  • Handle the response: Check if the response status code is 200. If so, it parses the JSON data asynchronously and prints it. If not, it prints an error message with the status code.

  • Set the URL and run the function: Use asyncio.run(fetch_async(url)) to execute the asynchronous function.

Real-World Application

The asynchronous approach with aiohttp is ideal for applications that need to handle many concurrent I/O-bound operations. For example, a web scraper fetching data from multiple sources simultaneously or a web application making numerous API calls to provide real-time updates would benefit significantly from this method.

Choosing Between requests and aiohttp

When deciding whether to use requests or aiohttp, consider the following questions:

  • Does your program need to handle many I/O-bound operations concurrently?

    • If yes, useaiohttp.
    • If no, userequests.
  • Is your application a real-time or high-concurrency application?

    • If yes, useaiohttp.
    • If no, userequests.
  • Do you need to perform other tasks while waiting for HTTP requests to complete (non-blocking operations)?

    • If yes, useaiohttp.
    • If no, userequests.
  • Is your project simple, with only a few HTTP requests, and you prefer straightforward, easy-to-maintain code?

    • If yes, userequests.
    • If no, useaiohttp.

Conclusion

In this article, we explored using requests and aiohttp for making API requests in Python. requests offers simplicity and ease of use, making it suitable for straightforward tasks. On the other hand, aiohttp provides high performance for applications requiring concurrency but requires a good understanding of asynchronous programming.

Feel free to ask questions or provide feedback in the comments section below. Happy coding, and good luck with your projects!

If you enjoyed this article and want to stay updated with more content, subscribe to my newsletter. I send out a weekly or bi-weekly digest of articles, tips, and exclusive content that you won't want to miss 🚀

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player