61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278 | async def get_subscription_for_entity( # noqa: C901
self,
*,
entity_type: ContentType,
subscriber_entity_id: str,
context: RouteContext | None = None,
**context_kwargs: Any,
) -> dict[str, Any]:
"""Find the subscription that contains the given subscriber entity.
Returns:
Raw subscription summary dict from the API. Callers should hydrate
this into a DomoSubscription instance in the classes layer.
"""
if not self.parent_auth_retrieval_fn:
raise ValueError(
"parent_auth_retrieval_fn is required to resolve subscriptions. "
"This function should accept a publisher_domain and return DomoAuth "
"for that instance (sync or async)."
)
context = RouteContext.build_context(
context=context,
session=self.session,
debug_api=self.debug_api,
debug_num_stacks_to_drop=2,
parent_class=self.__class__.__name__,
)
summaries_res = await publish_routes.get_subscription_summaries(
auth=self.subscriber_auth,
context=context,
)
if not summaries_res.is_success or not summaries_res.response:
raise ValueError(
f"Failed to retrieve subscriptions for instance "
f"{self.subscriber_auth.domo_instance}"
)
subscriptions_checked = 0
total_subscriptions = len(summaries_res.response)
target_id = str(subscriber_entity_id)
normalized_entity_type = (entity_type or "").upper()
acceptable_content_types = {normalized_entity_type}
# entity_type is now consistently "DATA_SOURCE" (not "DATASET"),
# but the API may return either value, so accept both aliases
if normalized_entity_type in ("DATA_SOURCE", "DATASET"):
acceptable_content_types.update({"DATA_SOURCE", "DATASET"})
await logger.debug(
f"Found {total_subscriptions} subscriptions",
extra={
"entity_type": entity_type,
"subscriber_entity_id": subscriber_entity_id,
"subscriber_instance": self.subscriber_auth.domo_instance,
"total_subscriptions": total_subscriptions,
},
)
for summary in summaries_res.response:
if (
self.max_subscriptions_to_check is not None
and subscriptions_checked >= self.max_subscriptions_to_check
):
await logger.debug(
f"Reached max subscriptions limit ({self.max_subscriptions_to_check}), stopping search",
extra={"max_subscriptions": self.max_subscriptions_to_check},
)
break
subscription_id = summary.get("subscriptionId")
publication_id = summary.get("publicationId")
subscriber_domain = summary.get("subscriberDomain")
publisher_domain = summary.get("publisherDomain")
if (
not subscription_id
or not publication_id
or not subscriber_domain
or not publisher_domain
):
continue
subscriptions_checked += 1
sub_idx = (
self.max_subscriptions_to_check
if self.max_subscriptions_to_check
else total_subscriptions
)
await logger.debug(
f"Checking subscription {subscriptions_checked}/{sub_idx}: {subscription_id} - {publisher_domain}",
extra={
"subscription_id": subscription_id,
"publisher_domain": publisher_domain,
"publication_id": publication_id,
"subscription_number": subscriptions_checked,
},
)
publisher_auth = None
try:
publisher_auth = await self._get_publisher_auth(
publisher_domain, context=context
)
except DomoError as exc: # pragma: no cover - best effort logging
await logger.warning(
f"Unable to fetch auth for publisher {publisher_domain}: {exc}",
extra={
"publisher_domain": publisher_domain,
"error": str(exc),
"error_type": type(exc).__name__,
},
)
continue
except (
ValueError,
KeyError,
LookupError,
) as exc: # pragma: no cover - auth lookup failure
await logger.warning(
f"Auth credentials not found for publisher {publisher_domain}: {exc}",
extra={
"publisher_domain": publisher_domain,
"error": str(exc),
"error_type": type(exc).__name__,
},
)
continue
except (
Exception # noqa: BLE001
) as exc: # pragma: no cover - unexpected error
await logger.error(
f"❌ Unexpected error fetching auth for publisher {publisher_domain}: {exc}",
extra={
"publisher_domain": publisher_domain,
"error": str(exc),
"error_type": type(exc).__name__,
},
exc_info=True,
)
continue
if not publisher_auth:
continue
try:
content_res = await publish_routes.get_subscriber_content_details(
auth=publisher_auth,
publication_id=publication_id,
subscriber_instance=subscriber_domain,
context=context,
)
except DomoError as exc: # pragma: no cover - best effort logging
await logger.warning(
f"Unable to fetch subscriber content details for {subscription_id}: {exc}",
extra={
"subscription_id": subscription_id,
"error": str(exc),
"error_type": type(exc).__name__,
},
)
continue
except (
Exception # noqa: BLE001
) as exc: # pragma: no cover - unexpected error
await logger.error(
f"❌ Unexpected error fetching subscriber content for {subscription_id}: {exc}",
extra={
"subscription_id": subscription_id,
"error": str(exc),
"error_type": type(exc).__name__,
},
exc_info=True,
)
continue
if not (content_res.is_success and content_res.response):
await logger.debug(
f"No subscriber content details for subscription {subscription_id}",
extra={"subscription_id": subscription_id},
)
continue
for item in content_res.response:
content_type = (item.get("contentType") or "").upper()
if (
content_type in acceptable_content_types
and str(item.get("subscriberObjectId")) == target_id
):
await logger.info(
f"✅ Found {normalized_entity_type} {target_id} in subscription {subscription_id}",
extra={
"entity_type": normalized_entity_type,
"entity_id": target_id,
"subscription_id": subscription_id,
"publisher_domain": publisher_domain,
},
)
# Return raw dict - caller hydrates into DomoSubscription
return summary
# No subscription found
if self.max_subscriptions_to_check:
raise ValueError(
f"Entity {subscriber_entity_id} (type {entity_type}) is not part of "
f"any subscription after checking {subscriptions_checked} "
f"subscriptions. Try increasing max_subscriptions_to_check "
f"(currently {self.max_subscriptions_to_check})."
)
raise ValueError(
f"Entity {subscriber_entity_id} (type {entity_type}) is not part of any "
f"subscription after checking all {subscriptions_checked} subscriptions."
)
|