|
5 | 5 | from datetime import timedelta |
6 | 6 | from typing import Annotated, Any, Literal |
7 | 7 |
|
8 | | -from pydantic import AnyUrl, BaseModel, ConfigDict, Field, RootModel |
| 8 | +from pydantic import AnyUrl, BaseModel, ConfigDict, Field, RootModel, model_validator |
9 | 9 |
|
10 | 10 | from apify_client._models import ActorJobStatus, WebhookCreate # noqa: TC001 |
11 | 11 |
|
@@ -62,13 +62,20 @@ class WebhookRepresentationList(RootModel[list[WebhookRepresentation]]): |
62 | 62 | """List of webhook representations with base64 encoding support.""" |
63 | 63 |
|
64 | 64 | @classmethod |
65 | | - def from_webhooks(cls, webhooks: list[WebhookCreate]) -> WebhookRepresentationList: |
66 | | - """Construct from a list of `WebhookCreate` models.""" |
| 65 | + def from_webhooks(cls, webhooks: list[dict | WebhookCreate]) -> WebhookRepresentationList: |
| 66 | + """Construct from a list of `WebhookCreate` models or plain dicts. |
| 67 | +
|
| 68 | + Dicts are validated directly as `WebhookRepresentation`, so only the minimal ad-hoc webhook fields |
| 69 | + (`event_types`, `request_url`, and optionally `payload_template`/`headers_template`) are required. |
| 70 | + """ |
67 | 71 | representations = list[WebhookRepresentation]() |
68 | 72 |
|
69 | | - for w in webhooks: |
70 | | - webhook_dict = w.model_dump(mode='json', exclude_none=True) |
71 | | - representations.append(WebhookRepresentation.model_validate(webhook_dict)) |
| 73 | + for webhook in webhooks: |
| 74 | + if isinstance(webhook, dict): |
| 75 | + representations.append(WebhookRepresentation.model_validate(webhook)) |
| 76 | + else: |
| 77 | + webhook_dict = webhook.model_dump(mode='json', exclude_none=True) |
| 78 | + representations.append(WebhookRepresentation.model_validate(webhook_dict)) |
72 | 79 |
|
73 | 80 | return cls(representations) |
74 | 81 |
|
@@ -131,3 +138,9 @@ class RequestDeleteInput(BaseModel): |
131 | 138 | Field(alias='uniqueKey', examples=['GET|60d83e70|e3b0c442|https://apify.com']), |
132 | 139 | ] = None |
133 | 140 | """A unique key used for request de-duplication.""" |
| 141 | + |
| 142 | + @model_validator(mode='after') |
| 143 | + def _check_at_least_one_identifier(self) -> RequestDeleteInput: |
| 144 | + if self.id is None and self.unique_key is None: |
| 145 | + raise ValueError('At least one of `id` or `unique_key` must be provided') |
| 146 | + return self |
0 commit comments