Asynchronous and Synchronous Resource Fetching with httpx

  • Share this:

Code introduction


This function uses the httpx library to asynchronously or synchronously fetch a resource from a given URL. It accepts a URL and an optional headers dictionary as parameters and returns the content of the fetched resource.


Technology Stack : httpx, asyncio

Code Type : Function

Code Difficulty : Intermediate


                
                    
import httpx
import random

def fetch_random_resource(url, headers=None):
    """
    Fetch a random resource from the given URL using httpx library.
    """
    async def fetch_resource():
        async with httpx.AsyncClient() as client:
            response = await client.get(url, headers=headers)
            return response.text

    # Randomly choose to use sync or async method
    if random.choice([True, False]):
        return httpx.get(url, headers=headers).text
    else:
        loop = httpx.get_event_loop()
        return loop.run_until_complete(fetch_resource())                
              
Tags: