Skip to content

Commit f0ba351

Browse files
authored
feat: Use Actor env vars (#105)
1 parent b96e135 commit f0ba351

File tree

12 files changed

+55
-54
lines changed

12 files changed

+55
-54
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ dependencies = [
2626
"aiofiles >= 22.1.0",
2727
"aioshutil >= 1.0",
2828
"apify-client >= 1.3.1",
29-
"apify-shared >= 1.0.0",
29+
"apify-shared >= 1.0.2",
3030
"colorama >= 0.4.6",
3131
"cryptography >= 39.0.0",
3232
"httpx >= 0.24.1",

src/apify/_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
INTEGER_ENV_VARS,
3535
INTEGER_ENV_VARS_TYPE,
3636
STRING_ENV_VARS_TYPE,
37+
ActorEnvVars,
3738
ApifyEnvVars,
3839
)
3940
from apify_shared.utils import ignore_docs, is_content_type_json, is_content_type_text, is_content_type_xml, maybe_extract_enum_member_value
@@ -144,7 +145,7 @@ def _fetch_and_parse_env_var(env_var: STRING_ENV_VARS_TYPE) -> Optional[str]:
144145

145146

146147
@overload
147-
def _fetch_and_parse_env_var(env_var: ApifyEnvVars) -> Optional[Any]:
148+
def _fetch_and_parse_env_var(env_var: Union[ActorEnvVars, ApifyEnvVars]) -> Optional[Any]:
148149
...
149150

150151

src/apify/actor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import Any, Awaitable, Callable, Dict, List, Optional, Type, TypeVar, Union, cast
1010

1111
from apify_client import ApifyClientAsync
12-
from apify_shared.consts import ActorEventTypes, ActorExitCodes, ApifyEnvVars, WebhookEventType
12+
from apify_shared.consts import ActorEnvVars, ActorEventTypes, ActorExitCodes, ApifyEnvVars, WebhookEventType
1313
from apify_shared.utils import ignore_docs, maybe_extract_enum_member_value
1414

1515
from ._crypto import _decrypt_input_secrets, _load_private_key
@@ -748,7 +748,7 @@ def _get_env_internal(self) -> Dict:
748748
self._raise_if_not_initialized()
749749

750750
return {
751-
env_var.name.lower(): _fetch_and_parse_env_var(env_var) for env_var in ApifyEnvVars
751+
env_var.name.lower(): _fetch_and_parse_env_var(env_var) for env_var in [*ActorEnvVars, *ApifyEnvVars]
752752
}
753753

754754
@classmethod

src/apify/config.py

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

3-
from apify_shared.consts import ApifyEnvVars
3+
from apify_shared.consts import ActorEnvVars, ApifyEnvVars
44

55
from ._utils import _fetch_and_parse_env_var
66

@@ -67,30 +67,30 @@ def __init__(
6767
system_info_interval_millis (str, optional): How often should the actor emit the SYSTEM_INFO event when running locally.
6868
"""
6969
# TODO: Document all these members
70-
self.actor_build_id = _fetch_and_parse_env_var(ApifyEnvVars.ACTOR_BUILD_ID)
71-
self.actor_build_number = _fetch_and_parse_env_var(ApifyEnvVars.ACTOR_BUILD_NUMBER)
72-
self.actor_events_ws_url = _fetch_and_parse_env_var(ApifyEnvVars.ACTOR_EVENTS_WS_URL)
73-
self.actor_id = _fetch_and_parse_env_var(ApifyEnvVars.ACTOR_ID)
74-
self.actor_run_id = _fetch_and_parse_env_var(ApifyEnvVars.ACTOR_RUN_ID)
75-
self.actor_task_id = _fetch_and_parse_env_var(ApifyEnvVars.ACTOR_TASK_ID)
70+
self.actor_build_id = _fetch_and_parse_env_var(ActorEnvVars.BUILD_ID)
71+
self.actor_build_number = _fetch_and_parse_env_var(ActorEnvVars.BUILD_NUMBER)
72+
self.actor_events_ws_url = _fetch_and_parse_env_var(ActorEnvVars.EVENTS_WEBSOCKET_URL)
73+
self.actor_id = _fetch_and_parse_env_var(ActorEnvVars.ID)
74+
self.actor_run_id = _fetch_and_parse_env_var(ActorEnvVars.RUN_ID)
75+
self.actor_task_id = _fetch_and_parse_env_var(ActorEnvVars.TASK_ID)
7676
self.api_base_url = api_base_url or _fetch_and_parse_env_var(ApifyEnvVars.API_BASE_URL, 'https://api.apify.com')
7777
self.api_public_base_url = api_public_base_url or _fetch_and_parse_env_var(ApifyEnvVars.API_PUBLIC_BASE_URL, 'https://api.apify.com')
7878
self.chrome_executable_path = _fetch_and_parse_env_var(ApifyEnvVars.CHROME_EXECUTABLE_PATH)
79-
self.container_port = container_port or _fetch_and_parse_env_var(ApifyEnvVars.CONTAINER_PORT, 4321)
80-
self.container_url = container_url or _fetch_and_parse_env_var(ApifyEnvVars.CONTAINER_URL, 'http://localhost:4321')
79+
self.container_port = container_port or _fetch_and_parse_env_var(ActorEnvVars.WEB_SERVER_PORT, 4321)
80+
self.container_url = container_url or _fetch_and_parse_env_var(ActorEnvVars.WEB_SERVER_URL, 'http://localhost:4321')
8181
self.dedicated_cpus = _fetch_and_parse_env_var(ApifyEnvVars.DEDICATED_CPUS)
8282
self.default_browser_path = _fetch_and_parse_env_var(ApifyEnvVars.DEFAULT_BROWSER_PATH)
83-
self.default_dataset_id = default_dataset_id or _fetch_and_parse_env_var(ApifyEnvVars.DEFAULT_DATASET_ID, 'default')
84-
self.default_key_value_store_id = default_key_value_store_id or _fetch_and_parse_env_var(ApifyEnvVars.DEFAULT_KEY_VALUE_STORE_ID, 'default')
85-
self.default_request_queue_id = default_request_queue_id or _fetch_and_parse_env_var(ApifyEnvVars.DEFAULT_REQUEST_QUEUE_ID, 'default')
83+
self.default_dataset_id = default_dataset_id or _fetch_and_parse_env_var(ActorEnvVars.DEFAULT_DATASET_ID, 'default')
84+
self.default_key_value_store_id = default_key_value_store_id or _fetch_and_parse_env_var(ActorEnvVars.DEFAULT_KEY_VALUE_STORE_ID, 'default')
85+
self.default_request_queue_id = default_request_queue_id or _fetch_and_parse_env_var(ActorEnvVars.DEFAULT_REQUEST_QUEUE_ID, 'default')
8686
self.disable_browser_sandbox = _fetch_and_parse_env_var(ApifyEnvVars.DISABLE_BROWSER_SANDBOX, False)
8787
self.headless = _fetch_and_parse_env_var(ApifyEnvVars.HEADLESS, True)
88-
self.input_key = input_key or _fetch_and_parse_env_var(ApifyEnvVars.INPUT_KEY, 'INPUT')
88+
self.input_key = input_key or _fetch_and_parse_env_var(ActorEnvVars.INPUT_KEY, 'INPUT')
8989
self.input_secrets_private_key_file = _fetch_and_parse_env_var(ApifyEnvVars.INPUT_SECRETS_PRIVATE_KEY_FILE)
9090
self.input_secrets_private_key_passphrase = _fetch_and_parse_env_var(ApifyEnvVars.INPUT_SECRETS_PRIVATE_KEY_PASSPHRASE)
9191
self.is_at_home = _fetch_and_parse_env_var(ApifyEnvVars.IS_AT_HOME, False)
9292
self.max_used_cpu_ratio = max_used_cpu_ratio or _fetch_and_parse_env_var(ApifyEnvVars.MAX_USED_CPU_RATIO, 0.95)
93-
self.memory_mbytes = _fetch_and_parse_env_var(ApifyEnvVars.MEMORY_MBYTES)
93+
self.memory_mbytes = _fetch_and_parse_env_var(ActorEnvVars.MEMORY_MBYTES)
9494
self.meta_origin = _fetch_and_parse_env_var(ApifyEnvVars.META_ORIGIN)
9595
self.metamorph_after_sleep_millis = metamorph_after_sleep_millis or _fetch_and_parse_env_var(ApifyEnvVars.METAMORPH_AFTER_SLEEP_MILLIS, 300000) # noqa: E501
9696
self.persist_state_interval_millis = persist_state_interval_millis or _fetch_and_parse_env_var(ApifyEnvVars.PERSIST_STATE_INTERVAL_MILLIS, 60000) # noqa: E501
@@ -100,8 +100,8 @@ def __init__(
100100
self.proxy_port = proxy_port or _fetch_and_parse_env_var(ApifyEnvVars.PROXY_PORT, 8000)
101101
self.proxy_status_url = proxy_status_url or _fetch_and_parse_env_var(ApifyEnvVars.PROXY_STATUS_URL, 'http://proxy.apify.com')
102102
self.purge_on_start = purge_on_start or _fetch_and_parse_env_var(ApifyEnvVars.PURGE_ON_START, False)
103-
self.started_at = _fetch_and_parse_env_var(ApifyEnvVars.STARTED_AT)
104-
self.timeout_at = _fetch_and_parse_env_var(ApifyEnvVars.TIMEOUT_AT)
103+
self.started_at = _fetch_and_parse_env_var(ActorEnvVars.STARTED_AT)
104+
self.timeout_at = _fetch_and_parse_env_var(ActorEnvVars.TIMEOUT_AT)
105105
self.token = token or _fetch_and_parse_env_var(ApifyEnvVars.TOKEN)
106106
self.user_id = _fetch_and_parse_env_var(ApifyEnvVars.USER_ID)
107107
self.xvfb = _fetch_and_parse_env_var(ApifyEnvVars.XVFB, False)

tests/integration/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ You can also pass extra imports directly to the main function:
6363
async def test_something(self, make_actor: ActorFactory) -> None:
6464
async def main():
6565
import os
66-
from apify_shared.consts import ActorEventTypes, ApifyEnvVars
66+
from apify_shared.consts import ActorEventTypes, ActorEnvVars
6767
async with Actor:
68-
print('The actor is running with ' + os.getenv(ApifyEnvVars.MEMORY_MBYTES) + 'MB of memory')
68+
print('The actor is running with ' + os.getenv(ActorEnvVars.MEMORY_MBYTES) + 'MB of memory')
6969
await Actor.on(ActorEventTypes.SYSTEM_INFO, lambda event_data: print(event_data))
7070

7171
actor = await make_actor('something', main_func=main)

tests/integration/test_actor_api_helpers.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ async def test_actor_new_client(self, make_actor: ActorFactory) -> None:
5454
async def main() -> None:
5555
import os
5656

57-
from apify_shared.consts import ApifyEnvVars
57+
from apify_shared.consts import ActorEnvVars
5858

5959
async with Actor:
6060
new_client = Actor.new_client()
6161
assert new_client is not Actor.apify_client
6262

63-
default_key_value_store_id = os.getenv(ApifyEnvVars.DEFAULT_KEY_VALUE_STORE_ID)
63+
default_key_value_store_id = os.getenv(ActorEnvVars.DEFAULT_KEY_VALUE_STORE_ID)
6464
assert default_key_value_store_id is not None
6565
kv_store_client = new_client.key_value_store(default_key_value_store_id)
6666
await kv_store_client.set_record('OUTPUT', 'TESTING-OUTPUT')
@@ -272,11 +272,11 @@ async def test_actor_metamorph(self, make_actor: ActorFactory) -> None:
272272
async def main_inner() -> None:
273273
import os
274274

275-
from apify_shared.consts import ApifyEnvVars
275+
from apify_shared.consts import ActorEnvVars
276276

277277
async with Actor:
278-
assert os.getenv(ApifyEnvVars.INPUT_KEY) is not None
279-
assert os.getenv(ApifyEnvVars.INPUT_KEY) != 'INPUT'
278+
assert os.getenv(ActorEnvVars.INPUT_KEY) is not None
279+
assert os.getenv(ActorEnvVars.INPUT_KEY) != 'INPUT'
280280
input = await Actor.get_input() or {}
281281

282282
test_value = input.get('test_value', '')
@@ -329,7 +329,7 @@ async def main_server() -> None:
329329
import os
330330
from http.server import BaseHTTPRequestHandler, HTTPServer
331331

332-
from apify_shared.consts import ApifyEnvVars
332+
from apify_shared.consts import ActorEnvVars
333333

334334
webhook_body = ''
335335

@@ -351,7 +351,7 @@ def do_POST(self) -> None: # noqa: N802
351351
self.end_headers()
352352
self.wfile.write(bytes('Hello, world!', encoding='utf-8'))
353353

354-
container_port = int(os.getenv(ApifyEnvVars.CONTAINER_PORT, ''))
354+
container_port = int(os.getenv(ActorEnvVars.WEB_SERVER_PORT, ''))
355355
with HTTPServer(('', container_port), WebhookHandler) as server:
356356
await Actor.set_value('INITIALIZED', True)
357357
while not webhook_body:

tests/integration/test_fixtures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ async def test_main_func(self, make_actor: ActorFactory) -> None:
1212
async def main() -> None:
1313
import os
1414

15-
from apify_shared.consts import ApifyEnvVars
15+
from apify_shared.consts import ActorEnvVars
1616

1717
async with Actor:
18-
await Actor.set_value('OUTPUT', os.getenv(ApifyEnvVars.ACTOR_ID))
18+
await Actor.set_value('OUTPUT', os.getenv(ActorEnvVars.ID))
1919

2020
actor = await make_actor('make-actor-main-func', main_func=main)
2121

tests/unit/actor/test_actor_dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from apify import Actor
55
from apify._memory_storage import MemoryStorageClient
6-
from apify_shared.consts import ApifyEnvVars
6+
from apify_shared.consts import ActorEnvVars
77

88
# NOTE: We only test the dataset methond available on Actor class/instance. Actual tests for the implementations are in storages/.
99

@@ -36,7 +36,7 @@ async def test_open_datatset_based_env_var(
3636
memory_storage_client: MemoryStorageClient,
3737
) -> None:
3838
default_dataset_id = 'my-new-default-id'
39-
monkeypatch.setenv(ApifyEnvVars.DEFAULT_DATASET_ID, default_dataset_id)
39+
monkeypatch.setenv(ActorEnvVars.DEFAULT_DATASET_ID, default_dataset_id)
4040

4141
async with Actor:
4242
ddt = await Actor.open_dataset()

tests/unit/actor/test_actor_env_helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77

88
from apify import Actor
9-
from apify_shared.consts import BOOL_ENV_VARS, DATETIME_ENV_VARS, FLOAT_ENV_VARS, INTEGER_ENV_VARS, STRING_ENV_VARS, ApifyEnvVars
9+
from apify_shared.consts import BOOL_ENV_VARS, DATETIME_ENV_VARS, FLOAT_ENV_VARS, INTEGER_ENV_VARS, STRING_ENV_VARS, ActorEnvVars, ApifyEnvVars
1010

1111

1212
class TestIsAtHome:
@@ -52,8 +52,8 @@ async def test_get_env_use_env_vars(self, monkeypatch: pytest.MonkeyPatch) -> No
5252
monkeypatch.setenv(string_env_var, expected_get_env[string_get_env_var])
5353

5454
# We need this override so that the actor doesn't fail when connecting to the platform events websocket
55-
monkeypatch.delenv(ApifyEnvVars.ACTOR_EVENTS_WS_URL)
56-
expected_get_env[ApifyEnvVars.ACTOR_EVENTS_WS_URL.name.lower()] = None
55+
monkeypatch.delenv(ActorEnvVars.EVENTS_WEBSOCKET_URL)
56+
expected_get_env[ActorEnvVars.EVENTS_WEBSOCKET_URL.name.lower()] = None
5757

5858
await Actor.init()
5959
assert expected_get_env == Actor.get_env()

tests/unit/test_config.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
from apify.config import Configuration
6-
from apify_shared.consts import ApifyEnvVars
6+
from apify_shared.consts import ActorEnvVars, ApifyEnvVars
77

88

99
class TestConfiguration:
@@ -28,13 +28,13 @@ def test_configuration_from_env_vars(self, monkeypatch: pytest.MonkeyPatch) -> N
2828
monkeypatch.setenv(ApifyEnvVars.PROXY_PASSWORD, 'DUMMY_PROXY_PASSWORD')
2929
monkeypatch.setenv(ApifyEnvVars.API_BASE_URL, 'DUMMY_API_BASE_URL')
3030
monkeypatch.setenv(ApifyEnvVars.PROXY_HOSTNAME, 'DUMMY_PROXY_HOSTNAME')
31-
monkeypatch.setenv(ApifyEnvVars.DEFAULT_KEY_VALUE_STORE_ID, 'DUMMY_DEFAULT_KEY_VALUE_STORE_ID')
32-
monkeypatch.setenv(ApifyEnvVars.DEFAULT_REQUEST_QUEUE_ID, 'DUMMY_DEFAULT_REQUEST_QUEUE_ID')
33-
monkeypatch.setenv(ApifyEnvVars.DEFAULT_DATASET_ID, 'DUMMY_DEFAULT_DATASET_ID')
31+
monkeypatch.setenv(ActorEnvVars.DEFAULT_KEY_VALUE_STORE_ID, 'DUMMY_DEFAULT_KEY_VALUE_STORE_ID')
32+
monkeypatch.setenv(ActorEnvVars.DEFAULT_REQUEST_QUEUE_ID, 'DUMMY_DEFAULT_REQUEST_QUEUE_ID')
33+
monkeypatch.setenv(ActorEnvVars.DEFAULT_DATASET_ID, 'DUMMY_DEFAULT_DATASET_ID')
3434
monkeypatch.setenv(ApifyEnvVars.IS_AT_HOME, '1')
3535
monkeypatch.setenv(ApifyEnvVars.PROXY_PORT, '1234')
36-
monkeypatch.setenv(ApifyEnvVars.MEMORY_MBYTES, '1024')
37-
monkeypatch.setenv(ApifyEnvVars.STARTED_AT, '2023-01-01T12:34:56.789Z')
36+
monkeypatch.setenv(ActorEnvVars.MEMORY_MBYTES, '1024')
37+
monkeypatch.setenv(ActorEnvVars.STARTED_AT, '2023-01-01T12:34:56.789Z')
3838

3939
config = Configuration()
4040
assert config.token == 'DUMMY_TOKEN'
@@ -55,9 +55,9 @@ def test_configuration_from_constructor_arguments(self, monkeypatch: pytest.Monk
5555
monkeypatch.setenv(ApifyEnvVars.PROXY_PASSWORD, 'DUMMY_PROXY_PASSWORD')
5656
monkeypatch.setenv(ApifyEnvVars.API_BASE_URL, 'DUMMY_API_BASE_URL')
5757
monkeypatch.setenv(ApifyEnvVars.PROXY_HOSTNAME, 'DUMMY_PROXY_HOSTNAME')
58-
monkeypatch.setenv(ApifyEnvVars.DEFAULT_DATASET_ID, 'DUMMY_DEFAULT_DATASET_ID')
59-
monkeypatch.setenv(ApifyEnvVars.DEFAULT_KEY_VALUE_STORE_ID, 'DUMMY_DEFAULT_KEY_VALUE_STORE_ID')
60-
monkeypatch.setenv(ApifyEnvVars.DEFAULT_REQUEST_QUEUE_ID, 'DUMMY_DEFAULT_REQUEST_QUEUE_ID')
58+
monkeypatch.setenv(ActorEnvVars.DEFAULT_DATASET_ID, 'DUMMY_DEFAULT_DATASET_ID')
59+
monkeypatch.setenv(ActorEnvVars.DEFAULT_KEY_VALUE_STORE_ID, 'DUMMY_DEFAULT_KEY_VALUE_STORE_ID')
60+
monkeypatch.setenv(ActorEnvVars.DEFAULT_REQUEST_QUEUE_ID, 'DUMMY_DEFAULT_REQUEST_QUEUE_ID')
6161
monkeypatch.setenv(ApifyEnvVars.PROXY_PORT, '1234')
6262

6363
config = Configuration(

0 commit comments

Comments
 (0)