Metadata-Version: 2.1
Name: async-http-requests
Version: 0.0.3
Summary: Asynchronous HTTP requests
Author-email: Andrea Sergi <andrea.serg1@gmail.com>
License: MIT License
        
        Copyright (c) 2023 sclash - Andrea Sergi
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/sclash/AsyncRequests
Keywords: http,requests,api,asyncio
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE

# Async Requests

Python library to handle asynchronous http requests, built on top of the [requests](https://requests.readthedocs.io/en/latest/) library 


- [Installation](#installation)
- [Usage](#usage)

## Installation

[PyPI page](https://pypi.org/project/async-http-requests/)

```bash
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 

```python
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.

```python
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](https://requests.readthedocs.io/en/latest/)

```python
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`

```python
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`

```python 
requests.response
requests.error_response
```

Check the `test.py` script for an example.
