2828from urllib .parse import quote
2929
3030import certifi
31- import httpx2 as httpx
31+ import httpx2
3232import msgspec
3333import structlog
3434from opentelemetry import trace
@@ -190,22 +190,22 @@ def getuser() -> str:
190190]
191191
192192
193- def get_json_error (response : httpx .Response ):
193+ def get_json_error (response : httpx2 .Response ):
194194 """Raise a ServerResponseError if we can extract error info from the error."""
195195 err = ServerResponseError .from_response (response )
196196 if err :
197197 raise err
198198
199199
200- def raise_on_4xx_5xx (response : httpx .Response ):
200+ def raise_on_4xx_5xx (response : httpx2 .Response ):
201201 return get_json_error (response ) or response .raise_for_status ()
202202
203203
204204# Py 3.11+ version
205- def raise_on_4xx_5xx_with_note (response : httpx .Response ):
205+ def raise_on_4xx_5xx_with_note (response : httpx2 .Response ):
206206 try :
207207 return get_json_error (response ) or response .raise_for_status ()
208- except httpx .HTTPStatusError as e :
208+ except httpx2 .HTTPStatusError as e :
209209 if TYPE_CHECKING :
210210 assert hasattr (e , "add_note" )
211211 e .add_note (
@@ -225,11 +225,11 @@ def raise_on_4xx_5xx_with_note(response: httpx.Response):
225225 raise_on_4xx_5xx = raise_on_4xx_5xx_with_note
226226
227227
228- def add_correlation_id (request : httpx .Request ):
228+ def add_correlation_id (request : httpx2 .Request ):
229229 request .headers ["correlation-id" ] = str (uuid7 ())
230230
231231
232- def inject_trace_context (request : httpx .Request ) -> None :
232+ def inject_trace_context (request : httpx2 .Request ) -> None :
233233 _trace_propagator .inject (request .headers )
234234
235235
@@ -1097,25 +1097,25 @@ def update_state(
10971097 self .client .patch (f"connection-tests/{ id } " , content = body .model_dump_json ())
10981098
10991099
1100- class BearerAuth (httpx .Auth ):
1100+ class BearerAuth (httpx2 .Auth ):
11011101 def __init__ (self , token : str ):
11021102 self .token : str = token
11031103
1104- def auth_flow (self , request : httpx .Request ):
1104+ def auth_flow (self , request : httpx2 .Request ):
11051105 if self .token :
11061106 request .headers ["Authorization" ] = "Bearer " + self .token
11071107 yield request
11081108
11091109
11101110# This exists as an aid for debugging or local running via the `dry_run` argument to Client. It doesn't make
11111111# sense for returning connections etc.
1112- def noop_handler (request : httpx .Request ) -> httpx .Response :
1112+ def noop_handler (request : httpx2 .Request ) -> httpx2 .Response :
11131113 path = request .url .path
11141114 log .debug ("Dry-run request" , method = request .method , path = path )
11151115
11161116 if path .startswith ("/task-instances/" ) and path .endswith ("/run" ):
11171117 # Return a fake context
1118- return httpx .Response (
1118+ return httpx2 .Response (
11191119 200 ,
11201120 json = {
11211121 "dag_run" : {
@@ -1131,7 +1131,7 @@ def noop_handler(request: httpx.Request) -> httpx.Response:
11311131 "should_retry" : False ,
11321132 },
11331133 )
1134- return httpx .Response (200 , json = {"text" : "Hello, world!" })
1134+ return httpx2 .Response (200 , json = {"text" : "Hello, world!" })
11351135
11361136
11371137# Note: Given defaults make attempts after 1, 3, 7, 15 and fails after 31seconds
@@ -1148,13 +1148,13 @@ def noop_handler(request: httpx.Request) -> httpx.Response:
11481148
11491149def _should_retry_api_request (exception : BaseException ) -> bool :
11501150 """Determine if an API request should be retried based on the exception type."""
1151- if isinstance (exception , httpx .HTTPStatusError ):
1151+ if isinstance (exception , httpx2 .HTTPStatusError ):
11521152 return exception .response .status_code >= 500
11531153
1154- return isinstance (exception , httpx .RequestError )
1154+ return isinstance (exception , httpx2 .RequestError )
11551155
11561156
1157- class Client (httpx .Client ):
1157+ class Client (httpx2 .Client ):
11581158 @lru_cache ()
11591159 @staticmethod
11601160 def _get_ssl_context_cached (ca_file : str | None = None , ca_path : str | None = None ) -> ssl .SSLContext :
@@ -1182,7 +1182,7 @@ def __init__(self, *, base_url: str | None, dry_run: bool = False, token: str, *
11821182 if dry_run :
11831183 # If dry run is requested, install a no op handler so that simple tasks can "heartbeat" using a
11841184 # real client, but just don't make any HTTP requests
1185- kwargs .setdefault ("transport" , httpx .MockTransport (noop_handler ))
1185+ kwargs .setdefault ("transport" , httpx2 .MockTransport (noop_handler ))
11861186 kwargs .setdefault ("base_url" , "dry-run://server" )
11871187 else :
11881188 kwargs ["base_url" ] = base_url
@@ -1212,7 +1212,7 @@ def __init__(self, *, base_url: str | None, dry_run: bool = False, token: str, *
12121212 ** kwargs ,
12131213 )
12141214
1215- def _update_auth (self , response : httpx .Response ):
1215+ def _update_auth (self , response : httpx2 .Response ):
12161216 if new_token := response .headers .get ("Refreshed-API-Token" ):
12171217 log .debug ("Execution API issued us a refreshed Task token" )
12181218 self .auth = BearerAuth (new_token )
@@ -1225,7 +1225,7 @@ def _update_auth(self, response: httpx.Response):
12251225 reraise = True ,
12261226 )
12271227 def request (self , * args , ** kwargs ):
1228- """Implement a convenience for httpx .Client.request with a retry layer."""
1228+ """Implement a convenience for httpx2 .Client.request with a retry layer."""
12291229 # Set content type as convenience if not already set
12301230 if kwargs .get ("content" , None ) is not None and "content-type" not in (
12311231 kwargs .get ("headers" , {}) or {}
@@ -1319,8 +1319,8 @@ def __repr__(self):
13191319 return repr (self .detail )
13201320
13211321
1322- class ServerResponseError (httpx .HTTPStatusError ):
1323- def __init__ (self , message : str , * , request : httpx .Request , response : httpx .Response ):
1322+ class ServerResponseError (httpx2 .HTTPStatusError ):
1323+ def __init__ (self , message : str , * , request : httpx2 .Request , response : httpx2 .Response ):
13241324 super ().__init__ (message , request = request , response = response )
13251325
13261326 detail : list [RemoteValidationError ] | str | dict [str , Any ] | None
@@ -1330,7 +1330,7 @@ def __reduce__(self) -> tuple[Any, ...]:
13301330 return Exception .__new__ , (type (self ),) + self .args , self .__dict__
13311331
13321332 @classmethod
1333- def from_response (cls , response : httpx .Response ) -> ServerResponseError | None :
1333+ def from_response (cls , response : httpx2 .Response ) -> ServerResponseError | None :
13341334 if response .is_success :
13351335 return None
13361336 # 4xx or 5xx error?
0 commit comments