Async HTTPX facade
AsyncHttpxClient
Facade for HTTPX library. Async version. Made for escaping 429.
Source code in aminofixfix/lib/facades/async_httpx.py
class AsyncHttpxClient:
"""
Facade for HTTPX library. Async version. Made for escaping 429.
"""
def __init__(
self,
headers: dict,
base_url: str,
proxies: dict = {},
timeout: TimeoutConfig | int = 30,
http2_enabled: bool = False,
**kwargs,
):
"""
Init of HTTPX Async Client.
"""
self.__proxies = proxies
self.__timeout = timeout
self.__base_url = base_url
self.__main_headers = headers
self.__http2_enabled = http2_enabled
self.__client: HTTPXASYNCCLIENT = HTTPXASYNCCLIENT(
base_url=self.__base_url,
proxies=self.__proxies,
timeout=self.__timeout,
headers=self.__main_headers,
http2=self.__http2_enabled,
follow_redirects=True,
)
async def request(
self,
method: str,
path: str,
headers: dict = {},
data: str | bytes | dict | None = None,
) -> Response:
"""
Request.
Args:
- method: str (GET, POST, DELETE, PUT)
- url: str
- headers: dict = {}
- etc. just for not breaking stuff
Returns:
- object `httpx.Response`
"""
while True:
r = await self.__client.request(
method=method,
url=path,
headers=self.__main_headers | headers,
data=data,
)
if r.status_code != 429:
return r
async def get(self, path: str, headers: dict = {}, **kwargs) -> Response:
"""
Get request.
Args:
- path: str
- headers: dict = {}
- etc. just for not breaking stuff
Returns:
- object `httpx.Response`
"""
return await self.request("GET", path, headers)
async def post(
self,
path: str,
headers: dict = {},
data: str | dict | bytes | None = None,
**kwargs,
) -> Response:
"""
Post request.
Args:
- path: str
- headers: dict = {}
- data: str | dict | bytes | None = None (it will autodetect if its dict and send it as json but not just data)
- etc. just for not breaking stuff
Returns:
- object `requests.Response`
"""
return await self.request("POST", path, headers, data)
async def delete(
self,
path: str,
headers: dict = {},
data: str | dict | bytes | None = None,
**kwargs,
) -> Response:
"""
Delete request.
Args:
- path: str
- headers: dict = {}
- data: str | dict | bytes | None = None (it will autodetect if its dict and send it as json but not just data)
- etc. just for not breaking stuff
Returns:
- object `httpx.Response`
"""
return await self.request("DELETE", path, headers, data)
__init__(headers, base_url, proxies={}, timeout=30, http2_enabled=False, **kwargs)
Init of HTTPX Async Client.
Source code in aminofixfix/lib/facades/async_httpx.py
def __init__(
self,
headers: dict,
base_url: str,
proxies: dict = {},
timeout: TimeoutConfig | int = 30,
http2_enabled: bool = False,
**kwargs,
):
"""
Init of HTTPX Async Client.
"""
self.__proxies = proxies
self.__timeout = timeout
self.__base_url = base_url
self.__main_headers = headers
self.__http2_enabled = http2_enabled
self.__client: HTTPXASYNCCLIENT = HTTPXASYNCCLIENT(
base_url=self.__base_url,
proxies=self.__proxies,
timeout=self.__timeout,
headers=self.__main_headers,
http2=self.__http2_enabled,
follow_redirects=True,
)
delete(path, headers={}, data=None, **kwargs)
async
Delete request.
Args: - path: str - headers: dict = {} - data: str | dict | bytes | None = None (it will autodetect if its dict and send it as json but not just data) - etc. just for not breaking stuff
Returns:
- object httpx.Response
Source code in aminofixfix/lib/facades/async_httpx.py
async def delete(
self,
path: str,
headers: dict = {},
data: str | dict | bytes | None = None,
**kwargs,
) -> Response:
"""
Delete request.
Args:
- path: str
- headers: dict = {}
- data: str | dict | bytes | None = None (it will autodetect if its dict and send it as json but not just data)
- etc. just for not breaking stuff
Returns:
- object `httpx.Response`
"""
return await self.request("DELETE", path, headers, data)
get(path, headers={}, **kwargs)
async
Get request.
Args: - path: str - headers: dict = {} - etc. just for not breaking stuff
Returns:
- object httpx.Response
Source code in aminofixfix/lib/facades/async_httpx.py
async def get(self, path: str, headers: dict = {}, **kwargs) -> Response:
"""
Get request.
Args:
- path: str
- headers: dict = {}
- etc. just for not breaking stuff
Returns:
- object `httpx.Response`
"""
return await self.request("GET", path, headers)
post(path, headers={}, data=None, **kwargs)
async
Post request.
Args: - path: str - headers: dict = {} - data: str | dict | bytes | None = None (it will autodetect if its dict and send it as json but not just data) - etc. just for not breaking stuff
Returns:
- object requests.Response
Source code in aminofixfix/lib/facades/async_httpx.py
async def post(
self,
path: str,
headers: dict = {},
data: str | dict | bytes | None = None,
**kwargs,
) -> Response:
"""
Post request.
Args:
- path: str
- headers: dict = {}
- data: str | dict | bytes | None = None (it will autodetect if its dict and send it as json but not just data)
- etc. just for not breaking stuff
Returns:
- object `requests.Response`
"""
return await self.request("POST", path, headers, data)
request(method, path, headers={}, data=None)
async
Request.
Args: - method: str (GET, POST, DELETE, PUT) - url: str - headers: dict = {} - etc. just for not breaking stuff
Returns:
- object httpx.Response
Source code in aminofixfix/lib/facades/async_httpx.py
async def request(
self,
method: str,
path: str,
headers: dict = {},
data: str | bytes | dict | None = None,
) -> Response:
"""
Request.
Args:
- method: str (GET, POST, DELETE, PUT)
- url: str
- headers: dict = {}
- etc. just for not breaking stuff
Returns:
- object `httpx.Response`
"""
while True:
r = await self.__client.request(
method=method,
url=path,
headers=self.__main_headers | headers,
data=data,
)
if r.status_code != 429:
return r