11from __future__ import annotations
22
3- from apify_client ._http_client import HTTPClient , HTTPClientAsync
3+ from apify_client ._client_config import ClientConfig
4+ from apify_client ._http_client import HttpClient , HttpClientAsync
45from apify_client ._resource_clients import (
56 ActorClient ,
67 ActorClientAsync ,
4950 WebhookDispatchCollectionClient ,
5051 WebhookDispatchCollectionClientAsync ,
5152)
52- from apify_client ._types import Statistics
53+ from apify_client ._statistics import Statistics
5354
54- DEFAULT_API_URL = 'https://api.apify.com'
55- DEFAULT_TIMEOUT = 360
56- API_VERSION = 'v2'
5755
58-
59- class _BaseApifyClient :
60- http_client : HTTPClient | HTTPClientAsync
61-
62- def __init__ (
63- self ,
64- token : str | None = None ,
65- * ,
66- api_url : str | None = None ,
67- api_public_url : str | None = None ,
68- max_retries : int | None = 8 ,
69- min_delay_between_retries_millis : int | None = 500 ,
70- timeout_secs : int | None = DEFAULT_TIMEOUT ,
71- ) -> None :
72- """Initialize a new instance.
73-
74- Args:
75- token: The Apify API token.
76- api_url: The URL of the Apify API server to which to connect. Defaults to https://api.apify.com. It can
77- be an internal URL that is not globally accessible, in such case `api_public_url` should be set as well.
78- api_public_url: The globally accessible URL of the Apify API server. It should be set only if the `api_url`
79- is an internal URL that is not globally accessible.
80- max_retries: How many times to retry a failed request at most.
81- min_delay_between_retries_millis: How long will the client wait between retrying requests
82- (increases exponentially from this value).
83- timeout_secs: The socket timeout of the HTTP requests sent to the Apify API.
84- """
85- self .token = token
86- api_url = (api_url or DEFAULT_API_URL ).rstrip ('/' )
87- self .base_url = f'{ api_url } /{ API_VERSION } '
88- api_public_url = (api_public_url or DEFAULT_API_URL ).rstrip ('/' )
89- self .public_base_url = f'{ api_public_url } /{ API_VERSION } '
90- self .max_retries = max_retries or 8
91- self .min_delay_between_retries_millis = min_delay_between_retries_millis or 500
92- self .timeout_secs = timeout_secs or DEFAULT_TIMEOUT
93-
94- def _options (self ) -> dict :
95- return {
96- 'root_client' : self ,
97- 'base_url' : self .base_url ,
98- 'http_client' : self .http_client ,
99- }
100-
101-
102- class ApifyClient (_BaseApifyClient ):
56+ class ApifyClient :
10357 """The Apify API client."""
10458
105- http_client : HTTPClient
106-
10759 def __init__ (
10860 self ,
10961 token : str | None = None ,
@@ -112,7 +64,7 @@ def __init__(
11264 api_public_url : str | None = None ,
11365 max_retries : int | None = 8 ,
11466 min_delay_between_retries_millis : int | None = 500 ,
115- timeout_secs : int | None = DEFAULT_TIMEOUT ,
67+ timeout_secs : int | None = 360 ,
11668 ) -> None :
11769 """Initialize a new instance.
11870
@@ -127,8 +79,8 @@ def __init__(
12779 (increases exponentially from this value).
12880 timeout_secs: The socket timeout of the HTTP requests sent to the Apify API.
12981 """
130- super (). __init__ (
131- token ,
82+ self . _config = ClientConfig . from_user_params (
83+ token = token ,
13284 api_url = api_url ,
13385 api_public_url = api_public_url ,
13486 max_retries = max_retries ,
@@ -137,21 +89,26 @@ def __init__(
13789 )
13890
13991 self .stats = Statistics ()
140- self .http_client = HTTPClient (
141- token = token ,
142- max_retries = self .max_retries ,
143- min_delay_between_retries_millis = self .min_delay_between_retries_millis ,
144- timeout_secs = self .timeout_secs ,
145- stats = self .stats ,
146- )
92+ self .http_client = HttpClient (config = self ._config , stats = self .stats )
93+
94+ def _options (self ) -> dict :
95+ return {
96+ 'base_url' : self ._config .base_url ,
97+ 'public_base_url' : self ._config .public_base_url ,
98+ 'http_client' : self .http_client ,
99+ }
147100
148101 def actor (self , actor_id : str ) -> ActorClient :
149102 """Retrieve the sub-client for manipulating a single Actor.
150103
151104 Args:
152105 actor_id: ID of the Actor to be manipulated.
153106 """
154- return ActorClient (resource_id = actor_id , ** self ._options ())
107+ return ActorClient (
108+ resource_id = actor_id ,
109+ resource_path = 'acts' ,
110+ ** self ._options ()
111+ )
155112
156113 def actors (self ) -> ActorCollectionClient :
157114 """Retrieve the sub-client for manipulating Actors."""
@@ -287,11 +244,9 @@ def store(self) -> StoreCollectionClient:
287244 return StoreCollectionClient (** self ._options ())
288245
289246
290- class ApifyClientAsync ( _BaseApifyClient ) :
247+ class ApifyClientAsync :
291248 """The asynchronous version of the Apify API client."""
292249
293- http_client : HTTPClientAsync
294-
295250 def __init__ (
296251 self ,
297252 token : str | None = None ,
@@ -300,7 +255,7 @@ def __init__(
300255 api_public_url : str | None = None ,
301256 max_retries : int | None = 8 ,
302257 min_delay_between_retries_millis : int | None = 500 ,
303- timeout_secs : int | None = DEFAULT_TIMEOUT ,
258+ timeout_secs : int | None = 360 ,
304259 ) -> None :
305260 """Initialize a new instance.
306261
@@ -315,8 +270,8 @@ def __init__(
315270 (increases exponentially from this value).
316271 timeout_secs: The socket timeout of the HTTP requests sent to the Apify API.
317272 """
318- super (). __init__ (
319- token ,
273+ self . _config = ClientConfig . from_user_params (
274+ token = token ,
320275 api_url = api_url ,
321276 api_public_url = api_public_url ,
322277 max_retries = max_retries ,
@@ -325,13 +280,14 @@ def __init__(
325280 )
326281
327282 self .stats = Statistics ()
328- self .http_client = HTTPClientAsync (
329- token = token ,
330- max_retries = self .max_retries ,
331- min_delay_between_retries_millis = self .min_delay_between_retries_millis ,
332- timeout_secs = self .timeout_secs ,
333- stats = self .stats ,
334- )
283+ self .http_client = HttpClientAsync (config = self ._config , stats = self .stats )
284+
285+ def _options (self ) -> dict :
286+ return {
287+ 'base_url' : self ._config .base_url ,
288+ 'public_base_url' : self ._config .public_base_url ,
289+ 'http_client' : self .http_client ,
290+ }
335291
336292 def actor (self , actor_id : str ) -> ActorClientAsync :
337293 """Retrieve the sub-client for manipulating a single Actor.
0 commit comments