Skip to content

Commit 7b148ce

Browse files
committed
Add use_check_availability to CheckDynamicStream
1 parent f59cd42 commit 7b148ce

File tree

6 files changed

+119
-68
lines changed

6 files changed

+119
-68
lines changed

airbyte_cdk/sources/declarative/checks/check_dynamic_stream.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class CheckDynamicStream(ConnectionChecker):
2323

2424
stream_count: int
2525
parameters: InitVar[Mapping[str, Any]]
26+
use_check_availability: bool = True
2627

2728
def __post_init__(self, parameters: Mapping[str, Any]) -> None:
2829
self._parameters = parameters
@@ -31,21 +32,23 @@ def check_connection(
3132
self, source: AbstractSource, logger: logging.Logger, config: Mapping[str, Any]
3233
) -> Tuple[bool, Any]:
3334
streams = source.streams(config=config)
35+
3436
if len(streams) == 0:
3537
return False, f"No streams to connect to from source {source}"
38+
if not self.use_check_availability:
39+
return True, None
40+
41+
availability_strategy = HttpAvailabilityStrategy()
3642

37-
for stream_index in range(min(self.stream_count, len(streams))):
38-
stream = streams[stream_index]
39-
availability_strategy = HttpAvailabilityStrategy()
40-
try:
41-
stream_is_available, reason = availability_strategy.check_availability(
42-
stream, logger
43-
)
43+
try:
44+
for stream in streams[:min(self.stream_count, len(streams))]:
45+
stream_is_available, reason = availability_strategy.check_availability(stream, logger)
4446
if not stream_is_available:
47+
logger.warning(f"Stream {stream.name} is not available: {reason}")
4548
return False, reason
46-
except Exception as error:
47-
logger.error(
48-
f"Encountered an error trying to connect to stream {stream.name}. Error: \n {traceback.format_exc()}"
49-
)
50-
return False, f"Unable to connect to stream {stream.name} - {error}"
49+
except Exception as error:
50+
error_message = f"Encountered an error trying to connect to stream {stream.name}. Error: {error}"
51+
logger.error(error_message, exc_info=True)
52+
return False, error_message
53+
5154
return True, None

airbyte_cdk/sources/declarative/declarative_component_schema.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ definitions:
320320
title: Stream Count
321321
description: Numbers of the streams to try reading from when running a check operation.
322322
type: integer
323+
use_check_availability:
324+
title: Use Check Availability
325+
description: Enables stream check availability. This field is automatically set by the CDK.
326+
type: boolean
327+
default: true
323328
CompositeErrorHandler:
324329
title: Composite Error Handler
325330
description: Error handler that sequentially iterates over a list of error handlers.

airbyte_cdk/sources/declarative/models/declarative_component_schema.py

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class CheckDynamicStream(BaseModel):
5959
description="Numbers of the streams to try reading from when running a check operation.",
6060
title="Stream Count",
6161
)
62+
use_check_availability: Optional[bool] = Field(
63+
True,
64+
description="Enables stream check availability. This field is automatically set by the CDK.",
65+
title="Use Check Availability",
66+
)
6267

6368

6469
class ConcurrencyLevel(BaseModel):
@@ -604,7 +609,9 @@ class OAuthAuthenticator(BaseModel):
604609
scopes: Optional[List[str]] = Field(
605610
None,
606611
description="List of scopes that should be granted to the access token.",
607-
examples=[["crm.list.read", "crm.objects.contacts.read", "crm.schema.contacts.read"]],
612+
examples=[
613+
["crm.list.read", "crm.objects.contacts.read", "crm.schema.contacts.read"]
614+
],
608615
title="Scopes",
609616
)
610617
token_expiry_date: Optional[str] = Field(
@@ -1040,24 +1047,28 @@ class OAuthConfigSpecification(BaseModel):
10401047
class Config:
10411048
extra = Extra.allow
10421049

1043-
oauth_user_input_from_connector_config_specification: Optional[Dict[str, Any]] = Field(
1044-
None,
1045-
description="OAuth specific blob. This is a Json Schema used to validate Json configurations used as input to OAuth.\nMust be a valid non-nested JSON that refers to properties from ConnectorSpecification.connectionSpecification\nusing special annotation 'path_in_connector_config'.\nThese are input values the user is entering through the UI to authenticate to the connector, that might also shared\nas inputs for syncing data via the connector.\nExamples:\nif no connector values is shared during oauth flow, oauth_user_input_from_connector_config_specification=[]\nif connector values such as 'app_id' inside the top level are used to generate the API url for the oauth flow,\n oauth_user_input_from_connector_config_specification={\n app_id: {\n type: string\n path_in_connector_config: ['app_id']\n }\n }\nif connector values such as 'info.app_id' nested inside another object are used to generate the API url for the oauth flow,\n oauth_user_input_from_connector_config_specification={\n app_id: {\n type: string\n path_in_connector_config: ['info', 'app_id']\n }\n }",
1046-
examples=[
1047-
{"app_id": {"type": "string", "path_in_connector_config": ["app_id"]}},
1048-
{
1049-
"app_id": {
1050-
"type": "string",
1051-
"path_in_connector_config": ["info", "app_id"],
1052-
}
1053-
},
1054-
],
1055-
title="OAuth user input",
1050+
oauth_user_input_from_connector_config_specification: Optional[Dict[str, Any]] = (
1051+
Field(
1052+
None,
1053+
description="OAuth specific blob. This is a Json Schema used to validate Json configurations used as input to OAuth.\nMust be a valid non-nested JSON that refers to properties from ConnectorSpecification.connectionSpecification\nusing special annotation 'path_in_connector_config'.\nThese are input values the user is entering through the UI to authenticate to the connector, that might also shared\nas inputs for syncing data via the connector.\nExamples:\nif no connector values is shared during oauth flow, oauth_user_input_from_connector_config_specification=[]\nif connector values such as 'app_id' inside the top level are used to generate the API url for the oauth flow,\n oauth_user_input_from_connector_config_specification={\n app_id: {\n type: string\n path_in_connector_config: ['app_id']\n }\n }\nif connector values such as 'info.app_id' nested inside another object are used to generate the API url for the oauth flow,\n oauth_user_input_from_connector_config_specification={\n app_id: {\n type: string\n path_in_connector_config: ['info', 'app_id']\n }\n }",
1054+
examples=[
1055+
{"app_id": {"type": "string", "path_in_connector_config": ["app_id"]}},
1056+
{
1057+
"app_id": {
1058+
"type": "string",
1059+
"path_in_connector_config": ["info", "app_id"],
1060+
}
1061+
},
1062+
],
1063+
title="OAuth user input",
1064+
)
10561065
)
1057-
oauth_connector_input_specification: Optional[OauthConnectorInputSpecification] = Field(
1058-
None,
1059-
description='The DeclarativeOAuth specific blob.\nPertains to the fields defined by the connector relating to the OAuth flow.\n\nInterpolation capabilities:\n- The variables placeholders are declared as `{{my_var}}`.\n- The nested resolution variables like `{{ {{my_nested_var}} }}` is allowed as well.\n\n- The allowed interpolation context is:\n + base64Encoder - encode to `base64`, {{ {{my_var_a}}:{{my_var_b}} | base64Encoder }}\n + base64Decorer - decode from `base64` encoded string, {{ {{my_string_variable_or_string_value}} | base64Decoder }}\n + urlEncoder - encode the input string to URL-like format, {{ https://test.host.com/endpoint | urlEncoder}}\n + urlDecorer - decode the input url-encoded string into text format, {{ urlDecoder:https%3A%2F%2Fairbyte.io | urlDecoder}}\n + codeChallengeS256 - get the `codeChallenge` encoded value to provide additional data-provider specific authorisation values, {{ {{state_value}} | codeChallengeS256 }}\n\nExamples:\n - The TikTok Marketing DeclarativeOAuth spec:\n {\n "oauth_connector_input_specification": {\n "type": "object",\n "additionalProperties": false,\n "properties": {\n "consent_url": "https://ads.tiktok.com/marketing_api/auth?{{client_id_key}}={{client_id_value}}&{{redirect_uri_key}}={{ {{redirect_uri_value}} | urlEncoder}}&{{state_key}}={{state_value}}",\n "access_token_url": "https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/",\n "access_token_params": {\n "{{ auth_code_key }}": "{{ auth_code_value }}",\n "{{ client_id_key }}": "{{ client_id_value }}",\n "{{ client_secret_key }}": "{{ client_secret_value }}"\n },\n "access_token_headers": {\n "Content-Type": "application/json",\n "Accept": "application/json"\n },\n "extract_output": ["data.access_token"],\n "client_id_key": "app_id",\n "client_secret_key": "secret",\n "auth_code_key": "auth_code"\n }\n }\n }',
1060-
title="DeclarativeOAuth Connector Specification",
1066+
oauth_connector_input_specification: Optional[OauthConnectorInputSpecification] = (
1067+
Field(
1068+
None,
1069+
description='The DeclarativeOAuth specific blob.\nPertains to the fields defined by the connector relating to the OAuth flow.\n\nInterpolation capabilities:\n- The variables placeholders are declared as `{{my_var}}`.\n- The nested resolution variables like `{{ {{my_nested_var}} }}` is allowed as well.\n\n- The allowed interpolation context is:\n + base64Encoder - encode to `base64`, {{ {{my_var_a}}:{{my_var_b}} | base64Encoder }}\n + base64Decorer - decode from `base64` encoded string, {{ {{my_string_variable_or_string_value}} | base64Decoder }}\n + urlEncoder - encode the input string to URL-like format, {{ https://test.host.com/endpoint | urlEncoder}}\n + urlDecorer - decode the input url-encoded string into text format, {{ urlDecoder:https%3A%2F%2Fairbyte.io | urlDecoder}}\n + codeChallengeS256 - get the `codeChallenge` encoded value to provide additional data-provider specific authorisation values, {{ {{state_value}} | codeChallengeS256 }}\n\nExamples:\n - The TikTok Marketing DeclarativeOAuth spec:\n {\n "oauth_connector_input_specification": {\n "type": "object",\n "additionalProperties": false,\n "properties": {\n "consent_url": "https://ads.tiktok.com/marketing_api/auth?{{client_id_key}}={{client_id_value}}&{{redirect_uri_key}}={{ {{redirect_uri_value}} | urlEncoder}}&{{state_key}}={{state_value}}",\n "access_token_url": "https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/",\n "access_token_params": {\n "{{ auth_code_key }}": "{{ auth_code_value }}",\n "{{ client_id_key }}": "{{ client_id_value }}",\n "{{ client_secret_key }}": "{{ client_secret_value }}"\n },\n "access_token_headers": {\n "Content-Type": "application/json",\n "Accept": "application/json"\n },\n "extract_output": ["data.access_token"],\n "client_id_key": "app_id",\n "client_secret_key": "secret",\n "auth_code_key": "auth_code"\n }\n }\n }',
1070+
title="DeclarativeOAuth Connector Specification",
1071+
)
10611072
)
10621073
complete_oauth_output_specification: Optional[Dict[str, Any]] = Field(
10631074
None,
@@ -1075,7 +1086,9 @@ class Config:
10751086
complete_oauth_server_input_specification: Optional[Dict[str, Any]] = Field(
10761087
None,
10771088
description="OAuth specific blob. This is a Json Schema used to validate Json configurations persisted as Airbyte Server configurations.\nMust be a valid non-nested JSON describing additional fields configured by the Airbyte Instance or Workspace Admins to be used by the\nserver when completing an OAuth flow (typically exchanging an auth code for refresh token).\nExamples:\n complete_oauth_server_input_specification={\n client_id: {\n type: string\n },\n client_secret: {\n type: string\n }\n }",
1078-
examples=[{"client_id": {"type": "string"}, "client_secret": {"type": "string"}}],
1089+
examples=[
1090+
{"client_id": {"type": "string"}, "client_secret": {"type": "string"}}
1091+
],
10791092
title="OAuth input specification",
10801093
)
10811094
complete_oauth_server_output_specification: Optional[Dict[str, Any]] = Field(
@@ -1661,7 +1674,9 @@ class RecordSelector(BaseModel):
16611674
description="Responsible for filtering records to be emitted by the Source.",
16621675
title="Record Filter",
16631676
)
1664-
schema_normalization: Optional[Union[SchemaNormalization, CustomSchemaNormalization]] = Field(
1677+
schema_normalization: Optional[
1678+
Union[SchemaNormalization, CustomSchemaNormalization]
1679+
] = Field(
16651680
SchemaNormalization.None_,
16661681
description="Responsible for normalization according to the schema.",
16671682
title="Schema Normalization",
@@ -1835,12 +1850,16 @@ class Config:
18351850
description="Component used to coordinate how records are extracted across stream slices and request pages.",
18361851
title="Retriever",
18371852
)
1838-
incremental_sync: Optional[Union[CustomIncrementalSync, DatetimeBasedCursor]] = Field(
1839-
None,
1840-
description="Component used to fetch data incrementally based on a time field in the data.",
1841-
title="Incremental Sync",
1853+
incremental_sync: Optional[Union[CustomIncrementalSync, DatetimeBasedCursor]] = (
1854+
Field(
1855+
None,
1856+
description="Component used to fetch data incrementally based on a time field in the data.",
1857+
title="Incremental Sync",
1858+
)
1859+
)
1860+
name: Optional[str] = Field(
1861+
"", description="The stream name.", example=["Users"], title="Name"
18421862
)
1843-
name: Optional[str] = Field("", description="The stream name.", example=["Users"], title="Name")
18441863
primary_key: Optional[PrimaryKey] = Field(
18451864
"", description="The primary key of the stream.", title="Primary Key"
18461865
)
@@ -2112,7 +2131,11 @@ class SimpleRetriever(BaseModel):
21122131
CustomPartitionRouter,
21132132
ListPartitionRouter,
21142133
SubstreamPartitionRouter,
2115-
List[Union[CustomPartitionRouter, ListPartitionRouter, SubstreamPartitionRouter]],
2134+
List[
2135+
Union[
2136+
CustomPartitionRouter, ListPartitionRouter, SubstreamPartitionRouter
2137+
]
2138+
],
21162139
]
21172140
] = Field(
21182141
[],
@@ -2156,7 +2179,9 @@ class AsyncRetriever(BaseModel):
21562179
)
21572180
download_extractor: Optional[
21582181
Union[CustomRecordExtractor, DpathExtractor, ResponseToFileExtractor]
2159-
] = Field(None, description="Responsible for fetching the records from provided urls.")
2182+
] = Field(
2183+
None, description="Responsible for fetching the records from provided urls."
2184+
)
21602185
creation_requester: Union[CustomRequester, HttpRequester] = Field(
21612186
...,
21622187
description="Requester component that describes how to prepare HTTP requests to send to the source API to create the async server-side job.",
@@ -2190,7 +2215,11 @@ class AsyncRetriever(BaseModel):
21902215
CustomPartitionRouter,
21912216
ListPartitionRouter,
21922217
SubstreamPartitionRouter,
2193-
List[Union[CustomPartitionRouter, ListPartitionRouter, SubstreamPartitionRouter]],
2218+
List[
2219+
Union[
2220+
CustomPartitionRouter, ListPartitionRouter, SubstreamPartitionRouter
2221+
]
2222+
],
21942223
]
21952224
] = Field(
21962225
[],
@@ -2258,10 +2287,12 @@ class DynamicDeclarativeStream(BaseModel):
22582287
stream_template: DeclarativeStream = Field(
22592288
..., description="Reference to the stream template.", title="Stream Template"
22602289
)
2261-
components_resolver: Union[HttpComponentsResolver, ConfigComponentsResolver] = Field(
2262-
...,
2263-
description="Component resolve and populates stream templates with components values.",
2264-
title="Components Resolver",
2290+
components_resolver: Union[HttpComponentsResolver, ConfigComponentsResolver] = (
2291+
Field(
2292+
...,
2293+
description="Component resolve and populates stream templates with components values.",
2294+
title="Components Resolver",
2295+
)
22652296
)
22662297

22672298

airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,11 @@ def create_check_stream(model: CheckStreamModel, config: Config, **kwargs: Any)
901901
def create_check_dynamic_stream(
902902
model: CheckDynamicStreamModel, config: Config, **kwargs: Any
903903
) -> CheckDynamicStream:
904-
return CheckDynamicStream(stream_count=model.stream_count, parameters={})
904+
assert model.use_check_availability is not None # for mypy
905+
906+
use_check_availability = model.use_check_availability
907+
908+
return CheckDynamicStream(stream_count=model.stream_count, use_check_availability=use_check_availability, parameters={})
905909

906910
def create_composite_error_handler(
907911
self, model: CompositeErrorHandlerModel, config: Config, **kwargs: Any

airbyte_cdk/sources/declarative/requesters/error_handlers/http_response_filter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,16 @@ def _create_error_message(self, response: requests.Response) -> Optional[str]:
151151
:param response: The HTTP response which can be used during interpolation
152152
:return: The evaluated error message string to be emitted
153153
"""
154-
return self.error_message.eval( # type: ignore [no-any-return, union-attr]
154+
return self.error_message.eval( # type: ignore[no-any-return, union-attr]
155155
self.config, response=self._safe_response_json(response), headers=response.headers
156156
)
157157

158158
def _response_matches_predicate(self, response: requests.Response) -> bool:
159159
return (
160160
bool(
161-
self.predicate.condition # type: ignore [union-attr]
162-
and self.predicate.eval( # type: ignore [union-attr]
163-
None, # type: ignore [arg-type]
161+
self.predicate.condition # type:ignore[union-attr]
162+
and self.predicate.eval( # type:ignore[union-attr]
163+
None, # type: ignore[arg-type]
164164
response=self._safe_response_json(response),
165165
headers=response.headers,
166166
)

0 commit comments

Comments
 (0)