Skip to content

Commit ed939bd

Browse files
clam004Carson Lam
andauthored
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 <[email protected]>
1 parent 87a05d2 commit ed939bd

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api"
44

55
[tool.poetry]
66
name = "together"
7-
version = "0.2.9"
7+
version = "0.2.10"
88
authors = [
99
"Together AI <[email protected]>"
1010
]

src/together/utils.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ def parse_timestamp(timestamp: str) -> datetime:
7575
raise ValueError("Timestamp does not match any expected format")
7676

7777

78+
def response_status_exception(response: requests.Response) -> None:
79+
if response.status_code == 429:
80+
raise together.RateLimitError(
81+
message="Too many requests received. Please pace your requests."
82+
)
83+
elif response.status_code == 500:
84+
raise Exception("server encountered an unexpected condition")
85+
elif response.status_code == 401:
86+
raise Exception("invalid authentication credentials")
87+
response.raise_for_status()
88+
89+
7890
def create_post_request(
7991
url: str,
8092
headers: Optional[Dict[Any, Any]] = None,
@@ -99,15 +111,7 @@ def create_post_request(
99111
except requests.exceptions.RequestException as e:
100112
raise together.ResponseError(e)
101113

102-
if response.status_code == 429:
103-
raise together.RateLimitError(
104-
message="Too many requests received. Please pace your requests."
105-
)
106-
elif response.status_code == 500:
107-
raise Exception("Invalid API key supplied.")
108-
elif response.status_code == 401:
109-
raise Exception("API Key not supplied")
110-
response.raise_for_status()
114+
response_status_exception(response)
111115

112116
return response
113117

@@ -139,15 +143,7 @@ def create_get_request(
139143
except requests.exceptions.RequestException as e:
140144
raise together.ResponseError(e)
141145

142-
if response.status_code == 429:
143-
raise together.RateLimitError(
144-
message="Too many requests received. Please pace your requests."
145-
)
146-
elif response.status_code == 500:
147-
raise Exception("Invalid API key supplied.")
148-
elif response.status_code == 401:
149-
raise Exception("API Key not supplied")
150-
response.raise_for_status()
146+
response_status_exception(response)
151147

152148
return response
153149

0 commit comments

Comments
 (0)