Skip to main content

Asynchronous HTTP requests

Project description

Async Requests

Python library to handle asynchronous http requests, built on top of the requests library

Installation

PyPI page

pip install async-http-requests

Usage

The library provide support for asynchronous http request, using the consumer-producer pattern.

Instantiate the class AsyncHTTP specifying a list of RequestObject, (you can specify N_PRODUCERS and N_CONSUMERS: default values are 50 for both)

The RequestObject supports all keyword arguments of the requests methods (headers,params,data, ...). It allows you to specify different keyword arguments across different requests

from AysncRequests import AsyncHTPP, RequestObject, RequestType,

# Public API endpoint, it retrieves all public APIs listed under params specification
# Refer to this if you want to know more https://api.publicapis.org/
api = 'https://api.publicapis.org/entries'

# default N_PRODUCERS and N_CONSUMERS to 50 

endpoints = [
    RequestObject(url = api, params = {"title":"cat"}),
    RequestObject(url = api, params = {"title":"dog"})
]

requests = AsyncHTTP(
    url = endpoints
) 


# specify number of producers and consumers

requests = AsyncHTTP(
    url = endpoints,
    N_PRODUCERS = 10,
    N_CONSUMERS = 10
)

To get the responses you can either call the generic async_request method explicitly specifying the http request type by passing to the request_type argument a RequestType Enum (GET, POST, PUT, PATCH, DELETE, HEAD), or you can use the async_get, async_post, async_delete,async_put, async_patch, async_head method without having to spepcify the request type.

requests.async_request(
    request_type=RequestType.GET,
)

requests.async_get()

All methods support additional fixed keyword arguments such as headers, auth etc. as per the usual requests module, in case you need certain arguments to stay fixed across requests

For the additional paramaters refer to the requests module documentation Requests docs

headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'} # Keyword arguments FIXED for all requests

# Specify the RequestType

requests.async_request(
    request_type=RequestType.GET,
    haeders = headers 
)

# Using async_get

requests.async_get(headers = headers)

All methods support the use callback functions, to be used by the consumers on the request.Response object generated by the producers, as they become available in the asyncio.Queue

def example_callaback(response: request.Response):
    return response.status_code 


requests.async_request(
    request_type=RequestType.GET,
    callback = example_callback,
    headers = headers
)

requests.async_get(
            headers = headers 
            callback = example_callback
)

The results will be stored in a list object, where you'll find either the requests.Response objects, or the output of the callback function. Requests that return an error code will be saved in requests.error_response

requests.response
requests.error_response

Check the test.py script for an example.

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

async-http-requests-0.0.2.tar.gz (7.2 kB view hashes)

Uploaded Source

Built Distribution

async_http_requests-0.0.2-py3-none-any.whl (7.0 kB view hashes)

Uploaded Python 3

Supported by

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