airbyte-agent-shopify 0.1.3__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 (57) hide show
  1. airbyte_agent_shopify/__init__.py +349 -0
  2. airbyte_agent_shopify/_vendored/__init__.py +1 -0
  3. airbyte_agent_shopify/_vendored/connector_sdk/__init__.py +82 -0
  4. airbyte_agent_shopify/_vendored/connector_sdk/auth_strategies.py +1173 -0
  5. airbyte_agent_shopify/_vendored/connector_sdk/auth_template.py +135 -0
  6. airbyte_agent_shopify/_vendored/connector_sdk/cloud_utils/__init__.py +5 -0
  7. airbyte_agent_shopify/_vendored/connector_sdk/cloud_utils/client.py +213 -0
  8. airbyte_agent_shopify/_vendored/connector_sdk/connector_model_loader.py +986 -0
  9. airbyte_agent_shopify/_vendored/connector_sdk/constants.py +78 -0
  10. airbyte_agent_shopify/_vendored/connector_sdk/exceptions.py +23 -0
  11. airbyte_agent_shopify/_vendored/connector_sdk/executor/__init__.py +31 -0
  12. airbyte_agent_shopify/_vendored/connector_sdk/executor/hosted_executor.py +196 -0
  13. airbyte_agent_shopify/_vendored/connector_sdk/executor/local_executor.py +1773 -0
  14. airbyte_agent_shopify/_vendored/connector_sdk/executor/models.py +190 -0
  15. airbyte_agent_shopify/_vendored/connector_sdk/extensions.py +693 -0
  16. airbyte_agent_shopify/_vendored/connector_sdk/http/__init__.py +37 -0
  17. airbyte_agent_shopify/_vendored/connector_sdk/http/adapters/__init__.py +9 -0
  18. airbyte_agent_shopify/_vendored/connector_sdk/http/adapters/httpx_adapter.py +251 -0
  19. airbyte_agent_shopify/_vendored/connector_sdk/http/config.py +98 -0
  20. airbyte_agent_shopify/_vendored/connector_sdk/http/exceptions.py +119 -0
  21. airbyte_agent_shopify/_vendored/connector_sdk/http/protocols.py +114 -0
  22. airbyte_agent_shopify/_vendored/connector_sdk/http/response.py +104 -0
  23. airbyte_agent_shopify/_vendored/connector_sdk/http_client.py +693 -0
  24. airbyte_agent_shopify/_vendored/connector_sdk/introspection.py +262 -0
  25. airbyte_agent_shopify/_vendored/connector_sdk/logging/__init__.py +11 -0
  26. airbyte_agent_shopify/_vendored/connector_sdk/logging/logger.py +273 -0
  27. airbyte_agent_shopify/_vendored/connector_sdk/logging/types.py +93 -0
  28. airbyte_agent_shopify/_vendored/connector_sdk/observability/__init__.py +11 -0
  29. airbyte_agent_shopify/_vendored/connector_sdk/observability/config.py +179 -0
  30. airbyte_agent_shopify/_vendored/connector_sdk/observability/models.py +19 -0
  31. airbyte_agent_shopify/_vendored/connector_sdk/observability/redactor.py +81 -0
  32. airbyte_agent_shopify/_vendored/connector_sdk/observability/session.py +103 -0
  33. airbyte_agent_shopify/_vendored/connector_sdk/performance/__init__.py +6 -0
  34. airbyte_agent_shopify/_vendored/connector_sdk/performance/instrumentation.py +57 -0
  35. airbyte_agent_shopify/_vendored/connector_sdk/performance/metrics.py +93 -0
  36. airbyte_agent_shopify/_vendored/connector_sdk/schema/__init__.py +75 -0
  37. airbyte_agent_shopify/_vendored/connector_sdk/schema/base.py +169 -0
  38. airbyte_agent_shopify/_vendored/connector_sdk/schema/components.py +239 -0
  39. airbyte_agent_shopify/_vendored/connector_sdk/schema/connector.py +120 -0
  40. airbyte_agent_shopify/_vendored/connector_sdk/schema/extensions.py +230 -0
  41. airbyte_agent_shopify/_vendored/connector_sdk/schema/operations.py +146 -0
  42. airbyte_agent_shopify/_vendored/connector_sdk/schema/security.py +237 -0
  43. airbyte_agent_shopify/_vendored/connector_sdk/secrets.py +182 -0
  44. airbyte_agent_shopify/_vendored/connector_sdk/telemetry/__init__.py +10 -0
  45. airbyte_agent_shopify/_vendored/connector_sdk/telemetry/config.py +32 -0
  46. airbyte_agent_shopify/_vendored/connector_sdk/telemetry/events.py +59 -0
  47. airbyte_agent_shopify/_vendored/connector_sdk/telemetry/tracker.py +155 -0
  48. airbyte_agent_shopify/_vendored/connector_sdk/types.py +254 -0
  49. airbyte_agent_shopify/_vendored/connector_sdk/utils.py +60 -0
  50. airbyte_agent_shopify/_vendored/connector_sdk/validation.py +828 -0
  51. airbyte_agent_shopify/connector.py +2985 -0
  52. airbyte_agent_shopify/connector_model.py +11641 -0
  53. airbyte_agent_shopify/models.py +1192 -0
  54. airbyte_agent_shopify/types.py +338 -0
  55. airbyte_agent_shopify-0.1.3.dist-info/METADATA +139 -0
  56. airbyte_agent_shopify-0.1.3.dist-info/RECORD +57 -0
  57. airbyte_agent_shopify-0.1.3.dist-info/WHEEL +4 -0
@@ -0,0 +1,986 @@
1
+ """Load and parse connector YAML definitions into ConnectorModel objects."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ import re
7
+ from pathlib import Path
8
+ from typing import Any
9
+ from uuid import UUID
10
+
11
+ import jsonref
12
+ import yaml
13
+ from pydantic import ValidationError
14
+
15
+ from .constants import (
16
+ OPENAPI_DEFAULT_VERSION,
17
+ OPENAPI_VERSION_PREFIX,
18
+ )
19
+
20
+ from .schema import OpenAPIConnector
21
+ from .schema.components import GraphQLBodyConfig, RequestBody
22
+ from .schema.security import AirbyteAuthConfig, AuthConfigFieldSpec
23
+ from .types import (
24
+ Action,
25
+ AuthConfig,
26
+ AuthOption,
27
+ AuthType,
28
+ ConnectorModel,
29
+ ContentType,
30
+ EndpointDefinition,
31
+ EntityDefinition,
32
+ )
33
+
34
+
35
+ class ConnectorModelLoaderError(Exception):
36
+ """Base exception for connector model loading errors."""
37
+
38
+ pass
39
+
40
+
41
+ class InvalidYAMLError(ConnectorModelLoaderError):
42
+ """Raised when YAML syntax is invalid."""
43
+
44
+ pass
45
+
46
+
47
+ class InvalidOpenAPIError(ConnectorModelLoaderError):
48
+ """Raised when OpenAPI specification is invalid."""
49
+
50
+ pass
51
+
52
+
53
+ class DuplicateEntityError(ConnectorModelLoaderError):
54
+ """Raised when duplicate entity names are detected."""
55
+
56
+ pass
57
+
58
+
59
+ class TokenExtractValidationError(ConnectorModelLoaderError):
60
+ """Raised when x-airbyte-token-extract references invalid server variables."""
61
+
62
+ pass
63
+
64
+
65
+ def extract_path_params(path: str) -> list[str]:
66
+ """Extract parameter names from path template.
67
+
68
+ Example: '/v1/customers/{id}/invoices/{invoice_id}' -> ['id', 'invoice_id']
69
+ """
70
+ return re.findall(r"\{(\w+)\}", path)
71
+
72
+
73
+ def resolve_schema_refs(schema: Any, spec_dict: dict) -> dict[str, Any]:
74
+ """Resolve all $ref references in a schema using jsonref.
75
+
76
+ This handles:
77
+ - Simple $refs to components/schemas
78
+ - Nested $refs within schemas
79
+ - Circular references (jsonref handles these gracefully)
80
+
81
+ Args:
82
+ schema: The schema that may contain $refs (can be dict or Pydantic model)
83
+ spec_dict: The full OpenAPI spec as a dict (for reference resolution)
84
+
85
+ Returns:
86
+ Resolved schema as a dictionary with all $refs replaced by their definitions
87
+ """
88
+ if not schema:
89
+ return {}
90
+
91
+ # Convert schema to dict if it's a Pydantic model
92
+ if hasattr(schema, "model_dump"):
93
+ schema_dict = schema.model_dump(by_alias=True, exclude_none=True)
94
+ elif isinstance(schema, dict):
95
+ schema_dict = schema
96
+ else:
97
+ return {}
98
+
99
+ # If there are no $refs, return as-is
100
+ if "$ref" not in str(schema_dict):
101
+ return schema_dict
102
+
103
+ # Use jsonref to resolve all references
104
+ # We need to embed the schema in the spec for proper reference resolution
105
+ temp_spec = spec_dict.copy()
106
+ temp_spec["__temp_schema__"] = schema_dict
107
+
108
+ try:
109
+ # Resolve all references
110
+ resolved_spec = jsonref.replace_refs( # type: ignore[union-attr]
111
+ temp_spec,
112
+ base_uri="",
113
+ jsonschema=True, # Use JSONSchema draft 7 semantics
114
+ lazy_load=False, # Resolve everything immediately
115
+ )
116
+
117
+ # Extract our resolved schema
118
+ resolved_schema = dict(resolved_spec.get("__temp_schema__", {}))
119
+
120
+ # Remove any remaining jsonref proxy objects by converting to plain dict
121
+ return _deproxy_schema(resolved_schema)
122
+ except (AttributeError, KeyError, RecursionError, Exception):
123
+ # If resolution fails, return the original schema
124
+ # This allows the system to continue even with malformed $refs
125
+ # AttributeError covers the case where jsonref might be None
126
+ # Exception catches jsonref.JsonRefError and other jsonref exceptions
127
+ return schema_dict
128
+
129
+
130
+ def _deproxy_schema(obj: Any) -> Any:
131
+ """Recursively convert jsonref proxy objects to plain dicts/lists.
132
+
133
+ jsonref returns proxy objects that behave like dicts but aren't actual dicts.
134
+ This converts them to plain Python objects for consistent behavior.
135
+ """
136
+ if isinstance(obj, dict) or (hasattr(obj, "__subject__") and hasattr(obj, "keys")):
137
+ # Handle both dicts and jsonref proxy objects
138
+ try:
139
+ return {str(k): _deproxy_schema(v) for k, v in obj.items()}
140
+ except (AttributeError, TypeError):
141
+ return obj
142
+ elif isinstance(obj, (list, tuple)):
143
+ return [_deproxy_schema(item) for item in obj]
144
+ else:
145
+ return obj
146
+
147
+
148
+ def parse_openapi_spec(raw_config: dict) -> OpenAPIConnector:
149
+ """Parse OpenAPI specification from YAML.
150
+
151
+ Args:
152
+ raw_config: Raw YAML configuration
153
+
154
+ Returns:
155
+ Parsed OpenAPIConnector with full validation
156
+
157
+ Raises:
158
+ InvalidOpenAPIError: If OpenAPI spec is invalid or missing required fields
159
+ """
160
+ # Validate OpenAPI version
161
+ openapi_version = raw_config.get("openapi", "")
162
+ if not openapi_version:
163
+ raise InvalidOpenAPIError("Missing required field: 'openapi' version")
164
+
165
+ # Check if version is 3.1.x (we don't support 2.x or 3.0.x)
166
+ if not openapi_version.startswith(OPENAPI_VERSION_PREFIX):
167
+ raise InvalidOpenAPIError(f"Unsupported OpenAPI version: {openapi_version}. Only {OPENAPI_VERSION_PREFIX}x is supported.")
168
+
169
+ # Validate required top-level fields
170
+ if "info" not in raw_config:
171
+ raise InvalidOpenAPIError("Missing required field: 'info'")
172
+
173
+ if "paths" not in raw_config:
174
+ raise InvalidOpenAPIError("Missing required field: 'paths'")
175
+
176
+ # Validate paths is not empty
177
+ if not raw_config["paths"]:
178
+ raise InvalidOpenAPIError("OpenAPI spec must have at least one path definition")
179
+
180
+ # Parse with Pydantic validation
181
+ try:
182
+ spec = OpenAPIConnector(**raw_config)
183
+ except ValidationError as e:
184
+ raise InvalidOpenAPIError(f"OpenAPI validation failed: {e}")
185
+
186
+ return spec
187
+
188
+
189
+ def _extract_request_body_config(
190
+ request_body: RequestBody | None, spec_dict: dict[str, Any]
191
+ ) -> tuple[list[str], dict[str, Any] | None, dict[str, Any] | None, dict[str, Any]]:
192
+ """Extract request body configuration (GraphQL or standard).
193
+
194
+ Args:
195
+ request_body: RequestBody object from OpenAPI operation
196
+ spec_dict: Full OpenAPI spec dict for $ref resolution
197
+
198
+ Returns:
199
+ Tuple of (body_fields, request_schema, graphql_body, request_body_defaults)
200
+ - body_fields: List of field names for standard JSON/form bodies
201
+ - request_schema: Resolved request schema dict (for standard bodies)
202
+ - graphql_body: GraphQL body configuration dict (for GraphQL bodies)
203
+ - request_body_defaults: Default values for request body fields
204
+ """
205
+ body_fields: list[str] = []
206
+ request_schema: dict[str, Any] | None = None
207
+ graphql_body: dict[str, Any] | None = None
208
+ request_body_defaults: dict[str, Any] = {}
209
+
210
+ if not request_body:
211
+ return body_fields, request_schema, graphql_body, request_body_defaults
212
+
213
+ # Check for GraphQL extension and extract GraphQL body configuration
214
+ if request_body.x_airbyte_body_type:
215
+ body_type_config = request_body.x_airbyte_body_type
216
+
217
+ # Check if it's GraphQL type (it's a GraphQLBodyConfig Pydantic model)
218
+ if isinstance(body_type_config, GraphQLBodyConfig):
219
+ # Convert Pydantic model to dict, excluding None values
220
+ graphql_body = body_type_config.model_dump(exclude_none=True, by_alias=False)
221
+ return body_fields, request_schema, graphql_body, request_body_defaults
222
+
223
+ # Parse standard request body
224
+ for content_type_key, media_type in request_body.content.items():
225
+ # media_type is now a MediaType object with schema_ field
226
+ schema = media_type.schema_ or {}
227
+
228
+ # Resolve all $refs in the schema using jsonref
229
+ request_schema = resolve_schema_refs(schema, spec_dict)
230
+
231
+ # Extract body field names and defaults from resolved schema
232
+ if isinstance(request_schema, dict) and "properties" in request_schema:
233
+ body_fields = list(request_schema["properties"].keys())
234
+ # Extract default values for each property
235
+ for field_name, field_schema in request_schema["properties"].items():
236
+ if isinstance(field_schema, dict) and "default" in field_schema:
237
+ request_body_defaults[field_name] = field_schema["default"]
238
+
239
+ return body_fields, request_schema, graphql_body, request_body_defaults
240
+
241
+
242
+ def convert_openapi_to_connector_model(spec: OpenAPIConnector) -> ConnectorModel:
243
+ """Convert OpenAPI spec to ConnectorModel format.
244
+
245
+ Args:
246
+ spec: OpenAPI connector specification (fully validated)
247
+
248
+ Returns:
249
+ ConnectorModel with entities and endpoints
250
+ """
251
+ # Validate x-airbyte-token-extract against server variables
252
+ _validate_token_extract(spec)
253
+
254
+ # Convert spec to dict for jsonref resolution
255
+ spec_dict = spec.model_dump(by_alias=True, exclude_none=True)
256
+
257
+ # Extract connector name and version
258
+ name = spec.info.x_airbyte_connector_name or spec.info.title.lower().replace(" ", "-")
259
+ version = spec.info.version
260
+
261
+ # Parse authentication first to get token_extract fields
262
+ auth_config = _parse_auth_from_openapi(spec)
263
+
264
+ # Extract base URL from servers - keep variable placeholders intact
265
+ # Variables will be substituted at runtime by HTTPClient using:
266
+ # - config_values: for user-provided values like subdomain
267
+ # - token_extract: for OAuth dynamic values like instance_url
268
+ # DO NOT substitute defaults here - that would prevent runtime substitution
269
+ base_url = ""
270
+ if spec.servers:
271
+ base_url = spec.servers[0].url
272
+
273
+ # Group operations by entity
274
+ entities_map: dict[str, dict[str, EndpointDefinition]] = {}
275
+
276
+ for path, path_item in spec.paths.items():
277
+ # Check each HTTP method
278
+ for method_name in ["get", "post", "put", "delete", "patch"]:
279
+ operation = getattr(path_item, method_name, None)
280
+ if not operation:
281
+ continue
282
+
283
+ # Extract entity and action from x-airbyte-entity and x-airbyte-action
284
+ entity_name = operation.x_airbyte_entity
285
+ action_name = operation.x_airbyte_action
286
+ path_override = operation.x_airbyte_path_override
287
+ record_extractor = operation.x_airbyte_record_extractor
288
+ meta_extractor = operation.x_airbyte_meta_extractor
289
+
290
+ if not entity_name:
291
+ raise InvalidOpenAPIError(
292
+ f"Missing required x-airbyte-entity in operation {method_name.upper()} {path}. All operations must specify an entity."
293
+ )
294
+
295
+ if not action_name:
296
+ raise InvalidOpenAPIError(
297
+ f"Missing required x-airbyte-action in operation {method_name.upper()} {path}. All operations must specify an action."
298
+ )
299
+
300
+ # Convert to Action enum
301
+ try:
302
+ action = Action(action_name)
303
+ except ValueError:
304
+ # Provide clear error for invalid actions
305
+ valid_actions = ", ".join([a.value for a in Action])
306
+ raise InvalidOpenAPIError(
307
+ f"Invalid action '{action_name}' in operation {method_name.upper()} {path}. Valid actions are: {valid_actions}"
308
+ )
309
+
310
+ # Determine content type
311
+ content_type = ContentType.JSON
312
+ if operation.request_body and operation.request_body.content:
313
+ if "application/x-www-form-urlencoded" in operation.request_body.content:
314
+ content_type = ContentType.FORM_URLENCODED
315
+ elif "multipart/form-data" in operation.request_body.content:
316
+ content_type = ContentType.FORM_DATA
317
+
318
+ # Extract parameters with their schemas (including defaults)
319
+ path_params: list[str] = []
320
+ path_params_schema: dict[str, dict[str, Any]] = {}
321
+ query_params: list[str] = []
322
+ query_params_schema: dict[str, dict[str, Any]] = {}
323
+ deep_object_params: list[str] = []
324
+ header_params: list[str] = []
325
+ header_params_schema: dict[str, dict[str, Any]] = {}
326
+
327
+ if operation.parameters:
328
+ for param in operation.parameters:
329
+ param_schema = param.schema_ or {}
330
+ schema_info = {
331
+ "type": param_schema.get("type", "string"),
332
+ "required": param.required or False,
333
+ "default": param_schema.get("default"),
334
+ }
335
+
336
+ if param.in_ == "path":
337
+ path_params.append(param.name)
338
+ # Path params are always required
339
+ schema_info["required"] = True
340
+ path_params_schema[param.name] = schema_info
341
+ elif param.in_ == "query":
342
+ query_params.append(param.name)
343
+ query_params_schema[param.name] = schema_info
344
+ # Check if this is a deepObject style parameter
345
+ if hasattr(param, "style") and param.style == "deepObject":
346
+ deep_object_params.append(param.name)
347
+ elif param.in_ == "header":
348
+ header_params.append(param.name)
349
+ header_params_schema[param.name] = schema_info
350
+
351
+ # Extract body fields and defaults from request schema
352
+ body_fields, request_schema, graphql_body, request_body_defaults = _extract_request_body_config(operation.request_body, spec_dict)
353
+
354
+ # Extract response schema
355
+ response_schema = None
356
+ if "200" in operation.responses:
357
+ response = operation.responses["200"]
358
+ if response.content and "application/json" in response.content:
359
+ media_type = response.content["application/json"]
360
+ schema = media_type.schema_ if media_type else {}
361
+
362
+ # Resolve all $refs in the response schema using jsonref
363
+ response_schema = resolve_schema_refs(schema, spec_dict)
364
+
365
+ # Extract file_field for download operations
366
+ file_field = getattr(operation, "x_airbyte_file_url", None)
367
+
368
+ # Extract untested flag
369
+ untested = getattr(operation, "x_airbyte_untested", None) or False
370
+
371
+ # Create endpoint definition
372
+ endpoint = EndpointDefinition(
373
+ method=method_name.upper(),
374
+ action=action,
375
+ path=path,
376
+ path_override=path_override,
377
+ record_extractor=record_extractor,
378
+ meta_extractor=meta_extractor,
379
+ description=operation.description or operation.summary,
380
+ body_fields=body_fields,
381
+ query_params=query_params,
382
+ query_params_schema=query_params_schema,
383
+ deep_object_params=deep_object_params,
384
+ path_params=path_params,
385
+ path_params_schema=path_params_schema,
386
+ header_params=header_params,
387
+ header_params_schema=header_params_schema,
388
+ request_body_defaults=request_body_defaults,
389
+ content_type=content_type,
390
+ request_schema=request_schema,
391
+ response_schema=response_schema,
392
+ graphql_body=graphql_body,
393
+ file_field=file_field,
394
+ untested=untested,
395
+ )
396
+
397
+ # Add to entities map
398
+ if entity_name not in entities_map:
399
+ entities_map[entity_name] = {}
400
+ entities_map[entity_name][action] = endpoint
401
+
402
+ # Note: No need to check for duplicate entity names - the dict structure
403
+ # automatically ensures uniqueness. If the OpenAPI spec contains duplicate
404
+ # operationIds, only the last one will be kept.
405
+
406
+ # Convert entities map to EntityDefinition list
407
+ entities = []
408
+ for entity_name, endpoints_dict in entities_map.items():
409
+ actions = list(endpoints_dict.keys())
410
+
411
+ # Get schema and stream_name from components if available
412
+ schema = None
413
+ entity_stream_name = None
414
+ if spec.components:
415
+ # Look for a schema matching the entity name
416
+ for schema_name, schema_def in spec.components.schemas.items():
417
+ if schema_def.x_airbyte_entity_name == entity_name or schema_name.lower() == entity_name.lower():
418
+ schema = schema_def.model_dump(by_alias=True)
419
+ entity_stream_name = schema_def.x_airbyte_stream_name
420
+ break
421
+
422
+ entity = EntityDefinition(
423
+ name=entity_name,
424
+ stream_name=entity_stream_name,
425
+ actions=actions,
426
+ endpoints=endpoints_dict,
427
+ schema=schema,
428
+ )
429
+ entities.append(entity)
430
+
431
+ # Extract retry config from x-airbyte-retry-config extension
432
+ retry_config = spec.info.x_airbyte_retry_config
433
+ connector_id = spec.info.x_airbyte_connector_id
434
+ if not connector_id:
435
+ raise InvalidOpenAPIError("Missing required x-airbyte-connector-id field")
436
+
437
+ # Create ConnectorModel
438
+ model = ConnectorModel(
439
+ id=connector_id,
440
+ name=name,
441
+ version=version,
442
+ base_url=base_url,
443
+ auth=auth_config,
444
+ entities=entities,
445
+ openapi_spec=spec,
446
+ retry_config=retry_config,
447
+ )
448
+
449
+ return model
450
+
451
+
452
+ def _get_attribute_flexible(obj: Any, *names: str) -> Any:
453
+ """Get attribute from object, trying multiple name variants.
454
+
455
+ Supports both snake_case and camelCase attribute names.
456
+ Returns None if no variant is found.
457
+
458
+ Args:
459
+ obj: Object to get attribute from
460
+ *names: Attribute names to try in order
461
+
462
+ Returns:
463
+ Attribute value if found, None otherwise
464
+
465
+ Example:
466
+ # Try both "refresh_url" and "refreshUrl"
467
+ url = _get_attribute_flexible(flow, "refresh_url", "refreshUrl")
468
+ """
469
+ for name in names:
470
+ value = getattr(obj, name, None)
471
+ if value is not None:
472
+ return value
473
+ return None
474
+
475
+
476
+ def _select_oauth2_flow(flows: Any) -> Any:
477
+ """Select the best OAuth2 flow from available flows.
478
+
479
+ Prefers authorizationCode (most secure for web apps), but falls back
480
+ to other flow types if not available.
481
+
482
+ Args:
483
+ flows: OAuth2 flows object from OpenAPI spec
484
+
485
+ Returns:
486
+ Selected flow object, or None if no flows available
487
+ """
488
+ # Priority order: authorizationCode > clientCredentials > password > implicit
489
+ flow_names = [
490
+ ("authorization_code", "authorizationCode"), # Preferred
491
+ ("client_credentials", "clientCredentials"), # Server-to-server
492
+ ("password", "password"), # Resource owner
493
+ ("implicit", "implicit"), # Legacy, less secure
494
+ ]
495
+
496
+ for snake_case, camel_case in flow_names:
497
+ flow = _get_attribute_flexible(flows, snake_case, camel_case)
498
+ if flow:
499
+ return flow
500
+
501
+ return None
502
+
503
+
504
+ def _parse_oauth2_config(scheme: Any) -> dict[str, str]:
505
+ """Parse OAuth2 authentication configuration from OpenAPI scheme.
506
+
507
+ Extracts configuration from standard OAuth2 flows and custom x-airbyte-token-refresh
508
+ extension for additional refresh behavior customization.
509
+
510
+ Args:
511
+ scheme: OAuth2 security scheme from OpenAPI spec
512
+
513
+ Returns:
514
+ Dictionary with OAuth2 configuration including:
515
+ - header: Authorization header name (default: "Authorization")
516
+ - prefix: Token prefix (default: "Bearer")
517
+ - refresh_url: Token refresh endpoint (from flows)
518
+ - auth_style: How to send credentials (from x-airbyte-token-refresh)
519
+ - body_format: Request encoding (from x-airbyte-token-refresh)
520
+ """
521
+ config: dict[str, str] = {
522
+ "header": "Authorization",
523
+ "prefix": "Bearer",
524
+ }
525
+
526
+ # Extract flow information for refresh_url
527
+ if scheme.flows:
528
+ flow = _select_oauth2_flow(scheme.flows)
529
+ if flow:
530
+ # Try to get refresh URL (supports both naming conventions)
531
+ refresh_url = _get_attribute_flexible(flow, "refresh_url", "refreshUrl")
532
+ if refresh_url:
533
+ config["refresh_url"] = refresh_url
534
+
535
+ # Extract custom refresh configuration from x-airbyte-token-refresh extension
536
+ # Note: x_token_refresh is a Dict[str, Any], not a Pydantic model, so use .get()
537
+ x_token_refresh = getattr(scheme, "x_token_refresh", None)
538
+ if x_token_refresh:
539
+ auth_style = x_token_refresh.get("auth_style")
540
+ if auth_style:
541
+ config["auth_style"] = auth_style
542
+
543
+ body_format = x_token_refresh.get("body_format")
544
+ if body_format:
545
+ config["body_format"] = body_format
546
+
547
+ # Extract token_extract fields from x-airbyte-token-extract extension
548
+ x_token_extract = getattr(scheme, "x_airbyte_token_extract", None)
549
+ if x_token_extract:
550
+ config["token_extract"] = x_token_extract
551
+
552
+ # Extract additional_headers from x-airbyte-auth-config extension
553
+ x_auth_config = getattr(scheme, "x_airbyte_auth_config", None)
554
+ if x_auth_config:
555
+ additional_headers = getattr(x_auth_config, "additional_headers", None)
556
+ if additional_headers:
557
+ config["additional_headers"] = additional_headers
558
+
559
+ return config
560
+
561
+
562
+ def _validate_token_extract(spec: OpenAPIConnector) -> None:
563
+ """Validate x-airbyte-token-extract against server variables.
564
+
565
+ Ensures that fields specified in x-airbyte-token-extract match defined
566
+ server variables. This catches configuration errors at load time rather
567
+ than at runtime during token refresh.
568
+
569
+ Args:
570
+ spec: OpenAPI connector specification
571
+
572
+ Raises:
573
+ TokenExtractValidationError: If token_extract fields don't match server variables
574
+ """
575
+ # Get server variables
576
+ server_variables: set[str] = set()
577
+ if spec.servers:
578
+ for server in spec.servers:
579
+ if server.variables:
580
+ server_variables.update(server.variables.keys())
581
+
582
+ # Get token_extract from security scheme
583
+ if not spec.components or not spec.components.security_schemes:
584
+ return
585
+
586
+ for scheme_name, scheme in spec.components.security_schemes.items():
587
+ if scheme.type != "oauth2":
588
+ continue
589
+
590
+ token_extract = getattr(scheme, "x_airbyte_token_extract", None)
591
+ if not token_extract:
592
+ continue
593
+
594
+ # Validate each field matches a server variable
595
+ for field in token_extract:
596
+ if field not in server_variables:
597
+ raise TokenExtractValidationError(
598
+ f"x-airbyte-token-extract field '{field}' does not match any defined "
599
+ f"server variable. Available server variables: {sorted(server_variables) or 'none'}. "
600
+ f"Please define '{{{field}}}' in your server URL and add a variable definition."
601
+ )
602
+
603
+
604
+ def _generate_default_auth_config(auth_type: AuthType) -> AirbyteAuthConfig:
605
+ """Generate default x-airbyte-auth-config for an auth type.
606
+
607
+ When x-airbyte-auth-config is not explicitly defined in the OpenAPI spec,
608
+ we generate a sensible default that maps user-friendly field names to
609
+ the auth scheme's parameters.
610
+
611
+ Args:
612
+ auth_type: The authentication type (BEARER, BASIC, API_KEY)
613
+
614
+ Returns:
615
+ Default auth config spec with properties and auth_mapping
616
+ """
617
+ if auth_type == AuthType.BEARER:
618
+ return AirbyteAuthConfig(
619
+ title=None,
620
+ description=None,
621
+ type="object",
622
+ required=["token"],
623
+ properties={
624
+ "token": AuthConfigFieldSpec(
625
+ type="string",
626
+ title="Bearer Token",
627
+ description="Authentication bearer token",
628
+ format=None,
629
+ pattern=None,
630
+ airbyte_secret=False,
631
+ default=None,
632
+ )
633
+ },
634
+ auth_mapping={"token": "${token}"},
635
+ oneOf=None,
636
+ )
637
+ elif auth_type == AuthType.BASIC:
638
+ return AirbyteAuthConfig(
639
+ title=None,
640
+ description=None,
641
+ type="object",
642
+ required=["username", "password"],
643
+ properties={
644
+ "username": AuthConfigFieldSpec(
645
+ type="string",
646
+ title="Username",
647
+ description="Authentication username",
648
+ format=None,
649
+ pattern=None,
650
+ airbyte_secret=False,
651
+ default=None,
652
+ ),
653
+ "password": AuthConfigFieldSpec(
654
+ type="string",
655
+ title="Password",
656
+ description="Authentication password",
657
+ format=None,
658
+ pattern=None,
659
+ airbyte_secret=False,
660
+ default=None,
661
+ ),
662
+ },
663
+ auth_mapping={"username": "${username}", "password": "${password}"},
664
+ oneOf=None,
665
+ )
666
+ elif auth_type == AuthType.API_KEY:
667
+ return AirbyteAuthConfig(
668
+ title=None,
669
+ description=None,
670
+ type="object",
671
+ required=["api_key"],
672
+ properties={
673
+ "api_key": AuthConfigFieldSpec(
674
+ type="string",
675
+ title="API Key",
676
+ description="API authentication key",
677
+ format=None,
678
+ pattern=None,
679
+ airbyte_secret=False,
680
+ default=None,
681
+ )
682
+ },
683
+ auth_mapping={"api_key": "${api_key}"},
684
+ oneOf=None,
685
+ )
686
+ elif auth_type == AuthType.OAUTH2:
687
+ # OAuth2: No fields are strictly required to support both modes:
688
+ # 1. Full token mode: user provides access_token (and optionally refresh credentials)
689
+ # 2. Refresh-token-only mode: user provides refresh_token, client_id, client_secret
690
+ # The auth_mapping includes all fields, but apply_auth_mapping
691
+ # will skip mappings for fields not provided by the user.
692
+ return AirbyteAuthConfig(
693
+ title=None,
694
+ description=None,
695
+ type="object",
696
+ required=[],
697
+ properties={
698
+ "access_token": AuthConfigFieldSpec(
699
+ type="string",
700
+ title="Access Token",
701
+ description="OAuth2 access token",
702
+ format=None,
703
+ pattern=None,
704
+ airbyte_secret=False,
705
+ default=None,
706
+ ),
707
+ "refresh_token": AuthConfigFieldSpec(
708
+ type="string",
709
+ title="Refresh Token",
710
+ description="OAuth2 refresh token (optional)",
711
+ format=None,
712
+ pattern=None,
713
+ airbyte_secret=False,
714
+ default=None,
715
+ ),
716
+ "client_id": AuthConfigFieldSpec(
717
+ type="string",
718
+ title="Client ID",
719
+ description="OAuth2 client ID (optional)",
720
+ format=None,
721
+ pattern=None,
722
+ airbyte_secret=False,
723
+ default=None,
724
+ ),
725
+ "client_secret": AuthConfigFieldSpec(
726
+ type="string",
727
+ title="Client Secret",
728
+ description="OAuth2 client secret (optional)",
729
+ format=None,
730
+ pattern=None,
731
+ airbyte_secret=False,
732
+ default=None,
733
+ ),
734
+ },
735
+ auth_mapping={
736
+ "access_token": "${access_token}",
737
+ "refresh_token": "${refresh_token}",
738
+ "client_id": "${client_id}",
739
+ "client_secret": "${client_secret}",
740
+ },
741
+ oneOf=None,
742
+ )
743
+ else:
744
+ # Unknown auth type - return minimal config
745
+ return AirbyteAuthConfig(
746
+ title=None,
747
+ description=None,
748
+ type="object",
749
+ required=None,
750
+ properties={},
751
+ auth_mapping={},
752
+ oneOf=None,
753
+ )
754
+
755
+
756
+ def _parse_auth_from_openapi(spec: OpenAPIConnector) -> AuthConfig:
757
+ """Parse authentication configuration from OpenAPI spec.
758
+
759
+ Supports both single and multiple security schemes. For backwards compatibility,
760
+ single-scheme connectors continue to use the legacy AuthConfig format.
761
+ If no security schemes are defined, generates a default Bearer auth config.
762
+
763
+ Args:
764
+ spec: OpenAPI connector specification
765
+
766
+ Returns:
767
+ AuthConfig with either single or multiple auth options
768
+ """
769
+ if not spec.components or not spec.components.security_schemes:
770
+ # Backwards compatibility: generate default Bearer auth when no schemes defined
771
+ default_config = _generate_default_auth_config(AuthType.BEARER)
772
+ return AuthConfig(
773
+ type=AuthType.BEARER,
774
+ config={"header": "Authorization", "prefix": "Bearer"},
775
+ user_config_spec=default_config,
776
+ options=None,
777
+ )
778
+
779
+ schemes = spec.components.security_schemes
780
+
781
+ # Single scheme: backwards compatible mode
782
+ if len(schemes) == 1:
783
+ scheme_name, scheme = next(iter(schemes.items()))
784
+ return _parse_single_security_scheme(scheme)
785
+
786
+ # Multiple schemes: new multi-auth mode
787
+ options = []
788
+ for scheme_name, scheme in schemes.items():
789
+ try:
790
+ auth_option = _parse_security_scheme_to_option(scheme_name, scheme)
791
+ options.append(auth_option)
792
+ except Exception as e:
793
+ # Log warning but continue - skip invalid schemes
794
+ logger = logging.getLogger(__name__)
795
+ logger.warning(f"Skipping invalid security scheme '{scheme_name}': {e}")
796
+ continue
797
+
798
+ if not options:
799
+ raise InvalidOpenAPIError("No valid security schemes found. Connector must define at least one valid security scheme.")
800
+
801
+ return AuthConfig(
802
+ type=None,
803
+ config={},
804
+ user_config_spec=None,
805
+ options=options,
806
+ )
807
+
808
+
809
+ def _parse_single_security_scheme(scheme: Any) -> AuthConfig:
810
+ """Parse a single security scheme into AuthConfig.
811
+
812
+ This extracts the existing single-scheme parsing logic for reuse.
813
+
814
+ Args:
815
+ scheme: SecurityScheme from OpenAPI spec
816
+
817
+ Returns:
818
+ AuthConfig in single-auth mode
819
+ """
820
+ auth_type = AuthType.API_KEY # Default
821
+ auth_config = {}
822
+
823
+ if scheme.type == "http":
824
+ if scheme.scheme == "bearer":
825
+ auth_type = AuthType.BEARER
826
+ auth_config = {"header": "Authorization", "prefix": "Bearer"}
827
+ elif scheme.scheme == "basic":
828
+ auth_type = AuthType.BASIC
829
+ auth_config = {}
830
+
831
+ elif scheme.type == "apiKey":
832
+ auth_type = AuthType.API_KEY
833
+ auth_config = {
834
+ "header": scheme.name or "Authorization",
835
+ "in": scheme.in_ or "header",
836
+ }
837
+
838
+ elif scheme.type == "oauth2":
839
+ # Parse OAuth2 configuration
840
+ oauth2_config = _parse_oauth2_config(scheme)
841
+ # Use explicit x-airbyte-auth-config if present, otherwise generate default
842
+ auth_config_obj = scheme.x_airbyte_auth_config or _generate_default_auth_config(AuthType.OAUTH2)
843
+ return AuthConfig(
844
+ type=AuthType.OAUTH2,
845
+ config=oauth2_config,
846
+ user_config_spec=auth_config_obj,
847
+ options=None,
848
+ )
849
+
850
+ # Use explicit x-airbyte-auth-config if present, otherwise generate default
851
+ auth_config_obj = scheme.x_airbyte_auth_config or _generate_default_auth_config(auth_type)
852
+
853
+ return AuthConfig(
854
+ type=auth_type,
855
+ config=auth_config,
856
+ user_config_spec=auth_config_obj,
857
+ options=None,
858
+ )
859
+
860
+
861
+ def _parse_security_scheme_to_option(scheme_name: str, scheme: Any) -> AuthOption:
862
+ """Parse a security scheme into an AuthOption for multi-auth connectors.
863
+
864
+ Args:
865
+ scheme_name: Name of the security scheme (e.g., "githubOAuth")
866
+ scheme: SecurityScheme from OpenAPI spec
867
+
868
+ Returns:
869
+ AuthOption containing the parsed configuration
870
+
871
+ Raises:
872
+ ValueError: If scheme is invalid or unsupported
873
+ """
874
+ # Parse using existing single-scheme logic
875
+ single_auth = _parse_single_security_scheme(scheme)
876
+
877
+ # Convert to AuthOption
878
+ return AuthOption(
879
+ scheme_name=scheme_name,
880
+ type=single_auth.type,
881
+ config=single_auth.config,
882
+ user_config_spec=single_auth.user_config_spec,
883
+ )
884
+
885
+
886
+ def load_connector_model(definition_path: str | Path) -> ConnectorModel:
887
+ """Load connector model from YAML definition file.
888
+
889
+ Supports both OpenAPI 3.1 format and legacy format.
890
+
891
+ Args:
892
+ definition_path: Path to connector.yaml file
893
+
894
+ Returns:
895
+ Parsed ConnectorModel
896
+
897
+ Raises:
898
+ FileNotFoundError: If definition file doesn't exist
899
+ ValueError: If YAML is invalid
900
+ """
901
+ definition_path = Path(definition_path)
902
+
903
+ if not definition_path.exists():
904
+ raise FileNotFoundError(f"Connector definition not found: {definition_path}")
905
+
906
+ # Load YAML with error handling
907
+ try:
908
+ with open(definition_path) as f:
909
+ raw_definition = yaml.safe_load(f)
910
+ except yaml.YAMLError as e:
911
+ raise InvalidYAMLError(f"Invalid YAML syntax in {definition_path}: {e}")
912
+ except Exception as e:
913
+ raise ConnectorModelLoaderError(f"Error reading definition file {definition_path}: {e}")
914
+
915
+ if not raw_definition:
916
+ raise ValueError("Invalid connector.yaml: empty file")
917
+
918
+ # Detect format: OpenAPI if 'openapi' key exists
919
+ if "openapi" in raw_definition:
920
+ spec = parse_openapi_spec(raw_definition)
921
+ return convert_openapi_to_connector_model(spec)
922
+
923
+ # Legacy format
924
+ if "connector" not in raw_definition:
925
+ raise ValueError("Invalid connector.yaml: missing 'connector' or 'openapi' key")
926
+
927
+ # Parse connector metadata
928
+ connector_meta = raw_definition["connector"]
929
+
930
+ # Parse auth config
931
+ auth_config = raw_definition.get("auth", {})
932
+
933
+ # Parse entities
934
+ entities = []
935
+ for entity_data in raw_definition.get("entities", []):
936
+ # Parse endpoints for each action
937
+ endpoints_dict = {}
938
+ for action_str in entity_data.get("actions", []):
939
+ action = Action(action_str)
940
+ endpoint_data = entity_data["endpoints"].get(action_str)
941
+
942
+ if endpoint_data:
943
+ # Extract path parameters from the path template
944
+ path_params = extract_path_params(endpoint_data["path"])
945
+
946
+ endpoint = EndpointDefinition(
947
+ method=endpoint_data["method"],
948
+ path=endpoint_data["path"],
949
+ description=endpoint_data.get("description"),
950
+ body_fields=endpoint_data.get("body_fields", []),
951
+ query_params=endpoint_data.get("query_params", []),
952
+ path_params=path_params,
953
+ graphql_body=None, # GraphQL only supported in OpenAPI format (via x-airbyte-body-type)
954
+ )
955
+ endpoints_dict[action] = endpoint
956
+
957
+ entity = EntityDefinition(
958
+ name=entity_data["name"],
959
+ actions=[Action(a) for a in entity_data["actions"]],
960
+ endpoints=endpoints_dict,
961
+ schema=entity_data.get("schema"),
962
+ )
963
+ entities.append(entity)
964
+
965
+ # Get connector ID
966
+ connector_id_value = connector_meta.get("id")
967
+ if connector_id_value:
968
+ # Try to parse as UUID (handles string UUIDs)
969
+ if isinstance(connector_id_value, str):
970
+ connector_id = UUID(connector_id_value)
971
+ else:
972
+ connector_id = connector_id_value
973
+ else:
974
+ raise ValueError
975
+
976
+ # Build ConnectorModel
977
+ model = ConnectorModel(
978
+ id=connector_id,
979
+ name=connector_meta["name"],
980
+ version=connector_meta.get("version", OPENAPI_DEFAULT_VERSION),
981
+ base_url=raw_definition.get("base_url", connector_meta.get("base_url", "")),
982
+ auth=auth_config,
983
+ entities=entities,
984
+ )
985
+
986
+ return model