11import os
2- from typing import Any , override
2+ from typing import Any
33
4- from httpx import URL , AsyncClient , AsyncHTTPTransport , HTTPError , HTTPStatusError , Response
5- from httpx ._client import USE_CLIENT_DEFAULT , UseClientDefault # noqa: PLC2701
6- from httpx ._types import ( # noqa: WPS235
4+ from httpx import (
5+ URL ,
6+ USE_CLIENT_DEFAULT ,
7+ AsyncClient ,
8+ AsyncHTTPTransport ,
9+ HTTPError ,
10+ HTTPStatusError ,
11+ )
12+ from httpx ._client import UseClientDefault
13+ from httpx ._types import AuthTypes as HttpxAuthTypes
14+
15+ from mpt_api_client .exceptions import MPTError , transform_http_status_exception
16+ from mpt_api_client .http .types import (
717 AuthTypes ,
8- CookieTypes ,
18+ ContentType ,
919 HeaderTypes ,
10- QueryParamTypes ,
11- RequestContent ,
20+ QueryParam ,
1221 RequestData ,
13- RequestExtensions ,
1422 RequestFiles ,
15- TimeoutTypes ,
23+ Response ,
1624)
1725
18- from mpt_api_client .exceptions import MPTError , transform_http_status_exception
19-
2026
21- class AsyncHTTPClient ( AsyncClient ) :
27+ class AsyncHTTPClient :
2228 """Async HTTP client for interacting with SoftwareOne Marketplace Platform API."""
2329
2430 def __init__ (
@@ -49,33 +55,55 @@ def __init__(
4955 "Authorization" : f"Bearer { api_token } " ,
5056 "Accept" : "application/json" ,
5157 }
52- super (). __init__ (
58+ self . httpx_client = AsyncClient (
5359 base_url = base_url ,
5460 headers = base_headers ,
5561 timeout = timeout ,
5662 transport = AsyncHTTPTransport (retries = retries ),
5763 )
5864
59- @override
6065 async def request ( # noqa: WPS211
6166 self ,
6267 method : str ,
6368 url : URL | str ,
6469 * ,
65- content : RequestContent | None = None , # noqa: WPS110
70+ content : ContentType | None = None , # noqa: WPS110
6671 data : RequestData | None = None , # noqa: WPS110
6772 files : RequestFiles | None = None ,
6873 json : Any | None = None ,
69- params : QueryParamTypes | None = None , # noqa: WPS110
74+ params : QueryParam | None = None , # noqa: WPS110
7075 headers : HeaderTypes | None = None ,
71- cookies : CookieTypes | None = None ,
72- auth : AuthTypes | UseClientDefault | None = USE_CLIENT_DEFAULT ,
73- follow_redirects : bool | UseClientDefault = USE_CLIENT_DEFAULT ,
74- timeout : TimeoutTypes | UseClientDefault = USE_CLIENT_DEFAULT ,
75- extensions : RequestExtensions | None = None ,
76+ auth : AuthTypes | bool | None = None ,
7677 ) -> Response :
78+ """Perform an HTTP request.
79+
80+ Args:
81+ method: HTTP method.
82+ url: URL to send the request to.
83+ content: Request content.
84+ data: Request data.
85+ files: Request files.
86+ json: Request JSON data.
87+ params: Query parameters.
88+ headers: Request headers.
89+ auth: Authentication.
90+
91+ Returns:
92+ Response object.
93+
94+ Raises:
95+ MPTError: If the request fails.
96+ MPTApiError: If the response contains an error.
97+ MPTHttpError: If the response contains an HTTP error.
98+ """
99+ httpx_auth : HttpxAuthTypes | UseClientDefault | None = auth # type: ignore[assignment]
100+ if auth is None :
101+ httpx_auth = USE_CLIENT_DEFAULT
102+ elif auth is False :
103+ httpx_auth = None
104+
77105 try :
78- response = await super () .request (
106+ response = await self . httpx_client .request (
79107 method ,
80108 url ,
81109 content = content ,
@@ -84,8 +112,7 @@ async def request( # noqa: WPS211
84112 json = json ,
85113 params = params ,
86114 headers = headers ,
87- cookies = cookies ,
88- auth = auth ,
115+ auth = httpx_auth ,
89116 )
90117 except HTTPError as err :
91118 raise MPTError (f"HTTP Error: { err } " ) from err
@@ -94,4 +121,12 @@ async def request( # noqa: WPS211
94121 response .raise_for_status ()
95122 except HTTPStatusError as http_status_exception :
96123 raise transform_http_status_exception (http_status_exception ) from http_status_exception
97- return response
124+ return Response (
125+ headers = dict (response .headers ),
126+ status_code = response .status_code ,
127+ content = response .content ,
128+ )
129+
130+ async def close (self ) -> None :
131+ """Close transport and proxies."""
132+ await self .httpx_client .aclose ()
0 commit comments