Commit 81ee194
authored
refactor!: Introduce fully typed clients (#604)
### Summary
This is a major refactoring that introduces fully typed Pydantic models
throughout the client library. The models are generated from the OpenAPI
specifications. All API responses now return typed objects instead of
raw dictionaries.
This follows up on apify/apify-docs#2182.
### Issues
- Closes: #21
- Closes: #481
### Packages
- Add direct dependency on `Pydantic`.
- Removes the dependency on `apify-shared`.
- Add dev dependency
[datamodel-code-generator](https://koxudaxi.github.io/datamodel-code-generator/)
for model generation.
### Key changes
- Uses
[datamodel-code-generator](https://koxudaxi.github.io/datamodel-code-generator/)
tool configured via `pyproject.toml` to generate Pydantic models based
on the [OpenAPI specs](https://docs.apify.com/api/openapi.json).
- Refactors the whole codebase to adopt the new generated models.
- All resource clients now return typed Pydantic models (`Actor`,
`Task`, `Run`, etc.).
- Adds response wrappers for validating and extracting API response
data.
- Updates list methods to return typed pagination models.
- Documentation examples now use typed attribute access.
- Updates the SDK to use the new typed client.
- See the corresponding PR in `apify/apify-sdk-python` for details -
apify/apify-sdk-python#719.
- It will be merged later.
### Architecture
- Get rid of 3/4/5 levels of inheritance.
- Get rid of inline imports because of circular dependencies.
- I had to utilize `ClientRegistry` to be able to achieve that (because
of resource clients-siblings imports).
### Breaking changes
- Client methods now return Pydantic models instead of dicts.
- Access patterns change from dict-style (`result['key']`) to
attribute-style (`result.key`).
### Test plan
- Updated test concurrency to 16 workers.
- A lot of new tests were implemented - coverage ~95%.
- Unit tests - do not call production API, only for testing utils or
other functionality using mocks.
- Integration tests - call production API.
- Thanks to the new tests, I was able to do a lot of fixes in the
OpenAPI specs.
### Next steps
- Explore the generation of resource clients using
[openapi-python-client](https://github.com/openapi-generators/openapi-python-client).
- Fully automate model updates based on changes in
[apify-api/openapi](https://github.com/apify/apify-docs/tree/master/apify-api/openapi).
- This will be released as part of the Apify client v3.0.1 parent dc6cf5c commit 81ee194
107 files changed
Lines changed: 13963 additions & 5148 deletions
File tree
- .github/workflows
- docs
- 01_overview/code
- 02_concepts/code
- 03_examples/code
- scripts
- src/apify_client
- _http_clients
- _resource_clients
- clients
- base
- resource_clients
- tests
- integration
- unit
- website/src/pages
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
| 23 | + | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
10 | | - | |
11 | | - | |
| 12 | + | |
| 13 | + | |
12 | 14 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
6 | | - | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
10 | | - | |
11 | | - | |
| 12 | + | |
| 13 | + | |
12 | 14 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
20 | 23 | | |
21 | 24 | | |
22 | 25 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
17 | | - | |
| 19 | + | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | 9 | | |
16 | 10 | | |
17 | 11 | | |
18 | 12 | | |
19 | | - | |
| 13 | + | |
20 | 14 | | |
21 | 15 | | |
22 | 16 | | |
| |||
31 | 25 | | |
32 | 26 | | |
33 | 27 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
| 28 | + | |
40 | 29 | | |
41 | 30 | | |
42 | 31 | | |
43 | 32 | | |
44 | | - | |
45 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
46 | 39 | | |
47 | | - | |
| 40 | + | |
48 | 41 | | |
49 | 42 | | |
50 | 43 | | |
| |||
0 commit comments