forked from apify/apify-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
209 lines (167 loc) · 7.67 KB
/
client.py
File metadata and controls
209 lines (167 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from typing import Dict, Optional
from ._http_client import _HTTPClient
from .clients import (
ActorClient,
ActorCollectionClient,
BuildClient,
BuildCollectionClient,
DatasetClient,
DatasetCollectionClient,
KeyValueStoreClient,
KeyValueStoreCollectionClient,
LogClient,
RequestQueueClient,
RequestQueueCollectionClient,
RunClient,
RunCollectionClient,
ScheduleClient,
ScheduleCollectionClient,
TaskClient,
TaskCollectionClient,
UserClient,
WebhookClient,
WebhookCollectionClient,
WebhookDispatchClient,
WebhookDispatchCollectionClient,
)
DEFAULT_API_URL = 'https://api.apify.com'
API_VERSION = 'v2'
class ApifyClient:
"""The Apify API client."""
def __init__(
self,
token: Optional[str] = None,
*,
api_url: Optional[str] = None,
max_retries: int = 8,
min_delay_between_retries_millis: int = 500,
):
"""Initialize the Apify API Client.
Args:
token (str, optional): The Apify API token
api_url (str, optional): The URL of the Apify API server to which to connect to. Defaults to https://api.apify.com
max_retries (int, optional): How many times to retry a failed request at most
min_delay_between_retries_millis (int, optional): How long will the client wait between retrying requests
(increases exponentially from this value)
"""
self.token = token
api_url = (api_url or DEFAULT_API_URL).rstrip('/')
self.base_url = f'{api_url}/{API_VERSION}'
self.max_retries = max_retries
self.min_delay_between_retries_millis = min_delay_between_retries_millis
self.http_client = _HTTPClient(
token=token,
max_retries=max_retries,
min_delay_between_retries_millis=min_delay_between_retries_millis,
)
# TODO statistics
# TODO logger
def _options(self) -> Dict:
return {
'root_client': self,
'base_url': self.base_url,
'http_client': self.http_client,
}
def actor(self, actor_id: str) -> ActorClient:
"""Retrieve the sub-client for manipulating a single actor.
Args:
actor_id (str): ID of the actor to be manipulated
"""
return ActorClient(resource_id=actor_id, **self._options())
def actors(self) -> ActorCollectionClient:
"""Retrieve the sub-client for manipulating actors."""
return ActorCollectionClient(**self._options())
def build(self, build_id: str) -> BuildClient:
"""Retrieve the sub-client for manipulating a single actor build.
Args:
build_id (str): ID of the actor build to be manipulated
"""
return BuildClient(resource_id=build_id, **self._options())
def builds(self) -> BuildCollectionClient:
"""Retrieve the sub-client for querying multiple builds of a user."""
return BuildCollectionClient(**self._options())
def run(self, run_id: str) -> RunClient:
"""Retrieve the sub-client for manipulating a single actor run.
Args:
run_id (str): ID of the actor run to be manipulated
"""
return RunClient(resource_id=run_id, **self._options())
def runs(self) -> RunCollectionClient:
"""Retrieve the sub-client for querying multiple actor runs of a user."""
return RunCollectionClient(**self._options())
def dataset(self, dataset_id: str) -> DatasetClient:
"""Retrieve the sub-client for manipulating a single dataset.
Args:
dataset_id (str): ID of the dataset to be manipulated
"""
return DatasetClient(resource_id=dataset_id, **self._options())
def datasets(self) -> DatasetCollectionClient:
"""Retrieve the sub-client for manipulating datasets."""
return DatasetCollectionClient(**self._options())
def key_value_store(self, key_value_store_id: str) -> KeyValueStoreClient:
"""Retrieve the sub-client for manipulating a single key-value store.
Args:
key_value_store_id (str): ID of the key-value store to be manipulated
"""
return KeyValueStoreClient(resource_id=key_value_store_id, **self._options())
def key_value_stores(self) -> KeyValueStoreCollectionClient:
"""Retrieve the sub-client for manipulating key-value stores."""
return KeyValueStoreCollectionClient(**self._options())
def request_queue(self, request_queue_id: str, *, client_key: Optional[str] = None) -> RequestQueueClient:
"""Retrieve the sub-client for manipulating a single request queue.
Args:
request_queue_id (str): ID of the request queue to be manipulated
client_key (str): A unique identifier of the client accessing the request queue
"""
return RequestQueueClient(resource_id=request_queue_id, client_key=client_key, **self._options())
def request_queues(self) -> RequestQueueCollectionClient:
"""Retrieve the sub-client for manipulating request queues."""
return RequestQueueCollectionClient(**self._options())
def webhook(self, webhook_id: str) -> WebhookClient:
"""Retrieve the sub-client for manipulating a single webhook.
Args:
webhook_id (str): ID of the webhook to be manipulated
"""
return WebhookClient(resource_id=webhook_id, **self._options())
def webhooks(self) -> WebhookCollectionClient:
"""Retrieve the sub-client for querying multiple webhooks of a user."""
return WebhookCollectionClient(**self._options())
def webhook_dispatch(self, webhook_dispatch_id: str) -> WebhookDispatchClient:
"""Retrieve the sub-client for accessing a single webhook dispatch.
Args:
webhook_dispatch_id (str): ID of the webhook dispatch to access
"""
return WebhookDispatchClient(resource_id=webhook_dispatch_id, **self._options())
def webhook_dispatches(self) -> WebhookDispatchCollectionClient:
"""Retrieve the sub-client for querying multiple webhook dispatches of a user."""
return WebhookDispatchCollectionClient(**self._options())
def schedule(self, schedule_id: str) -> ScheduleClient:
"""Retrieve the sub-client for manipulating a single schedule.
Args:
schedule_id (str): ID of the schedule to be manipulated
"""
return ScheduleClient(resource_id=schedule_id, **self._options())
def schedules(self) -> ScheduleCollectionClient:
"""Retrieve the sub-client for manipulating schedules."""
return ScheduleCollectionClient(**self._options())
def log(self, build_or_run_id: str) -> LogClient:
"""Retrieve the sub-client for retrieving logs.
Args:
build_or_run_id (str): ID of the actor build or run for which to access the log
"""
return LogClient(resource_id=build_or_run_id, **self._options())
def task(self, task_id: str) -> TaskClient:
"""Retrieve the sub-client for manipulating a single task.
Args:
task_id (str): ID of the task to be manipulated
"""
return TaskClient(resource_id=task_id, **self._options())
def tasks(self) -> TaskCollectionClient:
"""Retrieve the sub-client for manipulating tasks."""
return TaskCollectionClient(**self._options())
def user(self, user_id: Optional[str] = None) -> UserClient:
"""Retrieve the sub-client for querying users.
Args:
user_id (str, optional): ID of user to be queried. If None, queries the user belonging to the token supplied to the client
"""
return UserClient(resource_id=user_id, **self._options())