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 requests, using the consumer-producer pattern leveraging the built-in python modu.

Instantiate the class AsyncHTTP specifying a list of RequestObject, (you can specify N_PRODUCERS and N_CONSUMERS: default values are 10 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 10

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 the same header across all the requests
)

# Using async_get

requests.async_get(headers = headers)  # using the same header across all the requests

All methods support the use of 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.

In case of unsuccessfull requests the parameter max_retries can be specified (default is 0). When max retries is set larger than zero, any http request that has returned an error will be sent again up to the specified number of times if not successfull. If the given requests still generates problems after all the attempts it will be pushed to the requests.error_response and won't be found in requests.response

The max_retries paramater can be useful when you are sending a big number of requests to the same server, in which case you can get an error just momentarily even if nothing is really wrong.

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


requests.async_request(
    request_type=RequestType.GET,
    max_retries = 5,  # retry sendign the requests 5 times for all the unsuccessfull ones
    callback = example_callback,
    headers = headers
)


# no retries specified -> defaults to no further attempts in case of errors
requests.async_get(
            headers = headers,
            callback = lambda x: x.json() # lambda as 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
  • A summary of all the requests that were not successfull (if any) can be displayed summarized in a pandas.DataFrame object through the requests.error_data attribute
requests.response
requests.error_response
requests.error_data

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.5.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

async_http_requests-0.0.5-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file async-http-requests-0.0.5.tar.gz.

File metadata

  • Download URL: async-http-requests-0.0.5.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.9.6 requests/2.28.2 setuptools/59.6.0 requests-toolbelt/0.10.1 tqdm/4.65.0 CPython/3.11.0rc1

File hashes

Hashes for async-http-requests-0.0.5.tar.gz
Algorithm Hash digest
SHA256 8184d18e4290c473194b970d631267337e011a51c07e6782e7ec10a0a2b5cf12
MD5 c08f79907cb1cb9a5fb50be1c5834192
BLAKE2b-256 a0122e17023679738881efe30181938084de367d97fea95a59aa576a344111b8

See more details on using hashes here.

File details

Details for the file async_http_requests-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: async_http_requests-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.9.6 requests/2.28.2 setuptools/59.6.0 requests-toolbelt/0.10.1 tqdm/4.65.0 CPython/3.11.0rc1

File hashes

Hashes for async_http_requests-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 df561723be55980182f0d3890ebcced407f693d4fd5440ef9fbcd9aa4e5b65e0
MD5 2037a74e20509b10b56f8373b90c4df8
BLAKE2b-256 9a7f57b7a35bd99e9771585f0a0b99f09155cf7774965f86bbb5cb9c15d25486

See more details on using hashes here.

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