Skip to content

Commit 672bcea

Browse files
RaphaelEscrigBruceWouaigne
authored andcommitted
feat: implement new api errors and use the post endpoint
1 parent 5c88d9b commit 672bcea

File tree

2 files changed

+236
-74
lines changed

2 files changed

+236
-74
lines changed

linkup/client.py

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ def search(
9494
)
9595

9696
response: httpx.Response = self._request(
97-
method="GET",
97+
method="POST",
9898
url="/search",
99-
params=params,
99+
data=params,
100100
timeout=None,
101101
)
102102
if response.status_code != 200:
@@ -207,39 +207,60 @@ async def _async_request(
207207
)
208208

209209
def _raise_linkup_error(self, response: httpx.Response) -> None:
210-
message: Any = response.json().get("message", "No message provided")
211-
message = ", ".join(message) if isinstance(message, list) else str(message)
212-
213-
if response.status_code == 400:
214-
if message == "The query did not yield any result":
215-
raise LinkupNoResultError(
216-
"The Linkup API returned a no result error (400). Try rephrasing you query.\n"
217-
f"Original error message: {message}."
210+
error_data = response.json()
211+
212+
if "error" in error_data:
213+
error = error_data["error"]
214+
code = error.get("code", "")
215+
message = error.get("message", "")
216+
details = error.get("details", [])
217+
218+
error_msg = f"{message}"
219+
if details and isinstance(details, list):
220+
for detail in details:
221+
if isinstance(detail, dict):
222+
field = detail.get("field", "")
223+
field_message = detail.get("message", "")
224+
error_msg += f" {field}: {field_message}"
225+
226+
if response.status_code == 400:
227+
if code == "SEARCH_QUERY_NO_RESULT":
228+
raise LinkupNoResultError(
229+
"The Linkup API returned a no result error (400). "
230+
"Try rephrasing you query.\n"
231+
f"Original error message: {error_msg}."
232+
)
233+
else:
234+
raise LinkupInvalidRequestError(
235+
"The Linkup API returned an invalid request error (400). Make sure the "
236+
"parameters you used are valid (correct values, types, mandatory "
237+
"parameters, etc.) and you are using the latest version of the Python "
238+
"SDK.\n"
239+
f"Original error message: {error_msg}."
240+
)
241+
elif response.status_code == 401:
242+
raise LinkupAuthenticationError(
243+
"The Linkup API returned an authentication error (401). Make sure your API "
244+
"key is valid.\n"
245+
f"Original error message: {error_msg}."
246+
)
247+
elif response.status_code == 403:
248+
raise LinkupAuthenticationError(
249+
"The Linkup API returned an authorization error (403). Make sure your API "
250+
"key is valid.\n"
251+
f"Original error message: {error_msg}."
252+
)
253+
elif response.status_code == 429:
254+
raise LinkupInsufficientCreditError(
255+
"The Linkup API returned an insufficient credit error (429). Make sure "
256+
"you haven't exhausted your credits.\n"
257+
f"Original error message: {error_msg}."
218258
)
219259
else:
220-
raise LinkupInvalidRequestError(
221-
"The Linkup API returned an invalid request error (400). Make sure the "
222-
"parameters you used are valid (correct values, types, mandatory parameters, "
223-
"etc.) and you are using the latest version of the Python SDK.\n"
224-
f"Original error message: {message}."
260+
raise LinkupUnknownError(
261+
f"The Linkup API returned an unknown error ({response.status_code}).\n"
262+
f"Original error message: ({error_msg})."
225263
)
226-
elif response.status_code == 403:
227-
raise LinkupAuthenticationError(
228-
"The Linkup API returned an authentication error (403). Make sure your API "
229-
"key is valid.\n"
230-
f"Original error message: {message}."
231-
)
232-
elif response.status_code == 429:
233-
raise LinkupInsufficientCreditError(
234-
"The Linkup API returned an insufficient credit error (429). Make sure "
235-
"you haven't exhausted your credits.\n"
236-
f"Original error message: {message}."
237-
)
238-
else:
239-
raise LinkupUnknownError(
240-
f"The Linkup API returned an unknown error ({response.status_code}).\n"
241-
f"Original error message: ({message})."
242-
)
243264

244265
def _get_search_params(
245266
self,

0 commit comments

Comments
 (0)