Skip to content

Commit 909d916

Browse files
authored
fix: append /v2 to api_url only when it is missing (#1141)
`ApifyClient(api_url='https://api.apify.com/v2')` sent every request to `/v2/v2/...`. Both clients now append the `/v2` segment to `api_url` and `api_public_url` only when the URL path doesn't already end with it, the same as the JS client. Closes: #1138 *✍️ Drafted by Claude Code*
1 parent 40c7b30 commit 909d916

3 files changed

Lines changed: 78 additions & 13 deletions

File tree

‎src/apify_client/_apify_client.py‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from apify_client._client_registry import ClientRegistry, ClientRegistryAsync
77
from apify_client._consts import (
8-
API_VERSION,
98
DEFAULT_API_URL,
109
DEFAULT_MAX_RETRIES,
1110
DEFAULT_MIN_DELAY_BETWEEN_RETRIES,
@@ -72,7 +71,7 @@
7271
WebhookDispatchCollectionClientAsync,
7372
)
7473
from apify_client._statistics import ClientStatistics
75-
from apify_client._utils.http import check_custom_headers
74+
from apify_client._utils.http import check_custom_headers, to_api_base_url
7675
from apify_client.http_clients import HttpClient, HttpClientAsync, ImpitHttpClient, ImpitHttpClientAsync
7776
from apify_client.http_compressors._resolve import resolve_compressor
7877

@@ -138,9 +137,10 @@ def __init__(
138137
[Integrations](https://console.apify.com/account/integrations) page in the Apify Console.
139138
api_url: The URL of the Apify API server to connect to. Defaults to https://api.apify.com. It can
140139
be an internal URL that is not globally accessible, in which case `api_public_url` should be set
141-
as well.
140+
as well. The `/v2` API version segment is appended unless the URL path already ends with it.
142141
api_public_url: The globally accessible URL of the Apify API server. Should be set only if `api_url`
143-
is an internal URL that is not globally accessible. Defaults to https://api.apify.com.
142+
is an internal URL that is not globally accessible. Defaults to https://api.apify.com. The `/v2`
143+
API version segment is appended unless the URL path already ends with it.
144144
max_retries: How many times to retry a failed request at most. Set to 0 to disable retries.
145145
min_delay_between_retries: How long will the client wait between retrying requests
146146
(increases exponentially from this value).
@@ -171,10 +171,10 @@ def __init__(
171171
self._token = token
172172
"""Apify API token for authentication."""
173173

174-
self._base_url = f'{api_url.rstrip("/")}/{API_VERSION}'
174+
self._base_url = to_api_base_url(api_url)
175175
"""Base URL of the Apify API."""
176176

177-
self._public_base_url = f'{api_public_url.rstrip("/")}/{API_VERSION}'
177+
self._public_base_url = to_api_base_url(api_public_url)
178178
"""Public base URL for CDN access."""
179179

180180
self._statistics = ClientStatistics()
@@ -261,8 +261,10 @@ def send_request(self, *, method, url, headers, content, timeout, stream) -> Htt
261261
Args:
262262
token: The Apify API token. It is set as the `Authorization` header on the custom client,
263263
unless the client already has one configured.
264-
api_url: The URL of the Apify API server to connect to. Defaults to https://api.apify.com.
264+
api_url: The URL of the Apify API server to connect to. Defaults to https://api.apify.com. The `/v2`
265+
API version segment is appended unless the URL path already ends with it.
265266
api_public_url: The globally accessible URL of the Apify API server. Defaults to https://api.apify.com.
267+
The `/v2` API version segment is appended unless the URL path already ends with it.
266268
http_client: A custom HTTP client instance extending `HttpClient`.
267269
"""
268270
instance = cls(token=token, api_url=api_url, api_public_url=api_public_url)
@@ -514,9 +516,10 @@ def __init__(
514516
[Integrations](https://console.apify.com/account/integrations) page in the Apify Console.
515517
api_url: The URL of the Apify API server to connect to. Defaults to https://api.apify.com. It can
516518
be an internal URL that is not globally accessible, in which case `api_public_url` should be set
517-
as well.
519+
as well. The `/v2` API version segment is appended unless the URL path already ends with it.
518520
api_public_url: The globally accessible URL of the Apify API server. Should be set only if `api_url`
519-
is an internal URL that is not globally accessible. Defaults to https://api.apify.com.
521+
is an internal URL that is not globally accessible. Defaults to https://api.apify.com. The `/v2`
522+
API version segment is appended unless the URL path already ends with it.
520523
max_retries: How many times to retry a failed request at most. Set to 0 to disable retries.
521524
min_delay_between_retries: How long will the client wait between retrying requests
522525
(increases exponentially from this value).
@@ -547,10 +550,10 @@ def __init__(
547550
self._token = token
548551
"""Apify API token for authentication."""
549552

550-
self._base_url = f'{api_url.rstrip("/")}/{API_VERSION}'
553+
self._base_url = to_api_base_url(api_url)
551554
"""Base URL of the Apify API."""
552555

553-
self._public_base_url = f'{api_public_url.rstrip("/")}/{API_VERSION}'
556+
self._public_base_url = to_api_base_url(api_public_url)
554557
"""Public base URL for CDN access."""
555558

556559
self._statistics = ClientStatistics()
@@ -637,8 +640,10 @@ async def send_request(self, *, method, url, headers, content, timeout, stream)
637640
Args:
638641
token: The Apify API token. It is set as the `Authorization` header on the custom client,
639642
unless the client already has one configured.
640-
api_url: The URL of the Apify API server to connect to. Defaults to https://api.apify.com.
643+
api_url: The URL of the Apify API server to connect to. Defaults to https://api.apify.com. The `/v2`
644+
API version segment is appended unless the URL path already ends with it.
641645
api_public_url: The globally accessible URL of the Apify API server. Defaults to https://api.apify.com.
646+
The `/v2` API version segment is appended unless the URL path already ends with it.
642647
http_client: A custom HTTP client instance extending `HttpClientAsync`.
643648
"""
644649
instance = cls(token=token, api_url=api_url, api_public_url=api_public_url)

‎src/apify_client/_utils/http.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import warnings
44
from typing import TYPE_CHECKING, Any
5-
from urllib.parse import quote
5+
from urllib.parse import quote, urlsplit
66

77
from apify_client._consts import (
88
ALREADY_COMPRESSED_MEDIA_TYPE_PREFIXES,
99
ALREADY_COMPRESSED_MEDIA_TYPES,
10+
API_VERSION,
1011
COMPRESSIBLE_MEDIA_TYPE_SUFFIXES,
1112
COMPRESSIBLE_MEDIA_TYPES,
1213
OVERRIDABLE_DEFAULT_HEADERS,
@@ -29,6 +30,25 @@ def to_safe_id(id: str) -> str:
2930
return id.replace('/', '~')
3031

3132

33+
def to_api_base_url(url: str) -> str:
34+
"""Turn a user-supplied API server URL into the versioned base URL that request paths are appended to.
35+
36+
The API version segment is appended only when the URL path does not already end with it, so both
37+
`https://api.apify.com` and `https://api.apify.com/v2` resolve to `https://api.apify.com/v2`. Trailing slashes are
38+
dropped.
39+
40+
Args:
41+
url: The URL of the Apify API server, with or without the API version segment.
42+
43+
Returns:
44+
The URL ending with the API version segment and no trailing slash.
45+
"""
46+
base_url = url.rstrip('/')
47+
if urlsplit(base_url).path.endswith(f'/{API_VERSION}'):
48+
return base_url
49+
return f'{base_url}/{API_VERSION}'
50+
51+
3252
def to_path_segment(value: str) -> str:
3353
"""Percent-encode a caller-supplied value so it stays a single URL path segment.
3454

‎tests/unit/test_url_generation.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,43 @@ async def test_kvs_record_public_url_async(api_url: str, api_public_url: str | N
238238
f'{(api_public_url or DEFAULT_API_URL).strip("/")}/v2/key-value-stores/{MOCKED_KVS_ID}/'
239239
f'records/{key}{expected_signature}'
240240
)
241+
242+
243+
parametrized_versioned_api_urls = pytest.mark.parametrize(
244+
('api_url', 'expected_base_url'),
245+
[
246+
pytest.param('https://api.apify.com', 'https://api.apify.com/v2', id='no version'),
247+
pytest.param('https://api.apify.com/', 'https://api.apify.com/v2', id='no version, trailing slash'),
248+
pytest.param('https://api.apify.com/v2', 'https://api.apify.com/v2', id='with version'),
249+
pytest.param('https://api.apify.com/v2/', 'https://api.apify.com/v2', id='with version, trailing slash'),
250+
pytest.param('http://10.0.88.214:8010/v2', 'http://10.0.88.214:8010/v2', id='internal url with version'),
251+
pytest.param('https://proxy.example.com/apify/v2', 'https://proxy.example.com/apify/v2', id='custom path'),
252+
pytest.param('https://v2.example.com', 'https://v2.example.com/v2', id='version only in host'),
253+
],
254+
)
255+
256+
257+
@parametrized_versioned_api_urls
258+
def test_api_url_version_appended_once_sync(api_url: str, expected_base_url: str) -> None:
259+
"""The `/v2` segment is appended to `api_url` and `api_public_url` only when missing, with sync client."""
260+
client = ApifyClient(token='dummy-token', api_url=api_url, api_public_url=api_url)
261+
kvs = client.key_value_store(MOCKED_KVS_ID)
262+
263+
with mock.patch.object(client._http_client, 'call', return_value=_get_mocked_kvs_response()) as call_mock:
264+
public_url = kvs.get_record_public_url(key='some_key')
265+
266+
assert call_mock.call_args.kwargs['url'] == f'{expected_base_url}/key-value-stores/{MOCKED_KVS_ID}'
267+
assert public_url == f'{expected_base_url}/key-value-stores/{MOCKED_KVS_ID}/records/some_key'
268+
269+
270+
@parametrized_versioned_api_urls
271+
async def test_api_url_version_appended_once_async(api_url: str, expected_base_url: str) -> None:
272+
"""The `/v2` segment is appended to `api_url` and `api_public_url` only when missing, with async client."""
273+
client = ApifyClientAsync(token='dummy-token', api_url=api_url, api_public_url=api_url)
274+
kvs = client.key_value_store(MOCKED_KVS_ID)
275+
276+
with mock.patch.object(client._http_client, 'call', return_value=_get_mocked_kvs_response()) as call_mock:
277+
public_url = await kvs.get_record_public_url(key='some_key')
278+
279+
assert call_mock.call_args.kwargs['url'] == f'{expected_base_url}/key-value-stores/{MOCKED_KVS_ID}'
280+
assert public_url == f'{expected_base_url}/key-value-stores/{MOCKED_KVS_ID}/records/some_key'

0 commit comments

Comments
 (0)