Skip to content

Commit a1e962e

Browse files
authored
feat: Separate ID and name params for Actor.open_xxx (#56)
1 parent 61a5349 commit a1e962e

12 files changed

+71
-44
lines changed

docs/docs.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ That’s useful if you want to use the client as a different Apify user than the
144144

145145
***
146146

147-
#### async classmethod open_dataset(dataset_id_or_name=None, \*, force_cloud=False)
147+
#### async classmethod open_dataset(\*, id=None, name=None, force_cloud=False)
148148

149149
Open a dataset.
150150

@@ -154,8 +154,11 @@ The actual data is stored either on the local filesystem or in the Apify cloud.
154154

155155
* **Parameters**
156156

157-
* **dataset_id_or_name** (`str`, *optional*) – ID or name of the dataset to be opened.
158-
If not provided, the method returns the default dataset associated with the actor run.
157+
* **id** (`str`, *optional*) – ID of the dataset to be opened.
158+
If neither id nor name are provided, the method returns the default dataset associated with the actor run.
159+
160+
* **name** (`str`, *optional*) – Name of the dataset to be opened.
161+
If neither id nor name are provided, the method returns the default dataset associated with the actor run.
159162

160163
* **force_cloud** (`bool`, *optional*) – If set to True then the Apify cloud storage is always used.
161164
This way it is possible to combine local and cloud storage.
@@ -170,7 +173,7 @@ The actual data is stored either on the local filesystem or in the Apify cloud.
170173

171174
***
172175

173-
#### async classmethod open_key_value_store(key_value_store_id_or_name=None, \*, force_cloud=False)
176+
#### async classmethod open_key_value_store(\*, id=None, name=None, force_cloud=False)
174177

175178
Open a key-value store.
176179

@@ -180,8 +183,11 @@ The actual data is stored either on a local filesystem or in the Apify cloud.
180183

181184
* **Parameters**
182185

183-
* **key_value_store_id_or_name** (`str`, *optional*) – ID or name of the key-value store to be opened.
184-
If not provided, the method returns the default key-value store associated with the actor run.
186+
* **id** (`str`, *optional*) – ID of the key-value store to be opened.
187+
If neither id nor name are provided, the method returns the default key-value store associated with the actor run.
188+
189+
* **name** (`str`, *optional*) – Name of the key-value store to be opened.
190+
If neither id nor name are provided, the method returns the default key-value store associated with the actor run.
185191

186192
* **force_cloud** (`bool`, *optional*) – If set to True then the Apify cloud storage is always used.
187193
This way it is possible to combine local and cloud storage.
@@ -196,7 +202,7 @@ The actual data is stored either on a local filesystem or in the Apify cloud.
196202

197203
***
198204

199-
#### async classmethod open_request_queue(request_queue_id_or_name=None, \*, force_cloud=False)
205+
#### async classmethod open_request_queue(\*, id=None, name=None, force_cloud=False)
200206

201207
Open a request queue.
202208

@@ -207,8 +213,11 @@ and depth-first crawling orders.
207213

208214
* **Parameters**
209215

210-
* **request_queue_id_or_name** (`str`, *optional*) – ID or name of the request queue to be opened.
211-
If not provided, the method returns the default request queue associated with the actor run.
216+
* **id** (`str`, *optional*) – ID of the request queue to be opened.
217+
If neither id nor name are provided, the method returns the default request queue associated with the actor run.
218+
219+
* **name** (`str`, *optional*) – Name of the request queue to be opened.
220+
If neither id nor name are provided, the method returns the default request queue associated with the actor run.
212221

213222
* **force_cloud** (`bool`, *optional*) – If set to True then the Apify cloud storage is always used.
214223
This way it is possible to combine local and cloud storage.

src/apify/actor.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -478,56 +478,68 @@ def _get_storage_client(self, force_cloud: bool) -> Optional[ApifyClientAsync]:
478478
return self._apify_client if force_cloud else None
479479

480480
@classmethod
481-
async def open_dataset(cls, dataset_id_or_name: Optional[str] = None, *, force_cloud: bool = False) -> Dataset:
481+
async def open_dataset(cls, *, id: Optional[str] = None, name: Optional[str] = None, force_cloud: bool = False) -> Dataset:
482482
"""Open a dataset.
483483
484484
Datasets are used to store structured data where each object stored has the same attributes,
485485
such as online store products or real estate offers.
486486
The actual data is stored either on the local filesystem or in the Apify cloud.
487487
488488
Args:
489-
dataset_id_or_name (str, optional): ID or name of the dataset to be opened.
490-
If not provided, the method returns the default dataset associated with the actor run.
489+
id (str, optional): ID of the dataset to be opened.
490+
If neither `id` nor `name` are provided, the method returns the default dataset associated with the actor run.
491+
name (str, optional): Name of the dataset to be opened.
492+
If neither `id` nor `name` are provided, the method returns the default dataset associated with the actor run.
491493
force_cloud (bool, optional): If set to `True` then the Apify cloud storage is always used.
492494
This way it is possible to combine local and cloud storage.
493495
494496
Returns:
495497
Dataset: An instance of the `Dataset` class for the given ID or name.
496498
497499
"""
498-
return await cls._get_default_instance().open_dataset(dataset_id_or_name=dataset_id_or_name, force_cloud=force_cloud)
500+
return await cls._get_default_instance().open_dataset(id=id, name=name, force_cloud=force_cloud)
499501

500-
async def _open_dataset_internal(self, dataset_id_or_name: Optional[str] = None, *, force_cloud: bool = False) -> Dataset:
502+
async def _open_dataset_internal(self, *, id: Optional[str] = None, name: Optional[str] = None, force_cloud: bool = False) -> Dataset:
501503
self._raise_if_not_initialized()
502504

505+
dataset_id_or_name = id or name
503506
return await StorageManager.open_storage(Dataset, dataset_id_or_name, self._get_storage_client(force_cloud), self._config)
504507

505508
@classmethod
506-
async def open_key_value_store(cls, key_value_store_id_or_name: Optional[str] = None, *, force_cloud: bool = False) -> KeyValueStore:
509+
async def open_key_value_store(cls, *, id: Optional[str] = None, name: Optional[str] = None, force_cloud: bool = False) -> KeyValueStore:
507510
"""Open a key-value store.
508511
509512
Key-value stores are used to store records or files, along with their MIME content type.
510513
The records are stored and retrieved using a unique key.
511514
The actual data is stored either on a local filesystem or in the Apify cloud.
512515
513516
Args:
514-
key_value_store_id_or_name (str, optional): ID or name of the key-value store to be opened.
515-
If not provided, the method returns the default key-value store associated with the actor run.
517+
id (str, optional): ID of the key-value store to be opened.
518+
If neither `id` nor `name` are provided, the method returns the default key-value store associated with the actor run.
519+
name (str, optional): Name of the key-value store to be opened.
520+
If neither `id` nor `name` are provided, the method returns the default key-value store associated with the actor run.
516521
force_cloud (bool, optional): If set to `True` then the Apify cloud storage is always used.
517522
This way it is possible to combine local and cloud storage.
518523
519524
Returns:
520525
KeyValueStore: An instance of the `KeyValueStore` class for the given ID or name.
521526
"""
522-
return await cls._get_default_instance().open_key_value_store(key_value_store_id_or_name=key_value_store_id_or_name, force_cloud=force_cloud)
527+
return await cls._get_default_instance().open_key_value_store(id=id, name=name, force_cloud=force_cloud)
523528

524-
async def _open_key_value_store_internal(self, key_value_store_id_or_name: Optional[str] = None, *, force_cloud: bool = False) -> KeyValueStore:
529+
async def _open_key_value_store_internal(
530+
self,
531+
*,
532+
id: Optional[str] = None,
533+
name: Optional[str] = None,
534+
force_cloud: bool = False,
535+
) -> KeyValueStore:
525536
self._raise_if_not_initialized()
526537

538+
key_value_store_id_or_name = id or name
527539
return await StorageManager.open_storage(KeyValueStore, key_value_store_id_or_name, self._get_storage_client(force_cloud), self._config)
528540

529541
@classmethod
530-
async def open_request_queue(cls, request_queue_id_or_name: Optional[str] = None, *, force_cloud: bool = False) -> RequestQueue:
542+
async def open_request_queue(cls, *, id: Optional[str] = None, name: Optional[str] = None, force_cloud: bool = False) -> RequestQueue:
531543
"""Open a request queue.
532544
533545
Request queue represents a queue of URLs to crawl, which is stored either on local filesystem or in the Apify cloud.
@@ -536,24 +548,28 @@ async def open_request_queue(cls, request_queue_id_or_name: Optional[str] = None
536548
and depth-first crawling orders.
537549
538550
Args:
539-
request_queue_id_or_name (str, optional): ID or name of the request queue to be opened.
540-
If not provided, the method returns the default request queue associated with the actor run.
551+
id (str, optional): ID of the request queue to be opened.
552+
If neither `id` nor `name` are provided, the method returns the default request queue associated with the actor run.
553+
name (str, optional): Name of the request queue to be opened.
554+
If neither `id` nor `name` are provided, the method returns the default request queue associated with the actor run.
541555
force_cloud (bool, optional): If set to `True` then the Apify cloud storage is always used.
542556
This way it is possible to combine local and cloud storage.
543557
544558
Returns:
545559
RequestQueue: An instance of the `RequestQueue` class for the given ID or name.
546560
"""
547-
return await cls._get_default_instance().open_request_queue(request_queue_id_or_name=request_queue_id_or_name, force_cloud=force_cloud)
561+
return await cls._get_default_instance().open_request_queue(id=id, name=name, force_cloud=force_cloud)
548562

549563
async def _open_request_queue_internal(
550564
self,
551-
request_queue_id_or_name: Optional[str] = None,
552565
*,
566+
id: Optional[str] = None,
567+
name: Optional[str] = None,
553568
force_cloud: bool = False,
554569
) -> RequestQueue:
555570
self._raise_if_not_initialized()
556571

572+
request_queue_id_or_name = id or name
557573
return await StorageManager.open_storage(RequestQueue, request_queue_id_or_name, self._get_storage_client(force_cloud), self._config)
558574

559575
@classmethod

src/apify/storages/dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Dataset:
4747
def __init__(self, id: str, name: Optional[str], client: Union[ApifyClientAsync, MemoryStorage]) -> None:
4848
"""Create a `Dataset` instance.
4949
50-
Do not use the constructor directly, use the `Dataset.open` function instead.
50+
Do not use the constructor directly, use the `Actor.open_dataset()` function instead.
5151
5252
Args:
5353
id (str): ID of the dataset.

src/apify/storages/key_value_store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class KeyValueStore:
5353
def __init__(self, id: str, name: Optional[str], client: Union[ApifyClientAsync, MemoryStorage]) -> None:
5454
"""Create a `KeyValueStore` instance.
5555
56-
Do not use the constructor directly, use the `KeyValueStore.open` function instead.
56+
Do not use the constructor directly, use the `Actor.open_key_value_store()` function instead.
5757
5858
Args:
5959
id (str): ID of the key-value store.

src/apify/storages/request_queue.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class RequestQueue:
9797
def __init__(self, id: str, name: Optional[str], client: Union[ApifyClientAsync, MemoryStorage]) -> None:
9898
"""Create a `RequestQueue` instance.
9999
100-
Do not use the constructor directly, use the `RequestQueue.open` function instead.
100+
Do not use the constructor directly, use the `Actor.open_request_queue()` function instead.
101101
102102
Args:
103103
id (str): ID of the request queue.
@@ -499,8 +499,10 @@ async def open(cls, request_queue_id_or_name: Optional[str] = None, config: Opti
499499
and depth-first crawling orders.
500500
501501
Args:
502-
request_queue_id_or_name (str, optional): ID or name of the request queue to be opened.
503-
If not provided, the method returns the default request queue associated with the actor run.
502+
id (str, optional): ID of the request queue to be opened.
503+
If neither `id` nor `name` are provided, the method returns the default request queue associated with the actor run.
504+
name (str, optional): Name of the request queue to be opened.
505+
If neither `id` nor `name` are provided, the method returns the default request queue associated with the actor run.
504506
config (Configuration, optional): A `Configuration` instance, uses global configuration if omitted.
505507
506508
Returns:

tests/integration/test_actor_dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ async def main() -> None:
4747
async with Actor:
4848
input_object = await Actor.get_input()
4949
dataset_name = input_object['datasetName']
50-
dataset1 = await Actor.open_dataset(dataset_name)
51-
dataset2 = await Actor.open_dataset(dataset_name)
50+
dataset1 = await Actor.open_dataset(name=dataset_name)
51+
dataset2 = await Actor.open_dataset(name=dataset_name)
5252
assert dataset1 is dataset2
5353
await dataset1.drop()
5454

tests/integration/test_actor_key_value_store.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ async def main() -> None:
2525
async with Actor:
2626
input_object = await Actor.get_input()
2727
kvs_name = input_object['kvsName']
28-
kvs1 = await Actor.open_key_value_store(kvs_name)
29-
kvs2 = await Actor.open_key_value_store(kvs_name)
28+
kvs1 = await Actor.open_key_value_store(name=kvs_name)
29+
kvs2 = await Actor.open_key_value_store(name=kvs_name)
3030
assert kvs1 is kvs2
3131
await kvs1.drop()
3232

@@ -75,7 +75,7 @@ async def main_get() -> None:
7575
async with Actor:
7676
input_object = await Actor.get_input()
7777
# Access KVS of the previous 'set' run
78-
kvs = await Actor.open_key_value_store(input_object['kvs-id'])
78+
kvs = await Actor.open_key_value_store(name=input_object['kvs-id'])
7979
value = await kvs.get_value('test')
8080
assert value['number'] == 123
8181
assert value['string'] == 'a string'

tests/integration/test_actor_request_queue.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ async def main() -> None:
2525
async with Actor:
2626
input_object = await Actor.get_input()
2727
rq_name = input_object['rqName']
28-
rq1 = await Actor.open_request_queue(rq_name)
29-
rq2 = await Actor.open_request_queue(rq_name)
28+
rq1 = await Actor.open_request_queue(name=rq_name)
29+
rq2 = await Actor.open_request_queue(name=rq_name)
3030
assert rq1 is rq2
3131
await rq1.drop()
3232

tests/unit/actor/test_actor_dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ async def test_same_references(self) -> None:
2020
dataset2 = await Actor.open_dataset()
2121
assert dataset1 is dataset2
2222
dataset_name = 'non-default'
23-
dataset_named1 = await Actor.open_dataset(dataset_name)
24-
dataset_named2 = await Actor.open_dataset(dataset_name)
23+
dataset_named1 = await Actor.open_dataset(name=dataset_name)
24+
dataset_named2 = await Actor.open_dataset(name=dataset_name)
2525
assert dataset_named1 is dataset_named2
2626

2727
async def test_open_datatset_based_env_var(

tests/unit/actor/test_actor_key_value_store.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ async def test_same_references(self) -> None:
1818
kvs2 = await Actor.open_key_value_store()
1919
assert kvs1 is kvs2
2020
kvs_name = 'non-default'
21-
kvs_named1 = await Actor.open_key_value_store(kvs_name)
22-
kvs_named2 = await Actor.open_key_value_store(kvs_name)
21+
kvs_named1 = await Actor.open_key_value_store(name=kvs_name)
22+
kvs_named2 = await Actor.open_key_value_store(name=kvs_name)
2323
assert kvs_named1 is kvs_named2
2424

2525

0 commit comments

Comments
 (0)