From 2dc31963cfab162107bf2345c9b002b438655eb3 Mon Sep 17 00:00:00 2001 From: kraju3 Date: Mon, 9 Dec 2024 11:49:24 -0600 Subject: [PATCH 1/6] Validate attachment response and throw error --- nylas/handler/http_client.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/nylas/handler/http_client.py b/nylas/handler/http_client.py index 5b1d77a..e81098e 100644 --- a/nylas/handler/http_client.py +++ b/nylas/handler/http_client.py @@ -45,6 +45,10 @@ def _validate_response(response: Response) -> dict: return json +def _validate_download_response(response:Response, stream = False) -> Union[bytes, Response, dict]: + if response.status_code < 400: + return response if stream == True else response.content + return _validate_response(response) def _build_query_params(base_url: str, query_params: dict = None) -> str: query_param_parts = [] @@ -126,9 +130,9 @@ def _execute_download_request( # If we stream an iterator for streaming the content, otherwise return the entire byte array if stream: - return response + return _validate_download_response(response,stream) - return response.content if response.content else None + return _validate_download_response(response) if response.content else None except requests.exceptions.Timeout as exc: raise NylasSdkTimeoutError(url=request["url"], timeout=timeout) from exc From 4a410f60b6a535036695b22a26772d86c540690c Mon Sep 17 00:00:00 2001 From: kraju3 <35513942+kraju3@users.noreply.github.com> Date: Mon, 9 Dec 2024 13:19:10 -0600 Subject: [PATCH 2/6] Update nylas/handler/http_client.py Co-authored-by: Aaron de Mello <314152+AaronDDM@users.noreply.github.com> --- nylas/handler/http_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nylas/handler/http_client.py b/nylas/handler/http_client.py index e81098e..0e70c0a 100644 --- a/nylas/handler/http_client.py +++ b/nylas/handler/http_client.py @@ -130,7 +130,7 @@ def _execute_download_request( # If we stream an iterator for streaming the content, otherwise return the entire byte array if stream: - return _validate_download_response(response,stream) + return response if response.ok else _validate_response(response) return _validate_download_response(response) if response.content else None except requests.exceptions.Timeout as exc: From 8496d8392f9e5c265bee9c52a24950e95f4ae6c5 Mon Sep 17 00:00:00 2001 From: kraju3 Date: Mon, 9 Dec 2024 14:14:33 -0600 Subject: [PATCH 3/6] Resolve PR comments --- nylas/handler/http_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nylas/handler/http_client.py b/nylas/handler/http_client.py index 0e70c0a..d96d281 100644 --- a/nylas/handler/http_client.py +++ b/nylas/handler/http_client.py @@ -46,8 +46,8 @@ def _validate_response(response: Response) -> dict: return json def _validate_download_response(response:Response, stream = False) -> Union[bytes, Response, dict]: - if response.status_code < 400: - return response if stream == True else response.content + if response.ok: + return response.content return _validate_response(response) def _build_query_params(base_url: str, query_params: dict = None) -> str: From c6e181657bec45a9c155cfe6278ff2463dbcfb76 Mon Sep 17 00:00:00 2001 From: kraju3 Date: Mon, 9 Dec 2024 16:00:00 -0600 Subject: [PATCH 4/6] Fix pylint warnings --- nylas/handler/http_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nylas/handler/http_client.py b/nylas/handler/http_client.py index d96d281..2d47351 100644 --- a/nylas/handler/http_client.py +++ b/nylas/handler/http_client.py @@ -45,7 +45,7 @@ def _validate_response(response: Response) -> dict: return json -def _validate_download_response(response:Response, stream = False) -> Union[bytes, Response, dict]: +def _validate_download_response(response:Response) -> Union[bytes, Response, dict]: if response.ok: return response.content return _validate_response(response) From 12ee8be7e7c243a067fa56a71a3e25cd1a24733e Mon Sep 17 00:00:00 2001 From: kraju3 Date: Mon, 16 Dec 2024 09:10:41 -0600 Subject: [PATCH 5/6] Check the status code before and remove utlity function --- nylas/handler/http_client.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/nylas/handler/http_client.py b/nylas/handler/http_client.py index 2d47351..a2a7be2 100644 --- a/nylas/handler/http_client.py +++ b/nylas/handler/http_client.py @@ -45,11 +45,6 @@ def _validate_response(response: Response) -> dict: return json -def _validate_download_response(response:Response) -> Union[bytes, Response, dict]: - if response.ok: - return response.content - return _validate_response(response) - def _build_query_params(base_url: str, query_params: dict = None) -> str: query_param_parts = [] for key, value in query_params.items(): @@ -113,7 +108,7 @@ def _execute_download_request( query_params=None, stream=False, overrides=None, - ) -> Union[bytes, Response]: + ) -> Union[bytes, Response,dict]: request = self._build_request("GET", path, headers, query_params, overrides) timeout = self.timeout @@ -128,11 +123,14 @@ def _execute_download_request( stream=stream, ) + if not response.ok: + return _validate_response(response) + # If we stream an iterator for streaming the content, otherwise return the entire byte array if stream: - return response if response.ok else _validate_response(response) + return response - return _validate_download_response(response) if response.content else None + return response.content if response.content else None except requests.exceptions.Timeout as exc: raise NylasSdkTimeoutError(url=request["url"], timeout=timeout) from exc From 09410369631c03bcc16ee1e69e406baac9da7792 Mon Sep 17 00:00:00 2001 From: kraju3 Date: Mon, 16 Dec 2024 09:17:42 -0600 Subject: [PATCH 6/6] Added changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5ecbb7..ba2461d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ nylas-python Changelog ====================== +# Unreleased +---------------- +* Fixed attachment download response handling + v6.4.0 ---------------- * Add support for from field for sending messages