@@ -522,6 +522,9 @@ async def _http_middleware(
522522 raise UnsafeCallError (_ ("Call aborted due to safe mode" ))
523523
524524 response = await handler (request )
525+
526+ if "Correlation-Id" in response .headers :
527+ self ._set_correlation_id (response .headers ["Correlation-Id" ])
525528 return response
526529
527530 async def _send_request (
@@ -554,21 +557,6 @@ async def _send_request(
554557 # Somehow this does not work properly for multipart...
555558 if content_type is not None and content_type .startswith ("application/json" ):
556559 _headers ["Content-Type" ] = content_type
557- request = self ._session .prepare_request (
558- requests .Request (
559- method ,
560- url ,
561- auth = auth ,
562- params = params ,
563- headers = _headers ,
564- data = data ,
565- files = files ,
566- )
567- )
568- if content_type :
569- assert request .headers ["content-type" ].startswith (
570- content_type
571- ), f"{ request .headers ['content-type' ]} != { content_type } "
572560 try :
573561 async with aiohttp .ClientSession (
574562 headers = self ._headers , middlewares = (self ._http_middleware ,)
@@ -581,25 +569,26 @@ async def _send_request(
581569 data = data ,
582570 # files=files,
583571 ssl = self .ssl_context ,
572+ max_redirects = 0 ,
584573 ) as response :
585574 response .raise_for_status ()
586575 response_body = await response .read ()
587- except requests .TooManyRedirects as e :
588- assert e .response is not None
576+ except aiohttp .TooManyRedirects as e :
577+ # We could handle that in the middleware...
578+ assert e .history [- 1 ] is not None
589579 raise OpenAPIError (
590580 _ ("Received redirect to '{url}'. Please check your configuration." ).format (
591- url = e .response .headers ["location" ]
581+ url = e .history [ - 1 ] .headers ["location" ]
592582 )
593583 )
594- except requests . RequestException as e :
584+ except aiohttp . ClientResponseError as e :
595585 raise OpenAPIError (str (e ))
586+
596587 self ._debug_callback (1 , _ ("Response: {status_code}" ).format (status_code = response .status ))
597588 for key , value in response .headers .items ():
598589 self ._debug_callback (2 , f" { key } : { value } " )
599- if response .text :
600- self ._debug_callback (3 , f"{ response .text } " )
601- if "Correlation-Id" in response .headers :
602- self ._set_correlation_id (response .headers ["Correlation-Id" ])
590+ if response_body :
591+ self ._debug_callback (3 , f"{ response_body !r} " )
603592 if response .status == 401 :
604593 raise PulpAuthenticationFailed (method_spec ["operationId" ])
605594 if response .status == 403 :
0 commit comments