From ed939bd58b16de467df661f0b8bc98d1bd0062b3 Mon Sep 17 00:00:00 2001 From: Carson Lam Date: Fri, 8 Dec 2023 13:52:01 -0800 Subject: [PATCH] Fixed 500 and 401 errors reporting missing API Key (#66) * Editted 500 and 401 errors to their correct meaning before these resposne status codes were reporting that the API key was invalid or missing, these are actually server side or auth issues * Editted 500 and 401 errors to their correct meaning before these resposne status codes were reporting that the API key was invalid or missing, these are actually server side or auth issues * Removed a modified file from pull request * resolved mypy type suggestions errors * changed to version 0.2.10 --------- Co-authored-by: Carson Lam --- pyproject.toml | 2 +- src/together/utils.py | 32 ++++++++++++++------------------ 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e0def03e..8f87e323 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api" [tool.poetry] name = "together" -version = "0.2.9" +version = "0.2.10" authors = [ "Together AI " ] diff --git a/src/together/utils.py b/src/together/utils.py index bc667b13..674be032 100644 --- a/src/together/utils.py +++ b/src/together/utils.py @@ -75,6 +75,18 @@ def parse_timestamp(timestamp: str) -> datetime: raise ValueError("Timestamp does not match any expected format") +def response_status_exception(response: requests.Response) -> None: + if response.status_code == 429: + raise together.RateLimitError( + message="Too many requests received. Please pace your requests." + ) + elif response.status_code == 500: + raise Exception("server encountered an unexpected condition") + elif response.status_code == 401: + raise Exception("invalid authentication credentials") + response.raise_for_status() + + def create_post_request( url: str, headers: Optional[Dict[Any, Any]] = None, @@ -99,15 +111,7 @@ def create_post_request( except requests.exceptions.RequestException as e: raise together.ResponseError(e) - if response.status_code == 429: - raise together.RateLimitError( - message="Too many requests received. Please pace your requests." - ) - elif response.status_code == 500: - raise Exception("Invalid API key supplied.") - elif response.status_code == 401: - raise Exception("API Key not supplied") - response.raise_for_status() + response_status_exception(response) return response @@ -139,15 +143,7 @@ def create_get_request( except requests.exceptions.RequestException as e: raise together.ResponseError(e) - if response.status_code == 429: - raise together.RateLimitError( - message="Too many requests received. Please pace your requests." - ) - elif response.status_code == 500: - raise Exception("Invalid API key supplied.") - elif response.status_code == 401: - raise Exception("API Key not supplied") - response.raise_for_status() + response_status_exception(response) return response