Skip to main content

A Python Wrapper for httpx that combines the httpx.AsyncClient with the httpx.Client to allow both async and sync requests

Project description

aiohttpx

A Python Wrapper for httpx that combines the httpx.AsyncClient with the httpx.Client to allow both async and sync requests

Latest Version: PyPI version


Installation

# Install from pypi
pip install --upgrade aiohttpx

# Install from Github
pip install --upgrade git+https://github.com/trisongz/aiohttpx

Usage

aiohttpx is a wrapper around httpx that provides a unified async + sync interface for making HTTP requests. This is useful for making HTTP requests in both async and sync codebases.

Additionally, it includes a ProxyClient that can be used for scraping / high volume requests that would otherwise be blocked by the target server by using a rotating proxy pool through AWS API Gateway.

import asyncio

import aiohttpx

async def test_requests():
    # Notice it utilizes async context manager but can use sync methods.

    async with aiohttpx.Client() as client:
        # Make an Async GET request
        response = await client.async_get("https://httpbin.org/get")
        print(response.json())

        # Make a Sync GET request
        response = client.get("https://httpbin.org/get")
        print(response.json())
    
    # The same applies with the sync context manager
    with aiohttpx.Client() as client:
        # Make an Async GET request
        response = await client.async_get("https://httpbin.org/get")
        print(response.json())

        # Make a Sync GET request
        response = client.get("https://httpbin.org/get")
        print(response.json())


async def test_proxies():
    # Here we will test the ProxyClient
    # some magic/notes:
    
    # there is a wrapper for BeautifulSoup that is enabled for GET 
    # requests. This can be triggered by passing `soup_enabled=True` 
    # to the request method.
    
    # the ProxyClient will automatically terminate the api gateways upon 
    # exit from the context manager in both sync and async.

    # however if no context manager is used, then the ProxyClient will 
    # need to be manually terminated by calling 
    # `client.shutdown()` | `await client.async_shutdown()`

    # You can choose to perserve the api gateways by passing 
    # `reuse_gateways=True` to the ProxyClient constructor. 
    # This is useful if you want to reuse the same api gateways 
    # for multiple requests.

    # You can also increase the number of gateways per region to 
    # increase the number of concurrent requests. This can be done by 
    # passing `gateways_per_region=10` to the ProxyClient constructor.

    # by default the ProxyClient will use the `us-east-1` region. 
    # You can change this by passing `regions=["us-west-2"]` or 
    # `regions="us"` for all us regions to the ProxyClient constructor.


    base_url = "https://www.google.com"

    async with aiohttpx.ProxyClient(base_url = base_url) as client:
        # Make an Async GET request
        response = await client.async_get(
            "/search", 
            params = {"q": "httpx"},
            soup_enabled = True
        )
        print(response.soup)
        print(response.soup.title.text)

        # Make a Sync GET request
        response = client.get(
            "/search", 
            params = {"q": "httpx"},
            soup_enabled = True
        )
        print(response.soup)
        print(response.soup.title.text)
    
    # Upon exiting the context manager, the api gateways will be terminated.


async def run_tests():
    await asyncio.gather(
        test_requests(),
        test_proxies()
    )

asyncio.run(run_tests())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttpx-0.0.1.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiohttpx-0.0.1-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file aiohttpx-0.0.1.tar.gz.

File metadata

  • Download URL: aiohttpx-0.0.1.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for aiohttpx-0.0.1.tar.gz
Algorithm Hash digest
SHA256 b793e32d8253cdf81c722d299a80bb79d0f6c5ddca1f0f599887a1f365bbc1a3
MD5 c9d546056cb9d9938bf8fffd658c3c30
BLAKE2b-256 229934f30c39f4298a656810d43902a2d7b185398dea3bed1ffcf1b764708d1c

See more details on using hashes here.

File details

Details for the file aiohttpx-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: aiohttpx-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for aiohttpx-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f88bbb71344ccabcaac2132ac0426fd5f26a1990adfb3a1d4de0b198eb384420
MD5 08260468beeb86c2361af1defa24f6c4
BLAKE2b-256 b75ed54dc62e16f5d5a084cc7535c20d002289ed1a5e322d91c5c54a0f1120e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page