feat: Accept Pydantic models as alternatives to dicts in resource client methods#663
feat: Accept Pydantic models as alternatives to dicts in resource client methods#663
Conversation
ac60efe to
a1715c9
Compare
a1715c9 to
ac60efe
Compare
322306d to
e68c909
Compare
e68c909 to
0d584bc
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #663 +/- ##
==========================================
+ Coverage 94.74% 94.82% +0.07%
==========================================
Files 45 45
Lines 4340 4404 +64
==========================================
+ Hits 4112 4176 +64
Misses 228 228
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
This PR maybe closes: #421 ? |
There was a problem hiding this comment.
Pull request overview
This PR improves input-side typing across resource clients by allowing selected methods to accept either plain dicts or corresponding Pydantic models, and centralizes ad-hoc webhook encoding/validation via a dedicated Pydantic model.
Changes:
- Expand several resource client method inputs from
dicttodict | PydanticModeland validate dicts via Pydantic before sending. - Replace
encode_webhook_list_to_base64withWebhookRepresentationListfor building the base64 webhook query param. - Introduce
RequestInput/RequestDeleteInputmodels for Request Queue operations and use them in RQ client methods.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/unit/test_utils.py | Updates webhook base64 encoding test to use WebhookRepresentationList and Pydantic webhook models. |
| src/apify_client/_utils.py | Removes the legacy encode_webhook_list_to_base64 helper. |
| src/apify_client/_types.py | Adds webhook representation + base64 encoding models and new Request Queue input models; updates polling models config. |
| src/apify_client/_resource_clients/task_collection.py | Accepts TaskInput models (or dicts validated into them) when creating tasks. |
| src/apify_client/_resource_clients/task.py | Accepts TaskInput and webhook model inputs; switches webhook encoding to WebhookRepresentationList. |
| src/apify_client/_resource_clients/run.py | Minor import formatting cleanup. |
| src/apify_client/_resource_clients/request_queue.py | Validates/serializes request payloads via new input models for add/batch ops; uses Pydantic model dumps consistently. |
| src/apify_client/_resource_clients/actor.py | Accepts webhook model inputs; switches webhook encoding to WebhookRepresentationList. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Also, maybe we could replace the |
…el-types # Conflicts: # src/apify_client/_resource_clients/actor.py # src/apify_client/_resource_clients/task.py
Yeah, this would be great and it crossed my mind as well. Unfortunately, I haven't found a good way to generate equivalent |
Changes summary
dict | PydanticModelunion types in resource client methods that previously only accepted dict, enabling type-safe usage alongside plain dict input.encode_webhook_list_to_base64utility with a structuredWebhookRepresentationListmodel that handles validation and base64 encoding.RequestInputandRequestDeleteInputmodels in_types.pyfor RQ operations, matching the actual API schema requirements.Issues
Closes: #421
Motivation
Improve typing on the input side of resource client methods. We don't want to accept only Pydantic models, which would require users to import additional classes. Instead, methods now accept both dict and the corresponding Pydantic model. This way, users don't have to import anything extra, but can optionally use the model types for type-safe usage and to discover the expected dict shape.
Next steps
Once we fully update the OpenAPI spec (apify/apify-docs#2286), we can remove the manually created models in
_types.pyand replace them with the generated ones.Test plan