Skip to content

Commit d27cafc

Browse files
authored
Add data types to Enums, improve parsing of values of Enum arguments (#70)
1 parent 48dcfcc commit d27cafc

File tree

10 files changed

+50
-15
lines changed

10 files changed

+50
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Changelog
66

77
### Changed
88

9+
- improved handling of `Enum` arguments
910
- improved support for storing more data types in key-value stores
1011

1112
### Fixed

src/apify_client/_utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import re
66
import time
77
from datetime import datetime, timezone
8+
from enum import Enum
89
from http import HTTPStatus
910
from typing import Any, Callable, Dict, List, Optional, Tuple, TypeVar, cast
1011

@@ -138,7 +139,7 @@ def _encode_webhook_list_to_base64(webhooks: List[Dict]) -> bytes:
138139
data = []
139140
for webhook in webhooks:
140141
webhook_representation = {
141-
'eventTypes': [event_type.value for event_type in webhook['event_types']],
142+
'eventTypes': [_maybe_extract_enum_member_value(event_type) for event_type in webhook['event_types']],
142143
'requestUrl': webhook['request_url'],
143144
}
144145
if 'payload_template' in webhook:
@@ -203,6 +204,12 @@ def _encode_key_value_store_record_value(value: Any, content_type: Optional[str]
203204
return (value, content_type)
204205

205206

207+
def _maybe_extract_enum_member_value(maybe_enum_member: Any) -> Any:
208+
if isinstance(maybe_enum_member, Enum):
209+
return maybe_enum_member.value
210+
return maybe_enum_member
211+
212+
206213
class ListPage:
207214
"""A single page of items returned from a list() method."""
208215

src/apify_client/clients/resource_clients/actor.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
from typing import Any, Dict, List, Optional
22

3-
from ..._utils import _encode_key_value_store_record_value, _encode_webhook_list_to_base64, _parse_date_fields, _pluck_data
3+
from ..._utils import (
4+
_encode_key_value_store_record_value,
5+
_encode_webhook_list_to_base64,
6+
_maybe_extract_enum_member_value,
7+
_parse_date_fields,
8+
_pluck_data,
9+
)
410
from ...consts import ActorJobStatus
511
from ..base import ResourceClient
612
from .actor_version import ActorVersionClient
@@ -294,7 +300,7 @@ def last_run(self, *, status: Optional[ActorJobStatus] = None) -> RunClient:
294300
return RunClient(**self._sub_resource_init_options(
295301
resource_id='last',
296302
resource_path='runs',
297-
params=self._params(status=status.value if status is not None else None),
303+
params=self._params(status=_maybe_extract_enum_member_value(status) if status is not None else None),
298304
))
299305

300306
def versions(self) -> ActorVersionCollectionClient:

src/apify_client/clients/resource_clients/actor_version.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Any, Dict, List, Optional
22

3+
from ..._utils import _maybe_extract_enum_member_value
34
from ...consts import ActorSourceType
45
from ..base import ResourceClient
56

@@ -72,7 +73,7 @@ def update(
7273
if apply_env_vars_to_build is not None:
7374
version_fields['applyEnvVarsToBuild'] = apply_env_vars_to_build
7475
if source_type is not None:
75-
version_fields['sourceType'] = source_type.value
76+
version_fields['sourceType'] = _maybe_extract_enum_member_value(source_type)
7677
if source_code is not None:
7778
version_fields['sourceCode'] = source_code
7879
if base_docker_image is not None:

src/apify_client/clients/resource_clients/actor_version_collection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any, Dict, List, Optional
22

3-
from ..._utils import ListPage
3+
from ..._utils import ListPage, _maybe_extract_enum_member_value
44
from ...consts import ActorSourceType
55
from ..base import ResourceCollectionClient
66

@@ -77,7 +77,7 @@ def create(
7777
if apply_env_vars_to_build is not None:
7878
version_fields['applyEnvVarsToBuild'] = apply_env_vars_to_build
7979
if source_type is not None:
80-
version_fields['sourceType'] = source_type.value
80+
version_fields['sourceType'] = _maybe_extract_enum_member_value(source_type)
8181
if source_code is not None:
8282
version_fields['sourceCode'] = source_code
8383
if base_docker_image is not None:

src/apify_client/clients/resource_clients/run_collection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any, Optional
22

3-
from ..._utils import ListPage
3+
from ..._utils import ListPage, _maybe_extract_enum_member_value
44
from ...consts import ActorJobStatus
55
from ..base import ResourceCollectionClient
66

@@ -39,5 +39,5 @@ def list(
3939
limit=limit,
4040
offset=offset,
4141
desc=desc,
42-
status=status.value if status is not None else None,
42+
status=_maybe_extract_enum_member_value(status) if status is not None else None,
4343
)

src/apify_client/clients/resource_clients/task.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
from typing import Any, Dict, List, Optional, cast
22

33
from ..._errors import ApifyApiError
4-
from ..._utils import _catch_not_found_or_throw, _encode_webhook_list_to_base64, _filter_out_none_values_recursively, _parse_date_fields, _pluck_data
4+
from ..._utils import (
5+
_catch_not_found_or_throw,
6+
_encode_webhook_list_to_base64,
7+
_filter_out_none_values_recursively,
8+
_maybe_extract_enum_member_value,
9+
_parse_date_fields,
10+
_pluck_data,
11+
)
512
from ...consts import ActorJobStatus
613
from ..base import ResourceClient
714
from .run import RunClient
@@ -218,7 +225,7 @@ def last_run(self, *, status: Optional[ActorJobStatus] = None) -> RunClient:
218225
return RunClient(**self._sub_resource_init_options(
219226
resource_id='last',
220227
resource_path='runs',
221-
params=self._params(status=status.value if status is not None else None),
228+
params=self._params(status=_maybe_extract_enum_member_value(status) if status is not None else None),
222229
))
223230

224231
def webhooks(self) -> WebhookCollectionClient:

src/apify_client/clients/resource_clients/webhook.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Any, Dict, List, Optional
22

33
from ..._errors import ApifyApiError
4-
from ..._utils import _catch_not_found_or_throw, _parse_date_fields, _pluck_data, _snake_case_to_camel_case
4+
from ..._utils import _catch_not_found_or_throw, _maybe_extract_enum_member_value, _parse_date_fields, _pluck_data, _snake_case_to_camel_case
55
from ...consts import WebhookEventType
66
from ..base import ResourceClient
77
from .webhook_dispatch_collection import WebhookDispatchCollectionClient
@@ -40,7 +40,7 @@ def _prepare_webhook_representation(
4040
webhook['condition'] = condition
4141

4242
if event_types is not None:
43-
webhook['eventTypes'] = [event_type.value for event_type in event_types]
43+
webhook['eventTypes'] = [_maybe_extract_enum_member_value(event_type) for event_type in event_types]
4444

4545
return webhook
4646

src/apify_client/consts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from enum import Enum
22

33

4-
class ActorJobStatus(Enum):
4+
class ActorJobStatus(str, Enum):
55
"""Available statuses for actor jobs (runs or builds)."""
66

77
#: Actor job initialized but not started yet
@@ -27,7 +27,7 @@ def _is_terminal(self) -> bool:
2727
return self in (ActorJobStatus.SUCCEEDED, ActorJobStatus.FAILED, ActorJobStatus.TIMED_OUT, ActorJobStatus.ABORTED)
2828

2929

30-
class ActorSourceType(Enum):
30+
class ActorSourceType(str, Enum):
3131
"""Available source types for actors."""
3232

3333
#: Actor source code is a single JavaScript/Node.js file
@@ -42,7 +42,7 @@ class ActorSourceType(Enum):
4242
GITHUB_GIST = 'GITHUB_GIST'
4343

4444

45-
class WebhookEventType(Enum):
45+
class WebhookEventType(str, Enum):
4646
"""Events that can trigger a webhook."""
4747

4848
#: The actor run was created

tests/unit/test_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import time
33
import unittest
44
from datetime import datetime, timezone
5+
from enum import Enum
56
from typing import Any, Callable
67

78
from apify_client._utils import (
@@ -10,6 +11,7 @@
1011
_is_content_type_text,
1112
_is_content_type_xml,
1213
_is_file_or_bytes,
14+
_maybe_extract_enum_member_value,
1315
_parse_date_fields,
1416
_pluck_data,
1517
_retry_with_exp_backoff,
@@ -167,3 +169,14 @@ def test__encode_webhook_list_to_base64(self) -> None:
167169
]),
168170
b'W3siZXZlbnRUeXBlcyI6IFsiQUNUT1IuUlVOLkNSRUFURUQiXSwgInJlcXVlc3RVcmwiOiAiaHR0cHM6Ly9leGFtcGxlLmNvbS9ydW4tY3JlYXRlZCJ9LCB7ImV2ZW50VHlwZXMiOiBbIkFDVE9SLlJVTi5TVUNDRUVERUQiXSwgInJlcXVlc3RVcmwiOiAiaHR0cHM6Ly9leGFtcGxlLmNvbS9ydW4tc3VjY2VlZGVkIiwgInBheWxvYWRUZW1wbGF0ZSI6ICJ7XCJoZWxsb1wiOiBcIndvcmxkXCIsIFwicmVzb3VyY2VcIjp7e3Jlc291cmNlfX19In1d', # noqa: E501
169171
)
172+
173+
def test__maybe_extract_enum_member_value(self) -> None:
174+
class TestEnum(Enum):
175+
A = 'A'
176+
B = 'B'
177+
178+
self.assertEqual(_maybe_extract_enum_member_value(TestEnum.A), 'A')
179+
self.assertEqual(_maybe_extract_enum_member_value(TestEnum.B), 'B')
180+
self.assertEqual(_maybe_extract_enum_member_value('C'), 'C')
181+
self.assertEqual(_maybe_extract_enum_member_value(1), 1)
182+
self.assertEqual(_maybe_extract_enum_member_value(None), None)

0 commit comments

Comments
 (0)