Skip to content

Commit 64a173b

Browse files
committed
Introduce fully typed clients
1 parent 0fa43bc commit 64a173b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+4179
-1678
lines changed

docs/01_overview/code/01_usage_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ async def main() -> None:
1616
return
1717

1818
# Fetch results from the Actor run's default dataset.
19-
dataset_client = apify_client.dataset(call_result['defaultDatasetId'])
19+
dataset_client = apify_client.dataset(call_result.default_dataset_id)
2020
list_items_result = await dataset_client.list_items()
2121
print(f'Dataset: {list_items_result}')

docs/01_overview/code/01_usage_sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ def main() -> None:
1616
return
1717

1818
# Fetch results from the Actor run's default dataset.
19-
dataset_client = apify_client.dataset(call_result['defaultDatasetId'])
19+
dataset_client = apify_client.dataset(call_result.default_dataset_id)
2020
list_items_result = dataset_client.list_items()
2121
print(f'Dataset: {list_items_result}')

docs/02_concepts/code/01_async_support.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ async def main() -> None:
1111

1212
# Start the Actor and get the run ID
1313
run_result = await actor_client.start()
14-
run_client = apify_client.run(run_result['id'])
14+
run_client = apify_client.run(run_result.id)
1515
log_client = run_client.log()
1616

1717
# Stream the logs

docs/03_examples/code/02_tasks_async.py

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

33
from apify_client import ApifyClientAsync
4-
from apify_client.clients.resource_clients import TaskClientAsync
4+
from apify_client._models import Run, Task
5+
from apify_client._resource_clients import TaskClientAsync
56

67
TOKEN = 'MY-APIFY-TOKEN'
78
HASHTAGS = ['zebra', 'lion', 'hippo']
89

910

10-
async def run_apify_task(client: TaskClientAsync) -> dict:
11-
result = await client.call()
12-
return result or {}
11+
async def run_apify_task(client: TaskClientAsync) -> Run | None:
12+
return await client.call()
1313

1414

1515
async def main() -> None:
1616
apify_client = ApifyClientAsync(token=TOKEN)
1717

1818
# Create Apify tasks
19-
apify_tasks = list[dict]()
19+
apify_tasks = list[Task]()
2020
apify_tasks_client = apify_client.tasks()
2121

2222
for hashtag in HASHTAGS:
@@ -34,7 +34,7 @@ async def main() -> None:
3434
apify_task_clients = list[TaskClientAsync]()
3535

3636
for apify_task in apify_tasks:
37-
task_id = apify_task['id']
37+
task_id = apify_task.id
3838
apify_task_client = apify_client.task(task_id)
3939
apify_task_clients.append(apify_task_client)
4040

docs/03_examples/code/02_tasks_sync.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
from apify_client import ApifyClient
2-
from apify_client.clients.resource_clients import TaskClient
2+
from apify_client._models import Run, Task
3+
from apify_client._resource_clients import TaskClient
34

45
TOKEN = 'MY-APIFY-TOKEN'
56
HASHTAGS = ['zebra', 'lion', 'hippo']
67

78

8-
def run_apify_task(client: TaskClient) -> dict:
9-
result = client.call()
10-
return result or {}
9+
def run_apify_task(client: TaskClient) -> Run | None:
10+
return client.call()
1111

1212

1313
def main() -> None:
1414
apify_client = ApifyClient(token=TOKEN)
1515

1616
# Create Apify tasks
17-
apify_tasks = list[dict]()
17+
apify_tasks = list[Task]()
1818
apify_tasks_client = apify_client.tasks()
1919

2020
for hashtag in HASHTAGS:
@@ -32,18 +32,19 @@ def main() -> None:
3232
apify_task_clients = list[TaskClient]()
3333

3434
for apify_task in apify_tasks:
35-
task_id = apify_task['id']
35+
task_id = apify_task.id
3636
apify_task_client = apify_client.task(task_id)
3737
apify_task_clients.append(apify_task_client)
3838

3939
print('Task clients created:', apify_task_clients)
4040

4141
# Execute Apify tasks
42-
task_run_results = list[dict]()
42+
task_run_results = list[Run]()
4343

4444
for client in apify_task_clients:
4545
result = run_apify_task(client)
46-
task_run_results.append(result)
46+
if result is not None:
47+
task_run_results.append(result)
4748

4849
print('Task results:', task_run_results)
4950

docs/03_examples/code/03_retrieve_async.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ async def main() -> None:
1919

2020
for dataset_item in actor_datasets.items:
2121
# Dataset items can be handled here. Dataset items can be paginated
22-
dataset_client = apify_client.dataset(dataset_item['id'])
22+
dataset_client = apify_client.dataset(dataset_item.id)
2323
dataset_items = await dataset_client.list_items(limit=1000)
2424

2525
# Items can be pushed to single dataset
26-
merging_dataset_client = apify_client.dataset(merging_dataset['id'])
26+
merging_dataset_client = apify_client.dataset(merging_dataset.id)
2727
await merging_dataset_client.push_items(dataset_items.items)
2828

2929
# ...

docs/03_examples/code/03_retrieve_sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ def main() -> None:
1717

1818
for dataset_item in actor_datasets.items:
1919
# Dataset items can be handled here. Dataset items can be paginated
20-
dataset_client = apify_client.dataset(dataset_item['id'])
20+
dataset_client = apify_client.dataset(dataset_item.id)
2121
dataset_items = dataset_client.list_items(limit=1000)
2222

2323
# Items can be pushed to single dataset
24-
merging_dataset_client = apify_client.dataset(merging_dataset['id'])
24+
merging_dataset_client = apify_client.dataset(merging_dataset.id)
2525
merging_dataset_client.push_items(dataset_items.items)
2626

2727
# ...

pyproject.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ dev = [
5858
"setuptools", # setuptools are used by pytest but not explicitly required
5959
"types-colorama<0.5.0",
6060
"werkzeug<4.0.0", # Werkzeug is used by pytest-httpserver
61+
"datamodel-code-generator[http,ruff]<1.0.0",
6162
]
6263

6364
[tool.hatch.build.targets.wheel]
@@ -138,6 +139,10 @@ indent-style = "space"
138139
"N999", # Invalid module name
139140
"T201", # print found
140141
]
142+
"src/apify_client/_models.py" = [
143+
"D", # Everything from the pydocstyle
144+
"E501", # Line too long
145+
]
141146

142147
[tool.ruff.lint.flake8-quotes]
143148
docstring-quotes = "double"
@@ -187,3 +192,21 @@ exclude_lines = ["pragma: no cover", "if TYPE_CHECKING:", "assert_never()"]
187192

188193
[tool.ipdb]
189194
context = 7
195+
196+
# https://koxudaxi.github.io/datamodel-code-generator/
197+
[tool.datamodel-codegen]
198+
url = "https://docs.apify.com/api/openapi.json"
199+
input_file_type = "openapi"
200+
output = "src/apify_client/_models.py"
201+
target_python_version = "3.10"
202+
output_model_type = "pydantic_v2.BaseModel"
203+
use_schema_description = true
204+
use_field_description = true
205+
use_union_operator = true
206+
capitalise_enum_members = true
207+
collapse_root_models = true
208+
set_default_enum_member = true
209+
use_annotated = true
210+
wrap_string_literal = true
211+
snake_case_field = true
212+
formatters = ["ruff-check", "ruff-format"]

scripts/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_current_package_version() -> str:
2525
# It replaces the version number on the line with the format `version = "1.2.3"`
2626
def set_current_package_version(version: str) -> None:
2727
with open(PYPROJECT_TOML_FILE_PATH, 'r+', encoding='utf-8') as pyproject_toml_file:
28-
updated_pyproject_toml_file_lines = []
28+
updated_pyproject_toml_file_lines = list[str]()
2929
version_string_found = False
3030
for line in pyproject_toml_file:
3131
line_processed = line

src/apify_client/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from importlib import metadata
22

3-
from .client import ApifyClient, ApifyClientAsync
3+
from ._client import ApifyClient, ApifyClientAsync
44

55
__version__ = metadata.version('apify-client')
66

0 commit comments

Comments
 (0)