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+ USE_CLIENT_DEFAULT ,
6+ AsyncClient ,
7+ AsyncHTTPTransport ,
8+ HTTPError ,
9+ HTTPStatusError ,
10+ )
11+ from httpx ._client import UseClientDefault
12+ from httpx ._types import AuthTypes as HttpxAuthTypes
13+
14+ from mpt_api_client .exceptions import MPTError , transform_http_status_exception
15+ from mpt_api_client .http .types import (
716 AuthTypes ,
8- CookieTypes ,
17+ ContentType ,
918 HeaderTypes ,
10- QueryParamTypes ,
11- RequestContent ,
19+ QueryParam ,
1220 RequestData ,
13- RequestExtensions ,
1421 RequestFiles ,
15- TimeoutTypes ,
22+ Response ,
1623)
1724
18- from mpt_api_client .exceptions import MPTError , transform_http_status_exception
19-
2025
21- class AsyncHTTPClient ( AsyncClient ) :
26+ class AsyncHTTPClient :
2227 """Async HTTP client for interacting with SoftwareOne Marketplace Platform API."""
2328
2429 def __init__ (
@@ -49,33 +54,55 @@ def __init__(
4954 "Authorization" : f"Bearer { api_token } " ,
5055 "Accept" : "application/json" ,
5156 }
52- super (). __init__ (
57+ self . httpx_client = AsyncClient (
5358 base_url = base_url ,
5459 headers = base_headers ,
5560 timeout = timeout ,
5661 transport = AsyncHTTPTransport (retries = retries ),
5762 )
5863
59- @override
6064 async def request ( # noqa: WPS211
6165 self ,
6266 method : str ,
63- url : URL | str ,
67+ url : str ,
6468 * ,
65- content : RequestContent | None = None , # noqa: WPS110
69+ content : ContentType | None = None , # noqa: WPS110
6670 data : RequestData | None = None , # noqa: WPS110
6771 files : RequestFiles | None = None ,
6872 json : Any | None = None ,
69- params : QueryParamTypes | None = None , # noqa: WPS110
73+ params : QueryParam | None = None , # noqa: WPS110
7074 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 ,
75+ auth : AuthTypes | bool | None = None ,
7676 ) -> Response :
77+ """Perform an HTTP request.
78+
79+ Args:
80+ method: HTTP method.
81+ url: URL to send the request to.
82+ content: Request content.
83+ data: Request data.
84+ files: Request files.
85+ json: Request JSON data.
86+ params: Query parameters.
87+ headers: Request headers.
88+ auth: Authentication.
89+
90+ Returns:
91+ Response object.
92+
93+ Raises:
94+ MPTError: If the request fails.
95+ MPTApiError: If the response contains an error.
96+ MPTHttpError: If the response contains an HTTP error.
97+ """
98+ httpx_auth : HttpxAuthTypes | UseClientDefault | None = auth # type: ignore[assignment]
99+ if auth is None :
100+ httpx_auth = USE_CLIENT_DEFAULT
101+ elif auth is False :
102+ httpx_auth = None
103+
77104 try :
78- response = await super () .request (
105+ response = await self . httpx_client .request (
79106 method ,
80107 url ,
81108 content = content ,
@@ -84,8 +111,7 @@ async def request( # noqa: WPS211
84111 json = json ,
85112 params = params ,
86113 headers = headers ,
87- cookies = cookies ,
88- auth = auth ,
114+ auth = httpx_auth ,
89115 )
90116 except HTTPError as err :
91117 raise MPTError (f"HTTP Error: { err } " ) from err
@@ -94,4 +120,12 @@ async def request( # noqa: WPS211
94120 response .raise_for_status ()
95121 except HTTPStatusError as http_status_exception :
96122 raise transform_http_status_exception (http_status_exception ) from http_status_exception
97- return response
123+ return Response (
124+ headers = dict (response .headers ),
125+ status_code = response .status_code ,
126+ content = response .content ,
127+ )
128+
129+ async def close (self ) -> None :
130+ """Close transport and proxies."""
131+ await self .httpx_client .aclose ()
0 commit comments