|
29 | 29 |
|
30 | 30 | import json |
31 | 31 | import warnings |
32 | | -from collections.abc import Mapping |
| 32 | +from collections.abc import Mapping, Sequence |
33 | 33 | from dataclasses import dataclass, field |
34 | | -from datetime import datetime |
| 34 | +from datetime import datetime, timezone |
35 | 35 | from pathlib import Path |
36 | 36 | from typing import Any |
37 | 37 |
|
|
50 | 50 | build_async_ip_pinned_transport, |
51 | 51 | ) |
52 | 52 | from adcp.signing.standard_webhooks import decode_secret as _decode_sw_secret |
53 | | -from adcp.types import AdcpProtocol, GeneratedTaskStatus, TaskType |
| 53 | +from adcp.types import ( |
| 54 | + AdcpProtocol, |
| 55 | + GeneratedTaskStatus, |
| 56 | + NotificationConfig, |
| 57 | + TaskType, |
| 58 | + WholesaleFeedEvent, |
| 59 | + WholesaleFeedWebhook, |
| 60 | +) |
54 | 61 | from adcp.types.generated_poc.core.async_response_data import AdcpAsyncResponseData |
55 | 62 | from adcp.webhook_auth import ( |
56 | 63 | AdcpLegacyHmacStrategy, |
@@ -121,6 +128,24 @@ def _validate_hooks(hooks: tuple[TransportHook, ...], allow_private_destinations |
121 | 128 | validate(allow_private_destinations=allow_private_destinations) |
122 | 129 |
|
123 | 130 |
|
| 131 | +def _entity_type_for_wholesale_notification(notification_type: str) -> str: |
| 132 | + if notification_type.startswith("product."): |
| 133 | + return "product" |
| 134 | + if notification_type.startswith("signal."): |
| 135 | + return "signal" |
| 136 | + if notification_type == "wholesale_feed.bulk_change": |
| 137 | + return "feed" |
| 138 | + raise ValueError( |
| 139 | + f"unsupported wholesale feed notification_type {notification_type!r}; " |
| 140 | + "expected product.*, signal.*, or wholesale_feed.bulk_change" |
| 141 | + ) |
| 142 | + |
| 143 | + |
| 144 | +def _enum_value(value: Any) -> str: |
| 145 | + raw = getattr(value, "value", value) |
| 146 | + return str(raw) |
| 147 | + |
| 148 | + |
124 | 149 | @dataclass(frozen=True) |
125 | 150 | class WebhookDeliveryResult: |
126 | 151 | """Outcome of one ``send_*`` call. |
@@ -680,6 +705,137 @@ async def send_property_list_changed( |
680 | 705 | url=url, idempotency_key=key, payload=payload, extra_headers=extra_headers |
681 | 706 | ) |
682 | 707 |
|
| 708 | + async def send_wholesale_feed( |
| 709 | + self, |
| 710 | + *, |
| 711 | + url: str, |
| 712 | + subscriber_id: str, |
| 713 | + account_id: str, |
| 714 | + notification_type: str, |
| 715 | + wholesale_feed_version: str, |
| 716 | + cache_scope: str, |
| 717 | + event: WholesaleFeedEvent | Mapping[str, Any], |
| 718 | + previous_wholesale_feed_version: str | None = None, |
| 719 | + fired_at: datetime | None = None, |
| 720 | + idempotency_key: str | None = None, |
| 721 | + subscription_event_types: Sequence[Any] | None = None, |
| 722 | + extra_headers: Mapping[str, str] | None = None, |
| 723 | + ) -> WebhookDeliveryResult: |
| 724 | + """POST a signed account-scoped wholesale feed notification. |
| 725 | +
|
| 726 | + ``subscription_event_types`` is optional but recommended when the |
| 727 | + caller is sending to an ``accounts[].notification_configs[]`` entry: |
| 728 | + pass that entry's ``event_types`` to fail closed if the subscription |
| 729 | + did not request this notification type. |
| 730 | + """ |
| 731 | + |
| 732 | + if not isinstance(subscriber_id, str) or not subscriber_id: |
| 733 | + raise ValueError("subscriber_id must be a non-empty string") |
| 734 | + if not isinstance(account_id, str) or not account_id: |
| 735 | + raise ValueError("account_id must be a non-empty string") |
| 736 | + if not isinstance(wholesale_feed_version, str) or not wholesale_feed_version: |
| 737 | + raise ValueError("wholesale_feed_version must be a non-empty string") |
| 738 | + |
| 739 | + event_model = event |
| 740 | + if not isinstance(event_model, WholesaleFeedEvent): |
| 741 | + event_model = WholesaleFeedEvent.model_validate(event_model) |
| 742 | + notification_type_value = _enum_value(notification_type) |
| 743 | + event_type = _enum_value(event_model.event_type) |
| 744 | + entity_type = _enum_value(event_model.entity_type) |
| 745 | + if notification_type_value != event_type: |
| 746 | + raise ValueError( |
| 747 | + "notification_type must match event.event_type " |
| 748 | + f"(got {notification_type_value!r}, event has {event_type!r})" |
| 749 | + ) |
| 750 | + if subscription_event_types is not None: |
| 751 | + allowed_event_types = {_enum_value(item) for item in subscription_event_types} |
| 752 | + else: |
| 753 | + allowed_event_types = None |
| 754 | + if allowed_event_types is not None and notification_type_value not in allowed_event_types: |
| 755 | + raise ValueError( |
| 756 | + "notification_type is not present in the subscription's event_types; " |
| 757 | + "sellers must not silently widen account notification filters" |
| 758 | + ) |
| 759 | + |
| 760 | + expected_entity_type = _entity_type_for_wholesale_notification(notification_type_value) |
| 761 | + if entity_type != expected_entity_type: |
| 762 | + raise ValueError( |
| 763 | + "event.entity_type does not match notification_type " |
| 764 | + f"(got {entity_type!r}, expected {expected_entity_type!r})" |
| 765 | + ) |
| 766 | + |
| 767 | + cache_scope_value = _enum_value(cache_scope) |
| 768 | + applies_to = getattr(event_model.payload, "applies_to", None) |
| 769 | + applies_to_scope = _enum_value(getattr(applies_to, "scope", None)) |
| 770 | + if applies_to_scope != cache_scope_value: |
| 771 | + raise ValueError( |
| 772 | + "cache_scope must match event.payload.applies_to.scope " |
| 773 | + f"(got {cache_scope_value!r}, event has {applies_to_scope!r})" |
| 774 | + ) |
| 775 | + |
| 776 | + key = idempotency_key or generate_webhook_idempotency_key() |
| 777 | + timestamp = fired_at or datetime.now(timezone.utc) |
| 778 | + webhook = WholesaleFeedWebhook.model_validate( |
| 779 | + { |
| 780 | + "idempotency_key": key, |
| 781 | + "notification_id": event_model.event_id, |
| 782 | + "notification_type": notification_type_value, |
| 783 | + "fired_at": timestamp, |
| 784 | + "subscriber_id": subscriber_id, |
| 785 | + "account_id": account_id, |
| 786 | + "wholesale_feed_version": wholesale_feed_version, |
| 787 | + "previous_wholesale_feed_version": previous_wholesale_feed_version, |
| 788 | + "cache_scope": cache_scope_value, |
| 789 | + "event": event_model, |
| 790 | + } |
| 791 | + ) |
| 792 | + return await self.send_raw( |
| 793 | + url=url, |
| 794 | + idempotency_key=key, |
| 795 | + payload=webhook.model_dump(mode="json", exclude_none=True), |
| 796 | + extra_headers=extra_headers, |
| 797 | + ) |
| 798 | + |
| 799 | + async def send_wholesale_feed_to_subscription( |
| 800 | + self, |
| 801 | + *, |
| 802 | + subscription: NotificationConfig | Mapping[str, Any], |
| 803 | + account_id: str, |
| 804 | + notification_type: str, |
| 805 | + wholesale_feed_version: str, |
| 806 | + cache_scope: str, |
| 807 | + event: WholesaleFeedEvent | Mapping[str, Any], |
| 808 | + previous_wholesale_feed_version: str | None = None, |
| 809 | + fired_at: datetime | None = None, |
| 810 | + idempotency_key: str | None = None, |
| 811 | + extra_headers: Mapping[str, str] | None = None, |
| 812 | + ) -> WebhookDeliveryResult: |
| 813 | + """POST a wholesale feed notification to a ``NotificationConfig``. |
| 814 | +
|
| 815 | + This convenience wrapper keeps ``url``, ``subscriber_id``, and |
| 816 | + ``event_types`` coupled to the same persisted subscription entry. |
| 817 | + """ |
| 818 | + |
| 819 | + config = ( |
| 820 | + subscription |
| 821 | + if isinstance(subscription, NotificationConfig) |
| 822 | + else NotificationConfig.model_validate(subscription) |
| 823 | + ) |
| 824 | + return await self.send_wholesale_feed( |
| 825 | + url=str(config.url), |
| 826 | + subscriber_id=config.subscriber_id, |
| 827 | + account_id=account_id, |
| 828 | + notification_type=notification_type, |
| 829 | + wholesale_feed_version=wholesale_feed_version, |
| 830 | + cache_scope=cache_scope, |
| 831 | + event=event, |
| 832 | + previous_wholesale_feed_version=previous_wholesale_feed_version, |
| 833 | + fired_at=fired_at, |
| 834 | + idempotency_key=idempotency_key, |
| 835 | + subscription_event_types=config.event_types, |
| 836 | + extra_headers=extra_headers, |
| 837 | + ) |
| 838 | + |
683 | 839 | async def send_raw( |
684 | 840 | self, |
685 | 841 | *, |
|
0 commit comments