Skip to content

Commit fd422a5

Browse files
authored
fix: Incomplete URL logging on exception (#211)
* fix: Incomplete URL logging on exception * fix: tests
1 parent 81f4209 commit fd422a5

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

ai21/http_client/async_http_client.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@ async def execute_http_request(
100100
raise exception
101101

102102
if response.status_code != httpx.codes.OK:
103-
logger.error(
104-
f"Calling {method} {self._base_url} failed with a non-200 response code: {response.status_code}"
105-
)
103+
logger.error(f"Calling {method} {response.url} failed with a non-200 response code: {response.status_code}")
106104

107105
if stream:
108106
details = self._extract_streaming_error_details(response)

ai21/http_client/http_client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ def execute_http_request(
9999

100100
if response.status_code != httpx.codes.OK:
101101
_logger.error(
102-
f"Calling {method} {self._base_url} failed with a non-200 "
103-
f"response code: {response.status_code} headers: {response.headers}"
102+
f"Calling {method} {response.url} failed with a non-200 response code: {response.status_code}"
104103
)
105104

106105
if stream:

tests/unittests/test_ai21_env_config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from ai21 import AI21Client
55

66
_FAKE_API_KEY = "fake-key"
7-
os.environ["AI21_API_KEY"] = _FAKE_API_KEY
87

98

109
@contextmanager
@@ -15,6 +14,7 @@ def set_env_var(key: str, value: str):
1514

1615

1716
def test_env_config__when_set_via_init_and_env__should_be_taken_from_init():
17+
os.environ["AI21_API_KEY"] = _FAKE_API_KEY
1818
client = AI21Client()
1919
assert client._api_key == _FAKE_API_KEY
2020

@@ -25,6 +25,7 @@ def test_env_config__when_set_via_init_and_env__should_be_taken_from_init():
2525

2626

2727
def test_env_config__when_set_twice__should_be_updated():
28+
os.environ["AI21_API_KEY"] = _FAKE_API_KEY
2829
client = AI21Client()
2930

3031
assert client._api_key == _FAKE_API_KEY

tests/unittests/test_http_client.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import pytest
21
from unittest.mock import Mock
3-
from urllib.request import Request
42

53
import httpx
4+
import pytest
65

76
from ai21.errors import ServiceUnavailable, Unauthorized
7+
from ai21.http_client.async_http_client import AsyncAI21HTTPClient
88
from ai21.http_client.base_http_client import RETRY_ERROR_CODES
99
from ai21.http_client.http_client import AI21HTTPClient
10-
from ai21.http_client.async_http_client import AsyncAI21HTTPClient
1110

1211
_METHOD = "GET"
1312
_URL = "http://test_url"
1413
_API_KEY = "fake-key"
1514

1615

1716
def test__execute_http_request__when_retry_error_code_once__should_retry_and_succeed(mock_httpx_client: Mock) -> None:
18-
request = Request(method=_METHOD, url=_URL)
17+
request = httpx.Request(method=_METHOD, url=_URL)
1918
retries = 3
2019
mock_httpx_client.send.side_effect = [
2120
httpx.Response(status_code=429, request=request),
@@ -28,7 +27,7 @@ def test__execute_http_request__when_retry_error_code_once__should_retry_and_suc
2827

2928

3029
def test__execute_http_request__when_retry_error__should_retry_and_stop(mock_httpx_client: Mock) -> None:
31-
request = Request(method=_METHOD, url=_URL)
30+
request = httpx.Request(method=_METHOD, url=_URL)
3231
retries = len(RETRY_ERROR_CODES)
3332

3433
mock_httpx_client.send.side_effect = [
@@ -44,7 +43,7 @@ def test__execute_http_request__when_retry_error__should_retry_and_stop(mock_htt
4443

4544
def test__execute_http_request__when_streaming__should_handle_non_200_response_code(mock_httpx_client: Mock) -> None:
4645
error_details = "test_error"
47-
request = Request(method=_METHOD, url=_URL)
46+
request = httpx.Request(method=_METHOD, url=_URL)
4847
response = httpx.Response(status_code=401, request=request, text=error_details)
4948
mock_httpx_client.send.return_value = response
5049

@@ -57,7 +56,7 @@ def test__execute_http_request__when_streaming__should_handle_non_200_response_c
5756
async def test__execute_async_http_request__when_retry_error_code_once__should_retry_and_succeed(
5857
mock_httpx_async_client: Mock,
5958
) -> None:
60-
request = Request(method=_METHOD, url=_URL)
59+
request = httpx.Request(method=_METHOD, url=_URL)
6160
retries = 3
6261
mock_httpx_async_client.send.side_effect = [
6362
httpx.Response(status_code=429, request=request),
@@ -73,7 +72,7 @@ async def test__execute_async_http_request__when_retry_error_code_once__should_r
7372
async def test__execute_async_http_request__when_retry_error__should_retry_and_stop(
7473
mock_httpx_async_client: Mock,
7574
) -> None:
76-
request = Request(method=_METHOD, url=_URL)
75+
request = httpx.Request(method=_METHOD, url=_URL)
7776
retries = len(RETRY_ERROR_CODES)
7877

7978
mock_httpx_async_client.send.side_effect = [
@@ -92,7 +91,7 @@ async def test__execute_async_http_request__when_streaming__should_handle_non_20
9291
mock_httpx_async_client: Mock,
9392
) -> None:
9493
error_details = "test_error"
95-
request = Request(method=_METHOD, url=_URL)
94+
request = httpx.Request(method=_METHOD, url=_URL)
9695
response = httpx.Response(status_code=401, request=request, text=error_details)
9796
mock_httpx_async_client.send.return_value = response
9897

0 commit comments

Comments
 (0)