Asynchronous HTTP requests
Project description
Async Requests
Python library to handle asynchronous http requests, built on top of the requests library
Installation
pip install async-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
Built Distribution
Hashes for async-http-requests-0.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a93b1d3c226fabca41c52be4ac7fe153aec7dd7f2f4f6346bc6f5840453c969 |
|
MD5 | a780fb9656c0b7210fe8ef9fb386d1ea |
|
BLAKE2b-256 | a5e869961b5fae66b6070afd0b07a91f5bb2388908f02e80300848c255b332de |
Hashes for async_http_requests-0.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b70ef971b92c1d75e826def6c5072599d809bf6356102e41453403da7ae3c16 |
|
MD5 | 249046271ca0577f6aaaa0a91005b657 |
|
BLAKE2b-256 | 6d2e29010be21582360747b3e1d32dc323ee647fd557f831132c793aed67806c |