Skip to content

Commit d82f0be

Browse files
committed
Refactoring p2
1 parent dec5172 commit d82f0be

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+946
-925
lines changed

src/apify_client/_apify_client.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from apify_client._client_config import ClientConfig
3+
from apify_client._config import ClientConfig
44
from apify_client._http_client import HttpClient, HttpClientAsync
55
from apify_client._resource_clients import (
66
ActorClient,
@@ -50,7 +50,7 @@
5050
WebhookDispatchCollectionClient,
5151
WebhookDispatchCollectionClientAsync,
5252
)
53-
from apify_client._statistics import Statistics
53+
from apify_client._statistics import ClientStatistics
5454

5555

5656
class ApifyClient:
@@ -88,7 +88,7 @@ def __init__(
8888
timeout_secs=timeout_secs,
8989
)
9090

91-
self.stats = Statistics()
91+
self.stats = ClientStatistics()
9292
self.http_client = HttpClient(config=self._config, stats=self.stats)
9393

9494
def _options(self) -> dict:
@@ -104,11 +104,7 @@ def actor(self, actor_id: str) -> ActorClient:
104104
Args:
105105
actor_id: ID of the Actor to be manipulated.
106106
"""
107-
return ActorClient(
108-
resource_id=actor_id,
109-
resource_path='acts',
110-
**self._options()
111-
)
107+
return ActorClient(resource_id=actor_id, resource_path='acts', **self._options())
112108

113109
def actors(self) -> ActorCollectionClient:
114110
"""Retrieve the sub-client for manipulating Actors."""
@@ -279,7 +275,7 @@ def __init__(
279275
timeout_secs=timeout_secs,
280276
)
281277

282-
self.stats = Statistics()
278+
self.stats = ClientStatistics()
283279
self.http_client = HttpClientAsync(config=self._config, stats=self.stats)
284280

285281
def _options(self) -> dict:

src/apify_client/_consts.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,27 @@
33
from __future__ import annotations
44

55
from enum import Enum
6+
from typing import Any
7+
8+
# Type aliases
9+
JsonSerializable = str | int | float | bool | None | dict[str, Any] | list[Any]
10+
"""Type for representing json-serializable values. It's close enough to the real thing supported by json.parse.
11+
It was suggested in a discussion with (and approved by) Guido van Rossum, so I'd consider it correct enough.
12+
"""
13+
14+
# Constants for wait_for_finish functionality
15+
DEFAULT_WAIT_FOR_FINISH_SEC = 999999
16+
"""Default maximum wait time for job completion (effectively infinite)."""
17+
18+
DEFAULT_WAIT_WHEN_JOB_NOT_EXIST_SEC = 3
19+
"""How long to wait for a job to exist before giving up."""
20+
21+
# Standard timeout values for API operations
22+
FAST_OPERATION_TIMEOUT_SECS = 5
23+
"""Timeout for fast, idempotent operations (e.g., GET, DELETE)."""
24+
25+
STANDARD_OPERATION_TIMEOUT_SECS = 30
26+
"""Timeout for operations that may take longer (e.g., list operations, batch operations)."""
627

728

829
class ActorJobStatus(str, Enum):

src/apify_client/_http_client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
import impit
1515

1616
from apify_client._logging import log_context, logger_name
17-
from apify_client._statistics import Statistics
17+
from apify_client._statistics import ClientStatistics
1818
from apify_client._utils import is_retryable_error, retry_with_exp_backoff, retry_with_exp_backoff_async
1919
from apify_client.errors import ApifyApiError
2020

2121
if TYPE_CHECKING:
2222
from collections.abc import Callable
2323

24-
from apify_client._client_config import ClientConfig
25-
from apify_client._utils import JsonSerializable
24+
from apify_client._config import ClientConfig
25+
from apify_client._consts import JsonSerializable
2626

2727
DEFAULT_BACKOFF_EXPONENTIAL_FACTOR = 2
2828
DEFAULT_BACKOFF_RANDOM_FACTOR = 1
@@ -33,15 +33,15 @@
3333
class _BaseHttpClient:
3434
"""Base class for HTTP clients with shared configuration and utilities."""
3535

36-
def __init__(self, config: ClientConfig, stats: Statistics | None = None) -> None:
36+
def __init__(self, config: ClientConfig, stats: ClientStatistics | None = None) -> None:
3737
"""Initialize HTTP client with configuration.
3838
3939
Args:
4040
config: Immutable client configuration.
4141
stats: Optional statistics tracker.
4242
"""
4343
self.config = config
44-
self.stats = stats or Statistics()
44+
self.stats = stats or ClientStatistics()
4545

4646
# Build headers
4747
headers = {'Accept': 'application/json, */*'}

src/apify_client/_protocols.py

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

src/apify_client/_resource_clients/_resource_client.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@ def _url(
119119
# Convert to public URL if requested
120120
if public:
121121
if not url.startswith(self._base_url):
122-
raise ValueError(
123-
f'Cannot convert to public URL: {url} does not start with base URL {self._base_url}'
124-
)
122+
raise ValueError(f'Cannot convert to public URL: {url} does not start with base URL {self._base_url}')
125123
url = url.replace(self._base_url, self._public_base_url, 1)
126124

127125
# Append query parameters if provided
@@ -153,9 +151,6 @@ def _build_params(self, **kwargs: Any) -> dict:
153151
merged = {**self._default_params, **kwargs}
154152
return {k: v for k, v in merged.items() if v is not None}
155153

156-
# Backwards compatibility alias
157-
_params = _build_params
158-
159154
def _nested_client_config(self, **kwargs: Any) -> NestedClientConfig:
160155
"""Build configuration dict for initializing a nested resource client.
161156
@@ -180,10 +175,14 @@ def _nested_client_config(self, **kwargs: Any) -> NestedClientConfig:
180175
}
181176
return {**options, **kwargs}
182177

183-
# Backwards compatibility alias
184-
_sub_resource_init_options = _nested_client_config
185-
186-
def _create_sibling_client(self, client_class: type, *, resource_id: str, resource_path: str | None = None, **kwargs: Any) -> Any:
178+
def _create_sibling_client(
179+
self,
180+
client_class: type,
181+
*,
182+
resource_id: str,
183+
resource_path: str | None = None,
184+
**kwargs: Any,
185+
) -> Any:
187186
"""Create a sibling resource client (e.g., RunClient from ActorClient).
188187
189188
This is used when a resource client needs to create another resource client
@@ -310,9 +309,7 @@ def _url(
310309
# Convert to public URL if requested
311310
if public:
312311
if not url.startswith(self._base_url):
313-
raise ValueError(
314-
f'Cannot convert to public URL: {url} does not start with base URL {self._base_url}'
315-
)
312+
raise ValueError(f'Cannot convert to public URL: {url} does not start with base URL {self._base_url}')
316313
url = url.replace(self._base_url, self._public_base_url, 1)
317314

318315
# Append query parameters if provided
@@ -344,9 +341,6 @@ def _build_params(self, **kwargs: Any) -> dict:
344341
merged = {**self._default_params, **kwargs}
345342
return {k: v for k, v in merged.items() if v is not None}
346343

347-
# Backwards compatibility alias
348-
_params = _build_params
349-
350344
def _nested_client_config(self, **kwargs: Any) -> NestedClientConfig:
351345
"""Build configuration dict for initializing a nested resource client.
352346
@@ -371,10 +365,14 @@ def _nested_client_config(self, **kwargs: Any) -> NestedClientConfig:
371365
}
372366
return {**options, **kwargs}
373367

374-
# Backwards compatibility alias
375-
_sub_resource_init_options = _nested_client_config
376-
377-
def _create_sibling_client(self, client_class: type, *, resource_id: str, resource_path: str | None = None, **kwargs: Any) -> Any:
368+
def _create_sibling_client(
369+
self,
370+
client_class: type,
371+
*,
372+
resource_id: str,
373+
resource_path: str | None = None,
374+
**kwargs: Any,
375+
) -> Any:
378376
"""Create a sibling resource client (e.g., RunClientAsync from ActorClientAsync).
379377
380378
This is used when a resource client needs to create another resource client

0 commit comments

Comments
 (0)