supaflow-python-sdk 1.0.74__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. supaflow_python_sdk/__init__.py +150 -0
  2. supaflow_python_sdk/_definition_validation.py +220 -0
  3. supaflow_python_sdk/_legacy_manifest.py +89 -0
  4. supaflow_python_sdk/_legacy_naming.py +50 -0
  5. supaflow_python_sdk/_precision_json.py +74 -0
  6. supaflow_python_sdk/base_connector.py +146 -0
  7. supaflow_python_sdk/capabilities.py +74 -0
  8. supaflow_python_sdk/catalog_naming.py +189 -0
  9. supaflow_python_sdk/child_settings.py +44 -0
  10. supaflow_python_sdk/compatibility.json +10 -0
  11. supaflow_python_sdk/connector_definition.py +279 -0
  12. supaflow_python_sdk/connector_fixture.py +13 -0
  13. supaflow_python_sdk/connector_identity.py +55 -0
  14. supaflow_python_sdk/connector_property.py +97 -0
  15. supaflow_python_sdk/connector_resource.py +388 -0
  16. supaflow_python_sdk/csv_record_emitter.py +288 -0
  17. supaflow_python_sdk/cursor_candidates.py +167 -0
  18. supaflow_python_sdk/errors.py +130 -0
  19. supaflow_python_sdk/field_capabilities.py +50 -0
  20. supaflow_python_sdk/helpers.py +98 -0
  21. supaflow_python_sdk/host_api.py +102 -0
  22. supaflow_python_sdk/job_context.py +39 -0
  23. supaflow_python_sdk/jsonl_record_emitter.py +727 -0
  24. supaflow_python_sdk/keyless_object_handling.py +19 -0
  25. supaflow_python_sdk/models.py +1052 -0
  26. supaflow_python_sdk/naming.py +267 -0
  27. supaflow_python_sdk/native_mapped_jsonl_record_emitter.py +180 -0
  28. supaflow_python_sdk/oauth.py +399 -0
  29. supaflow_python_sdk/openapi_schema_builder.py +778 -0
  30. supaflow_python_sdk/property_rules.py +27 -0
  31. supaflow_python_sdk/py.typed +0 -0
  32. supaflow_python_sdk/resource_options.py +9 -0
  33. supaflow_python_sdk/schema_compliance.py +481 -0
  34. supaflow_python_sdk/source.py +165 -0
  35. supaflow_python_sdk/source_namespace.py +325 -0
  36. supaflow_python_sdk/supa_id.py +422 -0
  37. supaflow_python_sdk/temporal_stage_value.py +136 -0
  38. supaflow_python_sdk/type_inference.py +583 -0
  39. supaflow_python_sdk/ui.py +45 -0
  40. supaflow_python_sdk/write_plan.py +112 -0
  41. supaflow_python_sdk-1.0.74.dist-info/METADATA +80 -0
  42. supaflow_python_sdk-1.0.74.dist-info/RECORD +44 -0
  43. supaflow_python_sdk-1.0.74.dist-info/WHEEL +5 -0
  44. supaflow_python_sdk-1.0.74.dist-info/top_level.txt +1 -0
@@ -0,0 +1,399 @@
1
+ """Managed OAuth definitions and credential helpers for Python connectors.
2
+
3
+ This module provides three pieces:
4
+
5
+ 1. :class:`OAuthDefinition` -- a Python mirror of the Java
6
+ :class:`io.supaflow.connector.sdk.oauth.OAuthConfig` value object. Its
7
+ :meth:`OAuthDefinition.as_dict` and :meth:`OAuthDefinition.as_json`
8
+ methods emit the same camelCase JSON shape the Java Jackson serializer
9
+ produces, so a Python connector's manifest ``OAUTH_CONFIG`` default value
10
+ round-trips cleanly through ``ObjectMapper.readValue(..., OAuthConfig.class)``.
11
+ 2. :func:`managed_oauth_properties` -- a helper that emits the standard
12
+ ``authMethod`` / ``oauthConfig`` / ``accessToken`` / ``refreshToken`` /
13
+ ``tokenExpiresAt`` property cluster used by Supaflow-managed OAuth
14
+ connectors. Drop-in equivalent to the property cluster that Java
15
+ connectors like Google Drive / Airtable / HubSpot declare via
16
+ ``@Property``.
17
+ 3. :class:`ManagedOAuthCredentials` plus :func:`require_managed_oauth_config`
18
+ and :func:`is_managed_oauth_selected` -- runtime-side helpers a Python
19
+ connector calls inside ``init`` / ``read`` to retrieve the (already
20
+ refreshed) access token from the request config.
21
+
22
+ Design notes:
23
+
24
+ * The bridge is responsible for refreshing tokens before invoking Python
25
+ operations (see ``PythonConnectorBridge.applyManagedOAuthPreflight``).
26
+ The Python helper is intentionally read-only -- it validates that the
27
+ required token fields are present and surfaces an actionable
28
+ authentication error if not.
29
+ * No helper here logs ``access_token``, ``refresh_token``, ``oauthConfig``,
30
+ or the full config map. Authors using these helpers should follow the
31
+ same rule (``ManagedOAuthCredentials.__repr__`` redacts the secrets to
32
+ prevent accidental logging via ``f"{creds}"`` / ``log.info("%s", creds)``).
33
+ """
34
+ from __future__ import annotations
35
+
36
+ import json
37
+ from dataclasses import dataclass, field
38
+ from typing import Any, Mapping, Sequence
39
+
40
+ from supaflow_python_sdk.connector_property import ConnectorProperty
41
+ from supaflow_python_sdk.errors import ErrorType, fail
42
+ from supaflow_python_sdk.ui import UiNotice
43
+
44
+
45
+ DEFAULT_AUTH_PROPERTY = "authMethod"
46
+ DEFAULT_OAUTH_CONFIG_PROPERTY = "oauthConfig"
47
+ DEFAULT_ACCESS_TOKEN_PROPERTY = "accessToken"
48
+ DEFAULT_REFRESH_TOKEN_PROPERTY = "refreshToken"
49
+ DEFAULT_TOKEN_EXPIRES_AT_PROPERTY = "tokenExpiresAt"
50
+ DEFAULT_OAUTH_VALUE = "oauth"
51
+
52
+
53
+ @dataclass(frozen=True)
54
+ class OAuthDefinition:
55
+ """Python mirror of Java ``io.supaflow.connector.sdk.oauth.OAuthConfig``.
56
+
57
+ Field names are snake_case (Python convention) but :meth:`as_dict` and
58
+ :meth:`as_json` render the Java-compatible camelCase keys consumed by the
59
+ backend OAuth callback flow. The defaults mirror Java ``OAuthConfig``'s
60
+ own defaults so unset fields still parse correctly on the Java side.
61
+
62
+ The ``token_endpoint_auth_method`` / ``refresh_endpoint_auth_method``
63
+ values must match Java ``OAuthConfig.ClientAuthMethod`` (``NONE``,
64
+ ``CLIENT_SECRET_POST``, ``CLIENT_SECRET_BASIC``).
65
+ """
66
+
67
+ authorization_url: str
68
+ token_url: str
69
+ flow_type: str = "AUTHORIZATION_CODE"
70
+ refresh_url: str | None = None
71
+ scopes: Sequence[str] = ()
72
+ scope_separator: str = " "
73
+ supports_pkce: bool = True
74
+ code_challenge_method: str = "S256"
75
+ token_endpoint_auth_method: str = "CLIENT_SECRET_POST"
76
+ refresh_endpoint_auth_method: str | None = None
77
+ supports_refresh_token: bool = True
78
+ rotates_refresh_token: bool = False
79
+ access_token_expiry_seconds: int = 3600
80
+ clock_skew_seconds: int = 120
81
+ access_token_field: str = "access_token"
82
+ refresh_token_field: str = "refresh_token"
83
+ expires_in_field: str = "expires_in"
84
+ token_type_field: str = "token_type"
85
+ authorization_params: Mapping[str, str] = field(default_factory=dict)
86
+ token_params: Mapping[str, str] = field(default_factory=dict)
87
+ capture_response_fields: Sequence[str] = ()
88
+ field_mappings: Mapping[str, str] = field(default_factory=dict)
89
+
90
+ authorization_notice: UiNotice | None = None
91
+ account_label_property: str | None = field(default=None, kw_only=True)
92
+ introspection_url: str | None = field(default=None, kw_only=True)
93
+ user_info_url: str | None = field(default=None, kw_only=True)
94
+ subject_field: str | None = field(default=None, kw_only=True)
95
+ email_field: str | None = field(default=None, kw_only=True)
96
+ subject_property: str | None = field(default=None, kw_only=True)
97
+ email_property: str | None = field(default=None, kw_only=True)
98
+
99
+ def as_dict(self) -> dict[str, Any]:
100
+ """Render this config to a Java-compatible camelCase dict.
101
+
102
+ Output keys match the ``@JsonProperty`` annotations on the Java
103
+ ``OAuthConfig`` class. Refresh URL falls back to ``token_url`` when
104
+ unset (Java side does the same in ``getRefreshUrl()``); we emit it
105
+ explicitly so the JSON round-trips identically through both
106
+ serializers.
107
+ """
108
+ refresh_url = self.refresh_url if self.refresh_url else self.token_url
109
+ refresh_auth_method = (
110
+ self.refresh_endpoint_auth_method
111
+ if self.refresh_endpoint_auth_method
112
+ else self.token_endpoint_auth_method
113
+ )
114
+ result = {
115
+ "flowType": self.flow_type,
116
+ "authorizationUrl": self.authorization_url,
117
+ "tokenUrl": self.token_url,
118
+ "refreshUrl": refresh_url,
119
+ "scopes": list(self.scopes),
120
+ "scopeSeparator": self.scope_separator,
121
+ "supportsPKCE": self.supports_pkce,
122
+ "codeChallengeMethod": self.code_challenge_method,
123
+ "tokenEndpointAuthMethod": self.token_endpoint_auth_method,
124
+ "refreshEndpointAuthMethod": refresh_auth_method,
125
+ "supportsRefreshToken": self.supports_refresh_token,
126
+ "rotatesRefreshToken": self.rotates_refresh_token,
127
+ "accessTokenExpirySeconds": self.access_token_expiry_seconds,
128
+ "clockSkewSeconds": self.clock_skew_seconds,
129
+ "accessTokenField": self.access_token_field,
130
+ "refreshTokenField": self.refresh_token_field,
131
+ "expiresInField": self.expires_in_field,
132
+ "tokenTypeField": self.token_type_field,
133
+ "captureResponseFields": list(self.capture_response_fields),
134
+ "fieldMappings": dict(self.field_mappings),
135
+ "authorizationParams": dict(self.authorization_params),
136
+ "tokenParams": dict(self.token_params),
137
+ }
138
+
139
+ if self.account_label_property is not None:
140
+ result["accountLabelProperty"] = self.account_label_property
141
+ if self.introspection_url is not None:
142
+ result["introspectionUrl"] = self.introspection_url
143
+ if self.user_info_url is not None:
144
+ result["userInfoUrl"] = self.user_info_url
145
+ if self.subject_field is not None:
146
+ result["subjectField"] = self.subject_field
147
+ if self.email_field is not None:
148
+ result["emailField"] = self.email_field
149
+ if self.subject_property is not None:
150
+ result["subjectProperty"] = self.subject_property
151
+ if self.email_property is not None:
152
+ result["emailProperty"] = self.email_property
153
+ if self.authorization_notice is not None:
154
+ result["authorizationNotice"] = self.authorization_notice.as_dict()
155
+ return result
156
+
157
+ def as_json(self) -> str:
158
+ """Render the Java-compatible fields as compact JSON in declaration order."""
159
+ return json.dumps(self.as_dict(), sort_keys=False, separators=(",", ":"))
160
+
161
+
162
+ def managed_oauth_properties(
163
+ *,
164
+ oauth_config: OAuthDefinition,
165
+ auth_property: str = DEFAULT_AUTH_PROPERTY,
166
+ oauth_config_property: str = DEFAULT_OAUTH_CONFIG_PROPERTY,
167
+ access_token_property: str = DEFAULT_ACCESS_TOKEN_PROPERTY,
168
+ refresh_token_property: str = DEFAULT_REFRESH_TOKEN_PROPERTY,
169
+ token_expires_at_property: str = DEFAULT_TOKEN_EXPIRES_AT_PROPERTY,
170
+ oauth_value: str = DEFAULT_OAUTH_VALUE,
171
+ auth_values: Sequence[str] = (DEFAULT_OAUTH_VALUE,),
172
+ default_auth_value: str | None = None,
173
+ auth_label: str = "Authentication Method",
174
+ auth_description: str = "Authentication method for this source.",
175
+ property_group: str = "Authentication",
176
+ display_order: int = 0,
177
+ ):
178
+ """Generate the standard managed-OAuth property cluster.
179
+
180
+ Returned tuple of :class:`ConnectorProperty` matches the Java connector
181
+ convention used by Google Drive, HubSpot, Airtable, and Salesforce:
182
+
183
+ * ``authMethod`` -- ENUM dropdown, visible.
184
+ * ``oauthConfig`` -- OAUTH_CONFIG, hidden, default value populated from
185
+ ``oauth_config.as_json()``. Visible only when ``authMethod == oauth``.
186
+ * ``accessToken`` -- STRING, encrypted+sensitive+hidden, managed by UI.
187
+ * ``refreshToken`` -- STRING, encrypted+sensitive+hidden, managed by UI.
188
+ * ``tokenExpiresAt`` -- STRING, hidden, managed by UI.
189
+
190
+ Connector authors append provider-specific visible credential properties
191
+ (e.g., ``serviceAccountJson``) after this cluster, gated on
192
+ ``visibility = {property: authMethod, value: <other-value>}``.
193
+ """
194
+ if default_auth_value is None:
195
+ default_auth_value = oauth_value
196
+ if not auth_values:
197
+ auth_values = (oauth_value,)
198
+ if default_auth_value not in auth_values:
199
+ # Surface a developer-facing error early; this is a manifest-time
200
+ # mistake, not a user runtime mistake.
201
+ raise ValueError(
202
+ f"default_auth_value '{default_auth_value}' is not in auth_values {list(auth_values)}"
203
+ )
204
+
205
+ oauth_visibility: Mapping[str, Any] = {
206
+ "property": auth_property,
207
+ "value": oauth_value,
208
+ }
209
+
210
+ return (
211
+ ConnectorProperty(
212
+ name=auth_property,
213
+ label=auth_label,
214
+ description=auth_description,
215
+ type="ENUM",
216
+ required=True,
217
+ enum_values=tuple(auth_values),
218
+ default_value=default_auth_value,
219
+ input_type="DROPDOWN",
220
+ property_group=property_group,
221
+ display_order=display_order,
222
+ ),
223
+ ConnectorProperty(
224
+ name=oauth_config_property,
225
+ label="OAuth Configuration",
226
+ description="OAuth authorization-code flow configuration.",
227
+ type="OAUTH_CONFIG",
228
+ required=True,
229
+ hidden=True,
230
+ property_group=property_group,
231
+ display_order=display_order + 1,
232
+ default_value=oauth_config.as_json(),
233
+ visibility=oauth_visibility,
234
+ ),
235
+ ConnectorProperty(
236
+ name=access_token_property,
237
+ label="Access Token",
238
+ description="OAuth access token, automatically managed.",
239
+ type="STRING",
240
+ required=False,
241
+ encrypted=True,
242
+ sensitive=True,
243
+ hidden=True,
244
+ property_group=property_group,
245
+ display_order=display_order + 2,
246
+ visibility=oauth_visibility,
247
+ ),
248
+ ConnectorProperty(
249
+ name=refresh_token_property,
250
+ label="Refresh Token",
251
+ description="OAuth refresh token, automatically managed.",
252
+ type="STRING",
253
+ required=False,
254
+ encrypted=True,
255
+ sensitive=True,
256
+ hidden=True,
257
+ property_group=property_group,
258
+ display_order=display_order + 3,
259
+ visibility=oauth_visibility,
260
+ ),
261
+ ConnectorProperty(
262
+ name=token_expires_at_property,
263
+ label="Token Expires At",
264
+ description="OAuth token expiration timestamp, automatically managed.",
265
+ type="STRING",
266
+ required=False,
267
+ hidden=True,
268
+ property_group=property_group,
269
+ display_order=display_order + 4,
270
+ visibility=oauth_visibility,
271
+ ),
272
+ )
273
+
274
+
275
+ @dataclass(frozen=True)
276
+ class ManagedOAuthCredentials:
277
+ """Runtime view of Supaflow-managed OAuth credentials.
278
+
279
+ Constructed by :meth:`from_config` reading the connector request's
280
+ ``config`` mapping (which is what arrives in ``init.config`` / ``read.config``
281
+ after the Java bridge has already refreshed the token). Authors must not
282
+ log instances directly; ``__repr__`` redacts the access and refresh tokens.
283
+ """
284
+
285
+ access_token: str
286
+ refresh_token: str
287
+ token_expires_at: str | None = None
288
+
289
+ def __repr__(self) -> str: # pragma: no cover -- formatting only
290
+ token_preview = "<redacted>" if self.access_token else "<missing>"
291
+ refresh_preview = "<redacted>" if self.refresh_token else "<missing>"
292
+ return (
293
+ f"ManagedOAuthCredentials(access_token={token_preview}, "
294
+ f"refresh_token={refresh_preview}, "
295
+ f"token_expires_at={self.token_expires_at!r})"
296
+ )
297
+
298
+ def get_access_token(self) -> str:
299
+ """Return the current access token. The bridge guarantees freshness."""
300
+ return self.access_token
301
+
302
+ @classmethod
303
+ def from_config(
304
+ cls,
305
+ config: Mapping[str, Any],
306
+ *,
307
+ access_token_property: str = DEFAULT_ACCESS_TOKEN_PROPERTY,
308
+ refresh_token_property: str = DEFAULT_REFRESH_TOKEN_PROPERTY,
309
+ token_expires_at_property: str = DEFAULT_TOKEN_EXPIRES_AT_PROPERTY,
310
+ ) -> "ManagedOAuthCredentials":
311
+ """Read managed OAuth credentials from a request config mapping.
312
+
313
+ Raises ``ConnectorException(ErrorType.AUTH)`` with an actionable
314
+ message when the required token fields are missing. Does not log
315
+ any token values.
316
+ """
317
+ access_token = config.get(access_token_property)
318
+ refresh_token = config.get(refresh_token_property)
319
+ token_expires_at = config.get(token_expires_at_property)
320
+
321
+ if not isinstance(access_token, str) or not access_token.strip():
322
+ fail(
323
+ ErrorType.AUTH,
324
+ "OAuth access token is missing. Reconnect this datasource via "
325
+ "the OAuth flow before running the next sync.",
326
+ code="MANAGED_OAUTH_MISSING_ACCESS_TOKEN",
327
+ )
328
+ if not isinstance(refresh_token, str) or not refresh_token.strip():
329
+ fail(
330
+ ErrorType.AUTH,
331
+ "OAuth refresh token is missing. Reconnect this datasource via "
332
+ "the OAuth flow before running the next sync.",
333
+ code="MANAGED_OAUTH_MISSING_REFRESH_TOKEN",
334
+ )
335
+
336
+ if token_expires_at is not None and not isinstance(token_expires_at, str):
337
+ token_expires_at = str(token_expires_at)
338
+
339
+ return cls(
340
+ access_token=access_token,
341
+ refresh_token=refresh_token,
342
+ token_expires_at=token_expires_at,
343
+ )
344
+
345
+
346
+ def require_managed_oauth_config(
347
+ config: Mapping[str, Any],
348
+ *,
349
+ access_token_property: str = DEFAULT_ACCESS_TOKEN_PROPERTY,
350
+ refresh_token_property: str = DEFAULT_REFRESH_TOKEN_PROPERTY,
351
+ token_expires_at_property: str = DEFAULT_TOKEN_EXPIRES_AT_PROPERTY,
352
+ ) -> ManagedOAuthCredentials:
353
+ """Convenience wrapper around :meth:`ManagedOAuthCredentials.from_config`."""
354
+ return ManagedOAuthCredentials.from_config(
355
+ config,
356
+ access_token_property=access_token_property,
357
+ refresh_token_property=refresh_token_property,
358
+ token_expires_at_property=token_expires_at_property,
359
+ )
360
+
361
+
362
+ def is_managed_oauth_selected(
363
+ config: Mapping[str, Any],
364
+ *,
365
+ auth_property: str = DEFAULT_AUTH_PROPERTY,
366
+ oauth_value: str = DEFAULT_OAUTH_VALUE,
367
+ ) -> bool:
368
+ """Return ``True`` when the user picked the managed-OAuth auth branch.
369
+
370
+ Comparison is case-insensitive on the auth value to match the Java
371
+ ``relatedPropertyNameAndValue`` semantics (which the UI feeds back as
372
+ a plain string).
373
+
374
+ Returns ``False`` when ``auth_property`` is absent. The standard
375
+ cluster from :func:`managed_oauth_properties` always emits
376
+ ``authMethod`` with a default, so a missing key means the manifest is
377
+ non-standard and we fail-closed -- matching the Java
378
+ ``PythonConnectorOAuthRefresh.isOAuthBranchSelected`` semantics so
379
+ Python connector code and the Java preflight cannot disagree.
380
+ """
381
+ raw = config.get(auth_property)
382
+ if raw is None:
383
+ return False
384
+ return str(raw).strip().lower() == oauth_value.strip().lower()
385
+
386
+
387
+ __all__ = [
388
+ "DEFAULT_ACCESS_TOKEN_PROPERTY",
389
+ "DEFAULT_AUTH_PROPERTY",
390
+ "DEFAULT_OAUTH_CONFIG_PROPERTY",
391
+ "DEFAULT_OAUTH_VALUE",
392
+ "DEFAULT_REFRESH_TOKEN_PROPERTY",
393
+ "DEFAULT_TOKEN_EXPIRES_AT_PROPERTY",
394
+ "OAuthDefinition",
395
+ "ManagedOAuthCredentials",
396
+ "is_managed_oauth_selected",
397
+ "managed_oauth_properties",
398
+ "require_managed_oauth_config",
399
+ ]