Skip to content

Commit b6f6a8b

Browse files
committed
Rm client config
1 parent e43e9f9 commit b6f6a8b

File tree

15 files changed

+233
-219
lines changed

15 files changed

+233
-219
lines changed

src/apify_client/_apify_client.py

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
from functools import cached_property
44

55
from apify_client._client_registry import ClientRegistry, ClientRegistryAsync
6-
from apify_client._config import ClientConfig
7-
from apify_client._http_clients import HttpClient, HttpClientAsync
6+
from apify_client._consts import (
7+
API_VERSION,
8+
DEFAULT_API_URL,
9+
DEFAULT_MAX_RETRIES,
10+
DEFAULT_MIN_DELAY_BETWEEN_RETRIES_MILLIS,
11+
DEFAULT_TIMEOUT_SECS,
12+
)
13+
from apify_client._http_clients import AsyncHttpClient, SyncHttpClient
814
from apify_client._resource_clients import (
915
ActorClient,
1016
ActorClientAsync,
@@ -71,11 +77,11 @@ def __init__(
7177
self,
7278
token: str | None = None,
7379
*,
74-
api_url: str | None = None,
75-
api_public_url: str | None = None,
76-
max_retries: int | None = 8,
77-
min_delay_between_retries_millis: int | None = 500,
78-
timeout_secs: int | None = 360,
80+
api_url: str = DEFAULT_API_URL,
81+
api_public_url: str | None = DEFAULT_API_URL,
82+
max_retries: int = DEFAULT_MAX_RETRIES,
83+
min_delay_between_retries_millis: int = DEFAULT_MIN_DELAY_BETWEEN_RETRIES_MILLIS,
84+
timeout_secs: int = DEFAULT_TIMEOUT_SECS,
7985
) -> None:
8086
"""Initialize a new instance.
8187
@@ -84,26 +90,37 @@ def __init__(
8490
api_url: The URL of the Apify API server to which to connect. Defaults to https://api.apify.com. It can
8591
be an internal URL that is not globally accessible, in such case `api_public_url` should be set as well.
8692
api_public_url: The globally accessible URL of the Apify API server. It should be set only if the `api_url`
87-
is an internal URL that is not globally accessible.
93+
is an internal URL that is not globally accessible. Defaults to https://api.apify.com.
8894
max_retries: How many times to retry a failed request at most.
8995
min_delay_between_retries_millis: How long will the client wait between retrying requests
9096
(increases exponentially from this value).
9197
timeout_secs: The socket timeout of the HTTP requests sent to the Apify API.
9298
"""
93-
self._config = ClientConfig.from_user_params(
94-
token=token,
95-
api_url=api_url,
96-
api_public_url=api_public_url,
97-
max_retries=max_retries,
98-
min_delay_between_retries_millis=min_delay_between_retries_millis,
99-
timeout_secs=timeout_secs,
100-
)
101-
"""Resolved client configuration."""
99+
# We need to do this because of mocking in tests and default mutable arguments.
100+
api_url = DEFAULT_API_URL if api_url is None else api_url
101+
api_public_url = DEFAULT_API_URL if api_public_url is None else api_public_url
102+
103+
self._token = token
104+
"""Apify API token for authentication."""
105+
106+
self._base_url = f'{api_url.rstrip("/")}/{API_VERSION}'
107+
"""Base URL of the Apify API."""
108+
109+
self._public_base_url = f'{api_public_url.rstrip("/")}/{API_VERSION}'
110+
"""Public base URL for CDN access."""
102111

103112
self._statistics = ClientStatistics()
104113
"""Collector for client request statistics."""
105114

106-
self._http_client = HttpClient(config=self._config, statistics=self._statistics)
115+
self._http_client = SyncHttpClient(
116+
token=token,
117+
timeout_secs=timeout_secs or DEFAULT_TIMEOUT_SECS,
118+
max_retries=max_retries or DEFAULT_MAX_RETRIES,
119+
min_delay_between_retries_millis=(
120+
min_delay_between_retries_millis or DEFAULT_MIN_DELAY_BETWEEN_RETRIES_MILLIS
121+
),
122+
statistics=self._statistics,
123+
)
107124
"""HTTP client used to communicate with the Apify API."""
108125

109126
self._client_registry = ClientRegistry(
@@ -130,16 +147,16 @@ def __init__(
130147
def _base_kwargs(self) -> dict:
131148
"""Base keyword arguments for resource client construction."""
132149
return {
133-
'base_url': self._config.base_url,
134-
'public_base_url': self._config.public_base_url,
150+
'base_url': self._base_url,
151+
'public_base_url': self._public_base_url,
135152
'http_client': self._http_client,
136153
'client_registry': self._client_registry,
137154
}
138155

139156
@property
140157
def token(self) -> str | None:
141158
"""The Apify API token used by the client."""
142-
return self._config.token
159+
return self._token
143160

144161
def actor(self, actor_id: str) -> ActorClient:
145162
"""Retrieve the sub-client for manipulating a single Actor.
@@ -290,11 +307,11 @@ def __init__(
290307
self,
291308
token: str | None = None,
292309
*,
293-
api_url: str | None = None,
294-
api_public_url: str | None = None,
295-
max_retries: int | None = 8,
296-
min_delay_between_retries_millis: int | None = 500,
297-
timeout_secs: int | None = 360,
310+
api_url: str = DEFAULT_API_URL,
311+
api_public_url: str | None = DEFAULT_API_URL,
312+
max_retries: int = DEFAULT_MAX_RETRIES,
313+
min_delay_between_retries_millis: int = DEFAULT_MIN_DELAY_BETWEEN_RETRIES_MILLIS,
314+
timeout_secs: int = DEFAULT_TIMEOUT_SECS,
298315
) -> None:
299316
"""Initialize a new instance.
300317
@@ -303,26 +320,37 @@ def __init__(
303320
api_url: The URL of the Apify API server to which to connect. Defaults to https://api.apify.com. It can
304321
be an internal URL that is not globally accessible, in such case `api_public_url` should be set as well.
305322
api_public_url: The globally accessible URL of the Apify API server. It should be set only if the `api_url`
306-
is an internal URL that is not globally accessible.
323+
is an internal URL that is not globally accessible. Defaults to https://api.apify.com.
307324
max_retries: How many times to retry a failed request at most.
308325
min_delay_between_retries_millis: How long will the client wait between retrying requests
309326
(increases exponentially from this value).
310327
timeout_secs: The socket timeout of the HTTP requests sent to the Apify API.
311328
"""
312-
self._config = ClientConfig.from_user_params(
313-
token=token,
314-
api_url=api_url,
315-
api_public_url=api_public_url,
316-
max_retries=max_retries,
317-
min_delay_between_retries_millis=min_delay_between_retries_millis,
318-
timeout_secs=timeout_secs,
319-
)
320-
"""Resolved client configuration."""
329+
# We need to do this because of mocking in tests and default mutable arguments.
330+
api_url = DEFAULT_API_URL if api_url is None else api_url
331+
api_public_url = DEFAULT_API_URL if api_public_url is None else api_public_url
332+
333+
self._token = token
334+
"""Apify API token for authentication."""
335+
336+
self._base_url = f'{api_url.rstrip("/")}/{API_VERSION}'
337+
"""Base URL of the Apify API."""
338+
339+
self._public_base_url = f'{api_public_url.rstrip("/")}/{API_VERSION}'
340+
"""Public base URL for CDN access."""
321341

322342
self._statistics = ClientStatistics()
323343
"""Collector for client request statistics."""
324344

325-
self._http_client = HttpClientAsync(config=self._config, statistics=self._statistics)
345+
self._http_client = AsyncHttpClient(
346+
token=token,
347+
timeout_secs=timeout_secs or DEFAULT_TIMEOUT_SECS,
348+
max_retries=max_retries or DEFAULT_MAX_RETRIES,
349+
min_delay_between_retries_millis=(
350+
min_delay_between_retries_millis or DEFAULT_MIN_DELAY_BETWEEN_RETRIES_MILLIS
351+
),
352+
statistics=self._statistics,
353+
)
326354
"""HTTP client used to communicate with the Apify API."""
327355

328356
self._client_registry = ClientRegistryAsync(
@@ -349,16 +377,16 @@ def __init__(
349377
def _base_kwargs(self) -> dict:
350378
"""Base keyword arguments for resource client construction."""
351379
return {
352-
'base_url': self._config.base_url,
353-
'public_base_url': self._config.public_base_url,
380+
'base_url': self._base_url,
381+
'public_base_url': self._public_base_url,
354382
'http_client': self._http_client,
355383
'client_registry': self._client_registry,
356384
}
357385

358386
@property
359387
def token(self) -> str | None:
360388
"""The Apify API token used by the client."""
361-
return self._config.token
389+
return self._token
362390

363391
def actor(self, actor_id: str) -> ActorClientAsync:
364392
"""Retrieve the sub-client for manipulating a single Actor.

src/apify_client/_config.py

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/apify_client/_consts.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
33
from enum import Enum
44
from typing import Any
55

6+
DEFAULT_API_URL = 'https://api.apify.com'
7+
"""Default base URL for the Apify API."""
8+
9+
API_VERSION = 'v2'
10+
"""Current Apify API version."""
11+
12+
DEFAULT_TIMEOUT_SECS = 360
13+
"""Default request timeout in seconds."""
14+
15+
DEFAULT_MAX_RETRIES = 8
16+
"""Default maximum number of retries for failed requests."""
17+
18+
DEFAULT_MIN_DELAY_BETWEEN_RETRIES_MILLIS = 500
19+
"""Default minimum delay between retries in milliseconds."""
20+
621
# Type aliases
722
JsonSerializable = str | int | float | bool | None | dict[str, Any] | list[Any]
823
"""Type for representing json-serializable values. It's close enough to the real thing supported by json.parse.

src/apify_client/_http_clients/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from ._async import HttpClientAsync
2-
from ._sync import HttpClient
1+
from ._async import AsyncHttpClient
2+
from ._sync import SyncHttpClient
33

44
__all__ = [
55
'HttpClient',

src/apify_client/_http_clients/_async.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
import impit
1010

11+
from apify_client._consts import DEFAULT_MAX_RETRIES, DEFAULT_MIN_DELAY_BETWEEN_RETRIES_MILLIS, DEFAULT_TIMEOUT_SECS
1112
from apify_client._http_clients._base import BaseHttpClient
1213
from apify_client._logging import log_context, logger_name
1314
from apify_client.errors import ApifyApiError
1415

1516
if TYPE_CHECKING:
1617
from collections.abc import Awaitable, Callable
1718

18-
from apify_client._config import ClientConfig
1919
from apify_client._consts import JsonSerializable
2020
from apify_client._statistics import ClientStatistics
2121

@@ -24,22 +24,39 @@
2424
logger = logging.getLogger(logger_name)
2525

2626

27-
class HttpClientAsync(BaseHttpClient):
27+
class AsyncHttpClient(BaseHttpClient):
2828
"""Asynchronous HTTP client for Apify API with automatic retries and exponential backoff."""
2929

30-
def __init__(self, config: ClientConfig, statistics: ClientStatistics | None = None) -> None:
30+
def __init__(
31+
self,
32+
*,
33+
token: str | None = None,
34+
timeout_secs: int = DEFAULT_TIMEOUT_SECS,
35+
max_retries: int = DEFAULT_MAX_RETRIES,
36+
min_delay_between_retries_millis: int = DEFAULT_MIN_DELAY_BETWEEN_RETRIES_MILLIS,
37+
statistics: ClientStatistics | None = None,
38+
) -> None:
3139
"""Initialize the asynchronous HTTP client.
3240
3341
Args:
34-
config: Client configuration with API URL, token, timeout, and retry settings.
42+
token: Apify API token for authentication.
43+
timeout_secs: Request timeout in seconds.
44+
max_retries: Maximum number of retries for failed requests.
45+
min_delay_between_retries_millis: Minimum delay between retries in milliseconds.
3546
statistics: Statistics tracker for API calls. Created automatically if not provided.
3647
"""
37-
super().__init__(config, statistics)
48+
super().__init__(
49+
token=token,
50+
timeout_secs=timeout_secs,
51+
max_retries=max_retries,
52+
min_delay_between_retries_millis=min_delay_between_retries_millis,
53+
statistics=statistics,
54+
)
3855

3956
self._impit_async_client = impit.AsyncClient(
4057
headers=self._headers,
4158
follow_redirects=True,
42-
timeout=self._config.timeout_secs,
59+
timeout=self._timeout_secs,
4360
)
4461

4562
async def call(
@@ -92,8 +109,8 @@ async def call(
92109
stream=stream,
93110
timeout_secs=timeout_secs,
94111
),
95-
max_retries=self._config.max_retries,
96-
backoff_base_millis=self._config.min_delay_between_retries_millis,
112+
max_retries=self._max_retries,
113+
backoff_base_millis=self._min_delay_between_retries_millis,
97114
)
98115

99116
async def _make_request(

0 commit comments

Comments
 (0)