Skip to content

Commit

Permalink
Fixed 500 and 401 errors reporting missing API Key (#66)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
clam004 and Carson Lam authored Dec 8, 2023
1 parent 87a05d2 commit ed939bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api"

[tool.poetry]
name = "together"
version = "0.2.9"
version = "0.2.10"
authors = [
"Together AI <[email protected]>"
]
Expand Down
32 changes: 14 additions & 18 deletions src/together/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit ed939bd

Please sign in to comment.