Http
This module contains async implementation of HTTP step.
koheesio.asyncio.http.AsyncHttpGetStep #
Represents an asynchronous HTTP GET step.
This class inherits from the AsyncHttpStep class and specifies the HTTP method as GET.
Attributes: method (HttpMethod): The HTTP method for the step, set to HttpMethod.GET.
koheesio.asyncio.http.AsyncHttpStep #
Asynchronous HTTP step for making HTTP requests using aiohttp.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client_session |
Optional[ClientSession]
|
Aiohttp ClientSession. |
required |
url |
List[URL]
|
List of yarl.URL. |
required |
retry_options |
Optional[RetryOptionsBase]
|
Retry options for the request. |
required |
connector |
Optional[BaseConnector]
|
Connector for the aiohttp request. |
required |
headers |
Optional[Dict[str, Union[str, SecretStr]]]
|
Request headers. |
required |
Output
responses_urls : Optional[List[Tuple[Dict[str, Any], yarl.URL]]] List of responses from the API and request URL.
Examples:
>>> import asyncio
>>> from aiohttp import ClientSession
>>> from aiohttp.connector import TCPConnector
>>> from aiohttp_retry import ExponentialRetry
>>> from koheesio.steps.async.http import AsyncHttpStep
>>> from yarl import URL
>>> from typing import Dict, Any, Union, List, Tuple
>>>
>>> # Initialize the AsyncHttpStep
>>> async def main():
>>> session = ClientSession()
>>> urls = [URL('https://example.com/api/1'), URL('https://example.com/api/2')]
>>> retry_options = ExponentialRetry()
>>> connector = TCPConnector(limit=10)
>>> headers = {'Content-Type': 'application/json'}
>>> step = AsyncHttpStep(
>>> client_session=session,
>>> url=urls,
>>> retry_options=retry_options,
>>> connector=connector,
>>> headers=headers
>>> )
>>>
>>> # Execute the step
>>> responses_urls= await step.get()
>>>
>>> return responses_urls
>>>
>>> # Run the main function
>>> responses_urls = asyncio.run(main())
client_session
class-attribute
instance-attribute
#
client_session: Optional[ClientSession] = Field(
default=None,
description="Aiohttp ClientSession",
exclude=True,
)
connector
class-attribute
instance-attribute
#
connector: Optional[BaseConnector] = Field(
default=None,
description="Connector for the aiohttp request",
exclude=True,
)
headers
class-attribute
instance-attribute
#
headers: Dict[str, Union[str, SecretStr]] = Field(
default_factory=dict,
description="Request headers",
alias="header",
exclude=True,
)
method
class-attribute
instance-attribute
#
method: Union[str, HttpMethod] = Field(
default=GET,
description="What type of Http call to perform. One of 'get', 'post', 'put', 'delete'. Defaults to 'get'.",
)
retry_options
class-attribute
instance-attribute
#
retry_options: Optional[RetryOptionsBase] = Field(
default=None,
description="Retry options for the request",
exclude=True,
)
timeout
class-attribute
instance-attribute
#
url
class-attribute
instance-attribute
#
url: List[URL] = Field(
default=None,
alias="urls",
description="Expecting list, as there is no value in executing async request for one value.\n yarl.URL is preferable, because params/data can be injected into URL instance",
exclude=True,
)
Output #
delete
async
#
Make DELETE requests.
Returns:
Type | Description |
---|---|
List[Tuple[Dict[str, Any], URL]]
|
A list of response data and corresponding request URLs. |
Source code in src/koheesio/asyncio/http.py
execute #
execute() -> Output
Execute the step.
Raises:
Type | Description |
---|---|
ValueError
|
If the specified HTTP method is not implemented in AsyncHttpStep. |
Source code in src/koheesio/asyncio/http.py
get
async
#
Make GET requests.
Returns:
Type | Description |
---|---|
List[Tuple[Dict[str, Any], URL]]
|
A list of response data and corresponding request URLs. |
Source code in src/koheesio/asyncio/http.py
get_headers #
Get the request headers.
Returns:
Type | Description |
---|---|
Optional[Dict[str, Union[str, SecretStr]]]
|
The request headers. |
Source code in src/koheesio/asyncio/http.py
get_options #
post
async
#
Make POST requests.
Returns:
Type | Description |
---|---|
List[Tuple[Dict[str, Any], URL]]
|
A list of response data and corresponding request URLs. |
Source code in src/koheesio/asyncio/http.py
put
async
#
Make PUT requests.
Returns:
Type | Description |
---|---|
List[Tuple[Dict[str, Any], URL]]
|
A list of response data and corresponding request URLs. |
Source code in src/koheesio/asyncio/http.py
request
async
#
request(
method: HttpMethod, url: URL, **kwargs
) -> Tuple[Dict[str, Any], URL]
Make an HTTP request.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method |
HttpMethod
|
The HTTP method to use for the request. |
required |
url |
URL
|
The URL to make the request to. |
required |
kwargs |
Any
|
Additional keyword arguments to pass to the request. |
{}
|
Returns:
Type | Description |
---|---|
Tuple[Dict[str, Any], URL]
|
A tuple containing the response data and the request URL. |
Source code in src/koheesio/asyncio/http.py
set_outputs #
Set the outputs of the step.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response |
Any
|
The response data. |
required |
validate_timeout #
Validate the 'data' field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Any
|
The value of the 'timeout' field. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If 'data' is not allowed in AsyncHttpStep. |