From 49c3d31019e0a623efd23f740bbaaf488a0755e6 Mon Sep 17 00:00:00 2001 From: dblock Date: Wed, 25 Sep 2024 16:17:05 -0400 Subject: [PATCH] Await for the response before consuming headers or other status. Signed-off-by: dblock --- opensearchpy/connection/http_async.py | 52 +++++++++++++++------------ 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/opensearchpy/connection/http_async.py b/opensearchpy/connection/http_async.py index d0490878..0287419c 100644 --- a/opensearchpy/connection/http_async.py +++ b/opensearchpy/connection/http_async.py @@ -216,6 +216,35 @@ async def perform_request( raw_data = await response.text() duration = self.loop.time() - start + # raise warnings if any from the 'Warnings' header. + warning_headers = response.headers.getall("warning", ()) + self._raise_warnings(warning_headers) + + # raise errors based on http status codes, let the client handle those if needed + if not (200 <= response.status < 300) and response.status not in ignore: + self.log_request_fail( + method, + str(url), + url_path, + orig_body, + duration, + status_code=response.status, + response=raw_data, + ) + self._raise_error(response.status, raw_data) + + self.log_request_success( + method, + str(url), + url_path, + orig_body, + response.status, + raw_data, + duration, + ) + + return response.status, response.headers, raw_data + # We want to reraise a cancellation or recursion error. except reraise_exceptions: raise @@ -236,29 +265,6 @@ async def perform_request( raise ConnectionTimeout("TIMEOUT", str(e), e) raise ConnectionError("N/A", str(e), e) - # raise warnings if any from the 'Warnings' header. - warning_headers = response.headers.getall("warning", ()) - self._raise_warnings(warning_headers) - - # raise errors based on http status codes, let the client handle those if needed - if not (200 <= response.status < 300) and response.status not in ignore: - self.log_request_fail( - method, - str(url), - url_path, - orig_body, - duration, - status_code=response.status, - response=raw_data, - ) - self._raise_error(response.status, raw_data) - - self.log_request_success( - method, str(url), url_path, orig_body, response.status, raw_data, duration - ) - - return response.status, response.headers, raw_data - async def close(self) -> Any: """ Explicitly closes connection