airbyte-cdk 0.0.0.dev0__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 (368) hide show
  1. airbyte_cdk/__init__.py +358 -0
  2. airbyte_cdk/cli/__init__.py +1 -0
  3. airbyte_cdk/cli/source_declarative_manifest/__init__.py +5 -0
  4. airbyte_cdk/cli/source_declarative_manifest/_run.py +236 -0
  5. airbyte_cdk/cli/source_declarative_manifest/spec.json +17 -0
  6. airbyte_cdk/config_observation.py +104 -0
  7. airbyte_cdk/connector.py +123 -0
  8. airbyte_cdk/connector_builder/README.md +53 -0
  9. airbyte_cdk/connector_builder/__init__.py +3 -0
  10. airbyte_cdk/connector_builder/connector_builder_handler.py +121 -0
  11. airbyte_cdk/connector_builder/main.py +107 -0
  12. airbyte_cdk/connector_builder/models.py +73 -0
  13. airbyte_cdk/connector_builder/test_reader/__init__.py +7 -0
  14. airbyte_cdk/connector_builder/test_reader/helpers.py +689 -0
  15. airbyte_cdk/connector_builder/test_reader/message_grouper.py +173 -0
  16. airbyte_cdk/connector_builder/test_reader/reader.py +441 -0
  17. airbyte_cdk/connector_builder/test_reader/types.py +83 -0
  18. airbyte_cdk/destinations/__init__.py +8 -0
  19. airbyte_cdk/destinations/destination.py +154 -0
  20. airbyte_cdk/destinations/vector_db_based/README.md +37 -0
  21. airbyte_cdk/destinations/vector_db_based/__init__.py +38 -0
  22. airbyte_cdk/destinations/vector_db_based/config.py +298 -0
  23. airbyte_cdk/destinations/vector_db_based/document_processor.py +223 -0
  24. airbyte_cdk/destinations/vector_db_based/embedder.py +303 -0
  25. airbyte_cdk/destinations/vector_db_based/indexer.py +78 -0
  26. airbyte_cdk/destinations/vector_db_based/test_utils.py +63 -0
  27. airbyte_cdk/destinations/vector_db_based/utils.py +35 -0
  28. airbyte_cdk/destinations/vector_db_based/writer.py +104 -0
  29. airbyte_cdk/entrypoint.py +414 -0
  30. airbyte_cdk/exception_handler.py +56 -0
  31. airbyte_cdk/logger.py +109 -0
  32. airbyte_cdk/models/__init__.py +72 -0
  33. airbyte_cdk/models/airbyte_protocol.py +88 -0
  34. airbyte_cdk/models/airbyte_protocol_serializers.py +44 -0
  35. airbyte_cdk/models/well_known_types.py +5 -0
  36. airbyte_cdk/py.typed +0 -0
  37. airbyte_cdk/sources/__init__.py +26 -0
  38. airbyte_cdk/sources/abstract_source.py +326 -0
  39. airbyte_cdk/sources/concurrent_source/__init__.py +8 -0
  40. airbyte_cdk/sources/concurrent_source/concurrent_read_processor.py +255 -0
  41. airbyte_cdk/sources/concurrent_source/concurrent_source.py +165 -0
  42. airbyte_cdk/sources/concurrent_source/concurrent_source_adapter.py +147 -0
  43. airbyte_cdk/sources/concurrent_source/partition_generation_completed_sentinel.py +24 -0
  44. airbyte_cdk/sources/concurrent_source/stream_thread_exception.py +25 -0
  45. airbyte_cdk/sources/concurrent_source/thread_pool_manager.py +115 -0
  46. airbyte_cdk/sources/config.py +27 -0
  47. airbyte_cdk/sources/connector_state_manager.py +161 -0
  48. airbyte_cdk/sources/declarative/__init__.py +3 -0
  49. airbyte_cdk/sources/declarative/async_job/__init__.py +0 -0
  50. airbyte_cdk/sources/declarative/async_job/job.py +52 -0
  51. airbyte_cdk/sources/declarative/async_job/job_orchestrator.py +525 -0
  52. airbyte_cdk/sources/declarative/async_job/job_tracker.py +79 -0
  53. airbyte_cdk/sources/declarative/async_job/repository.py +35 -0
  54. airbyte_cdk/sources/declarative/async_job/status.py +24 -0
  55. airbyte_cdk/sources/declarative/async_job/timer.py +39 -0
  56. airbyte_cdk/sources/declarative/auth/__init__.py +8 -0
  57. airbyte_cdk/sources/declarative/auth/declarative_authenticator.py +42 -0
  58. airbyte_cdk/sources/declarative/auth/jwt.py +197 -0
  59. airbyte_cdk/sources/declarative/auth/oauth.py +293 -0
  60. airbyte_cdk/sources/declarative/auth/selective_authenticator.py +45 -0
  61. airbyte_cdk/sources/declarative/auth/token.py +267 -0
  62. airbyte_cdk/sources/declarative/auth/token_provider.py +82 -0
  63. airbyte_cdk/sources/declarative/checks/__init__.py +24 -0
  64. airbyte_cdk/sources/declarative/checks/check_dynamic_stream.py +61 -0
  65. airbyte_cdk/sources/declarative/checks/check_stream.py +56 -0
  66. airbyte_cdk/sources/declarative/checks/connection_checker.py +35 -0
  67. airbyte_cdk/sources/declarative/concurrency_level/__init__.py +7 -0
  68. airbyte_cdk/sources/declarative/concurrency_level/concurrency_level.py +50 -0
  69. airbyte_cdk/sources/declarative/concurrent_declarative_source.py +526 -0
  70. airbyte_cdk/sources/declarative/datetime/__init__.py +3 -0
  71. airbyte_cdk/sources/declarative/datetime/datetime_parser.py +65 -0
  72. airbyte_cdk/sources/declarative/datetime/min_max_datetime.py +118 -0
  73. airbyte_cdk/sources/declarative/declarative_component_schema.yaml +3975 -0
  74. airbyte_cdk/sources/declarative/declarative_source.py +36 -0
  75. airbyte_cdk/sources/declarative/declarative_stream.py +241 -0
  76. airbyte_cdk/sources/declarative/decoders/__init__.py +33 -0
  77. airbyte_cdk/sources/declarative/decoders/composite_raw_decoder.py +218 -0
  78. airbyte_cdk/sources/declarative/decoders/decoder.py +32 -0
  79. airbyte_cdk/sources/declarative/decoders/decoder_parser.py +30 -0
  80. airbyte_cdk/sources/declarative/decoders/json_decoder.py +65 -0
  81. airbyte_cdk/sources/declarative/decoders/noop_decoder.py +21 -0
  82. airbyte_cdk/sources/declarative/decoders/pagination_decoder_decorator.py +39 -0
  83. airbyte_cdk/sources/declarative/decoders/xml_decoder.py +98 -0
  84. airbyte_cdk/sources/declarative/decoders/zipfile_decoder.py +56 -0
  85. airbyte_cdk/sources/declarative/exceptions.py +9 -0
  86. airbyte_cdk/sources/declarative/extractors/__init__.py +21 -0
  87. airbyte_cdk/sources/declarative/extractors/dpath_extractor.py +86 -0
  88. airbyte_cdk/sources/declarative/extractors/http_selector.py +37 -0
  89. airbyte_cdk/sources/declarative/extractors/record_extractor.py +27 -0
  90. airbyte_cdk/sources/declarative/extractors/record_filter.py +91 -0
  91. airbyte_cdk/sources/declarative/extractors/record_selector.py +170 -0
  92. airbyte_cdk/sources/declarative/extractors/response_to_file_extractor.py +176 -0
  93. airbyte_cdk/sources/declarative/extractors/type_transformer.py +55 -0
  94. airbyte_cdk/sources/declarative/incremental/__init__.py +37 -0
  95. airbyte_cdk/sources/declarative/incremental/concurrent_partition_cursor.py +497 -0
  96. airbyte_cdk/sources/declarative/incremental/datetime_based_cursor.py +459 -0
  97. airbyte_cdk/sources/declarative/incremental/declarative_cursor.py +13 -0
  98. airbyte_cdk/sources/declarative/incremental/global_substream_cursor.py +357 -0
  99. airbyte_cdk/sources/declarative/incremental/per_partition_cursor.py +380 -0
  100. airbyte_cdk/sources/declarative/incremental/per_partition_with_global.py +200 -0
  101. airbyte_cdk/sources/declarative/incremental/resumable_full_refresh_cursor.py +122 -0
  102. airbyte_cdk/sources/declarative/interpolation/__init__.py +9 -0
  103. airbyte_cdk/sources/declarative/interpolation/filters.py +139 -0
  104. airbyte_cdk/sources/declarative/interpolation/interpolated_boolean.py +66 -0
  105. airbyte_cdk/sources/declarative/interpolation/interpolated_mapping.py +56 -0
  106. airbyte_cdk/sources/declarative/interpolation/interpolated_nested_mapping.py +52 -0
  107. airbyte_cdk/sources/declarative/interpolation/interpolated_string.py +79 -0
  108. airbyte_cdk/sources/declarative/interpolation/interpolation.py +34 -0
  109. airbyte_cdk/sources/declarative/interpolation/jinja.py +161 -0
  110. airbyte_cdk/sources/declarative/interpolation/macros.py +191 -0
  111. airbyte_cdk/sources/declarative/manifest_declarative_source.py +421 -0
  112. airbyte_cdk/sources/declarative/migrations/__init__.py +0 -0
  113. airbyte_cdk/sources/declarative/migrations/legacy_to_per_partition_state_migration.py +98 -0
  114. airbyte_cdk/sources/declarative/migrations/state_migration.py +24 -0
  115. airbyte_cdk/sources/declarative/models/__init__.py +2 -0
  116. airbyte_cdk/sources/declarative/models/declarative_component_schema.py +2503 -0
  117. airbyte_cdk/sources/declarative/parsers/__init__.py +3 -0
  118. airbyte_cdk/sources/declarative/parsers/custom_code_compiler.py +157 -0
  119. airbyte_cdk/sources/declarative/parsers/custom_exceptions.py +21 -0
  120. airbyte_cdk/sources/declarative/parsers/manifest_component_transformer.py +172 -0
  121. airbyte_cdk/sources/declarative/parsers/manifest_reference_resolver.py +213 -0
  122. airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py +3407 -0
  123. airbyte_cdk/sources/declarative/partition_routers/__init__.py +29 -0
  124. airbyte_cdk/sources/declarative/partition_routers/async_job_partition_router.py +65 -0
  125. airbyte_cdk/sources/declarative/partition_routers/cartesian_product_stream_slicer.py +176 -0
  126. airbyte_cdk/sources/declarative/partition_routers/list_partition_router.py +121 -0
  127. airbyte_cdk/sources/declarative/partition_routers/partition_router.py +62 -0
  128. airbyte_cdk/sources/declarative/partition_routers/single_partition_router.py +63 -0
  129. airbyte_cdk/sources/declarative/partition_routers/substream_partition_router.py +437 -0
  130. airbyte_cdk/sources/declarative/requesters/README.md +56 -0
  131. airbyte_cdk/sources/declarative/requesters/__init__.py +9 -0
  132. airbyte_cdk/sources/declarative/requesters/error_handlers/__init__.py +25 -0
  133. airbyte_cdk/sources/declarative/requesters/error_handlers/backoff_strategies/__init__.py +23 -0
  134. airbyte_cdk/sources/declarative/requesters/error_handlers/backoff_strategies/constant_backoff_strategy.py +45 -0
  135. airbyte_cdk/sources/declarative/requesters/error_handlers/backoff_strategies/exponential_backoff_strategy.py +45 -0
  136. airbyte_cdk/sources/declarative/requesters/error_handlers/backoff_strategies/header_helper.py +41 -0
  137. airbyte_cdk/sources/declarative/requesters/error_handlers/backoff_strategies/wait_time_from_header_backoff_strategy.py +70 -0
  138. airbyte_cdk/sources/declarative/requesters/error_handlers/backoff_strategies/wait_until_time_from_header_backoff_strategy.py +77 -0
  139. airbyte_cdk/sources/declarative/requesters/error_handlers/backoff_strategy.py +17 -0
  140. airbyte_cdk/sources/declarative/requesters/error_handlers/composite_error_handler.py +101 -0
  141. airbyte_cdk/sources/declarative/requesters/error_handlers/default_error_handler.py +147 -0
  142. airbyte_cdk/sources/declarative/requesters/error_handlers/default_http_response_filter.py +40 -0
  143. airbyte_cdk/sources/declarative/requesters/error_handlers/error_handler.py +17 -0
  144. airbyte_cdk/sources/declarative/requesters/error_handlers/http_response_filter.py +179 -0
  145. airbyte_cdk/sources/declarative/requesters/http_job_repository.py +350 -0
  146. airbyte_cdk/sources/declarative/requesters/http_requester.py +433 -0
  147. airbyte_cdk/sources/declarative/requesters/paginators/__init__.py +21 -0
  148. airbyte_cdk/sources/declarative/requesters/paginators/default_paginator.py +327 -0
  149. airbyte_cdk/sources/declarative/requesters/paginators/no_pagination.py +76 -0
  150. airbyte_cdk/sources/declarative/requesters/paginators/paginator.py +65 -0
  151. airbyte_cdk/sources/declarative/requesters/paginators/strategies/__init__.py +25 -0
  152. airbyte_cdk/sources/declarative/requesters/paginators/strategies/cursor_pagination_strategy.py +98 -0
  153. airbyte_cdk/sources/declarative/requesters/paginators/strategies/offset_increment.py +102 -0
  154. airbyte_cdk/sources/declarative/requesters/paginators/strategies/page_increment.py +71 -0
  155. airbyte_cdk/sources/declarative/requesters/paginators/strategies/pagination_strategy.py +48 -0
  156. airbyte_cdk/sources/declarative/requesters/paginators/strategies/stop_condition.py +66 -0
  157. airbyte_cdk/sources/declarative/requesters/request_option.py +117 -0
  158. airbyte_cdk/sources/declarative/requesters/request_options/__init__.py +23 -0
  159. airbyte_cdk/sources/declarative/requesters/request_options/datetime_based_request_options_provider.py +92 -0
  160. airbyte_cdk/sources/declarative/requesters/request_options/default_request_options_provider.py +60 -0
  161. airbyte_cdk/sources/declarative/requesters/request_options/interpolated_nested_request_input_provider.py +59 -0
  162. airbyte_cdk/sources/declarative/requesters/request_options/interpolated_request_input_provider.py +68 -0
  163. airbyte_cdk/sources/declarative/requesters/request_options/interpolated_request_options_provider.py +119 -0
  164. airbyte_cdk/sources/declarative/requesters/request_options/request_options_provider.py +79 -0
  165. airbyte_cdk/sources/declarative/requesters/request_path.py +15 -0
  166. airbyte_cdk/sources/declarative/requesters/requester.py +144 -0
  167. airbyte_cdk/sources/declarative/resolvers/__init__.py +41 -0
  168. airbyte_cdk/sources/declarative/resolvers/components_resolver.py +55 -0
  169. airbyte_cdk/sources/declarative/resolvers/config_components_resolver.py +136 -0
  170. airbyte_cdk/sources/declarative/resolvers/http_components_resolver.py +112 -0
  171. airbyte_cdk/sources/declarative/retrievers/__init__.py +19 -0
  172. airbyte_cdk/sources/declarative/retrievers/async_retriever.py +124 -0
  173. airbyte_cdk/sources/declarative/retrievers/file_uploader.py +89 -0
  174. airbyte_cdk/sources/declarative/retrievers/retriever.py +54 -0
  175. airbyte_cdk/sources/declarative/retrievers/simple_retriever.py +702 -0
  176. airbyte_cdk/sources/declarative/schema/__init__.py +25 -0
  177. airbyte_cdk/sources/declarative/schema/default_schema_loader.py +47 -0
  178. airbyte_cdk/sources/declarative/schema/dynamic_schema_loader.py +285 -0
  179. airbyte_cdk/sources/declarative/schema/inline_schema_loader.py +19 -0
  180. airbyte_cdk/sources/declarative/schema/json_file_schema_loader.py +92 -0
  181. airbyte_cdk/sources/declarative/schema/schema_loader.py +17 -0
  182. airbyte_cdk/sources/declarative/spec/__init__.py +7 -0
  183. airbyte_cdk/sources/declarative/spec/spec.py +48 -0
  184. airbyte_cdk/sources/declarative/stream_slicers/__init__.py +7 -0
  185. airbyte_cdk/sources/declarative/stream_slicers/declarative_partition_generator.py +93 -0
  186. airbyte_cdk/sources/declarative/stream_slicers/stream_slicer.py +25 -0
  187. airbyte_cdk/sources/declarative/transformations/__init__.py +17 -0
  188. airbyte_cdk/sources/declarative/transformations/add_fields.py +146 -0
  189. airbyte_cdk/sources/declarative/transformations/dpath_flatten_fields.py +61 -0
  190. airbyte_cdk/sources/declarative/transformations/flatten_fields.py +52 -0
  191. airbyte_cdk/sources/declarative/transformations/keys_replace_transformation.py +61 -0
  192. airbyte_cdk/sources/declarative/transformations/keys_to_lower_transformation.py +22 -0
  193. airbyte_cdk/sources/declarative/transformations/keys_to_snake_transformation.py +68 -0
  194. airbyte_cdk/sources/declarative/transformations/remove_fields.py +75 -0
  195. airbyte_cdk/sources/declarative/transformations/transformation.py +37 -0
  196. airbyte_cdk/sources/declarative/types.py +25 -0
  197. airbyte_cdk/sources/declarative/yaml_declarative_source.py +67 -0
  198. airbyte_cdk/sources/file_based/README.md +152 -0
  199. airbyte_cdk/sources/file_based/__init__.py +24 -0
  200. airbyte_cdk/sources/file_based/availability_strategy/__init__.py +11 -0
  201. airbyte_cdk/sources/file_based/availability_strategy/abstract_file_based_availability_strategy.py +73 -0
  202. airbyte_cdk/sources/file_based/availability_strategy/default_file_based_availability_strategy.py +149 -0
  203. airbyte_cdk/sources/file_based/config/__init__.py +0 -0
  204. airbyte_cdk/sources/file_based/config/abstract_file_based_spec.py +153 -0
  205. airbyte_cdk/sources/file_based/config/avro_format.py +25 -0
  206. airbyte_cdk/sources/file_based/config/csv_format.py +210 -0
  207. airbyte_cdk/sources/file_based/config/excel_format.py +18 -0
  208. airbyte_cdk/sources/file_based/config/file_based_stream_config.py +99 -0
  209. airbyte_cdk/sources/file_based/config/jsonl_format.py +18 -0
  210. airbyte_cdk/sources/file_based/config/parquet_format.py +25 -0
  211. airbyte_cdk/sources/file_based/config/unstructured_format.py +102 -0
  212. airbyte_cdk/sources/file_based/config/validate_config_transfer_modes.py +81 -0
  213. airbyte_cdk/sources/file_based/discovery_policy/__init__.py +8 -0
  214. airbyte_cdk/sources/file_based/discovery_policy/abstract_discovery_policy.py +21 -0
  215. airbyte_cdk/sources/file_based/discovery_policy/default_discovery_policy.py +33 -0
  216. airbyte_cdk/sources/file_based/exceptions.py +159 -0
  217. airbyte_cdk/sources/file_based/file_based_source.py +466 -0
  218. airbyte_cdk/sources/file_based/file_based_stream_permissions_reader.py +123 -0
  219. airbyte_cdk/sources/file_based/file_based_stream_reader.py +209 -0
  220. airbyte_cdk/sources/file_based/file_record_data.py +22 -0
  221. airbyte_cdk/sources/file_based/file_types/__init__.py +37 -0
  222. airbyte_cdk/sources/file_based/file_types/avro_parser.py +233 -0
  223. airbyte_cdk/sources/file_based/file_types/csv_parser.py +527 -0
  224. airbyte_cdk/sources/file_based/file_types/excel_parser.py +196 -0
  225. airbyte_cdk/sources/file_based/file_types/file_transfer.py +30 -0
  226. airbyte_cdk/sources/file_based/file_types/file_type_parser.py +86 -0
  227. airbyte_cdk/sources/file_based/file_types/jsonl_parser.py +145 -0
  228. airbyte_cdk/sources/file_based/file_types/parquet_parser.py +275 -0
  229. airbyte_cdk/sources/file_based/file_types/unstructured_parser.py +480 -0
  230. airbyte_cdk/sources/file_based/remote_file.py +18 -0
  231. airbyte_cdk/sources/file_based/schema_helpers.py +281 -0
  232. airbyte_cdk/sources/file_based/schema_validation_policies/__init__.py +17 -0
  233. airbyte_cdk/sources/file_based/schema_validation_policies/abstract_schema_validation_policy.py +20 -0
  234. airbyte_cdk/sources/file_based/schema_validation_policies/default_schema_validation_policies.py +52 -0
  235. airbyte_cdk/sources/file_based/stream/__init__.py +13 -0
  236. airbyte_cdk/sources/file_based/stream/abstract_file_based_stream.py +197 -0
  237. airbyte_cdk/sources/file_based/stream/concurrent/__init__.py +0 -0
  238. airbyte_cdk/sources/file_based/stream/concurrent/adapters.py +343 -0
  239. airbyte_cdk/sources/file_based/stream/concurrent/cursor/__init__.py +9 -0
  240. airbyte_cdk/sources/file_based/stream/concurrent/cursor/abstract_concurrent_file_based_cursor.py +59 -0
  241. airbyte_cdk/sources/file_based/stream/concurrent/cursor/file_based_concurrent_cursor.py +313 -0
  242. airbyte_cdk/sources/file_based/stream/concurrent/cursor/file_based_final_state_cursor.py +83 -0
  243. airbyte_cdk/sources/file_based/stream/cursor/__init__.py +4 -0
  244. airbyte_cdk/sources/file_based/stream/cursor/abstract_file_based_cursor.py +66 -0
  245. airbyte_cdk/sources/file_based/stream/cursor/default_file_based_cursor.py +149 -0
  246. airbyte_cdk/sources/file_based/stream/default_file_based_stream.py +396 -0
  247. airbyte_cdk/sources/file_based/stream/identities_stream.py +49 -0
  248. airbyte_cdk/sources/file_based/stream/permissions_file_based_stream.py +92 -0
  249. airbyte_cdk/sources/file_based/types.py +10 -0
  250. airbyte_cdk/sources/http_config.py +10 -0
  251. airbyte_cdk/sources/http_logger.py +55 -0
  252. airbyte_cdk/sources/message/__init__.py +19 -0
  253. airbyte_cdk/sources/message/repository.py +137 -0
  254. airbyte_cdk/sources/source.py +95 -0
  255. airbyte_cdk/sources/specs/transfer_modes.py +26 -0
  256. airbyte_cdk/sources/streams/__init__.py +8 -0
  257. airbyte_cdk/sources/streams/availability_strategy.py +84 -0
  258. airbyte_cdk/sources/streams/call_rate.py +704 -0
  259. airbyte_cdk/sources/streams/checkpoint/__init__.py +26 -0
  260. airbyte_cdk/sources/streams/checkpoint/checkpoint_reader.py +335 -0
  261. airbyte_cdk/sources/streams/checkpoint/cursor.py +77 -0
  262. airbyte_cdk/sources/streams/checkpoint/per_partition_key_serializer.py +22 -0
  263. airbyte_cdk/sources/streams/checkpoint/resumable_full_refresh_cursor.py +51 -0
  264. airbyte_cdk/sources/streams/checkpoint/substream_resumable_full_refresh_cursor.py +110 -0
  265. airbyte_cdk/sources/streams/concurrent/README.md +7 -0
  266. airbyte_cdk/sources/streams/concurrent/__init__.py +3 -0
  267. airbyte_cdk/sources/streams/concurrent/abstract_stream.py +96 -0
  268. airbyte_cdk/sources/streams/concurrent/abstract_stream_facade.py +37 -0
  269. airbyte_cdk/sources/streams/concurrent/adapters.py +397 -0
  270. airbyte_cdk/sources/streams/concurrent/availability_strategy.py +94 -0
  271. airbyte_cdk/sources/streams/concurrent/clamping.py +99 -0
  272. airbyte_cdk/sources/streams/concurrent/cursor.py +481 -0
  273. airbyte_cdk/sources/streams/concurrent/cursor_types.py +32 -0
  274. airbyte_cdk/sources/streams/concurrent/default_stream.py +102 -0
  275. airbyte_cdk/sources/streams/concurrent/exceptions.py +18 -0
  276. airbyte_cdk/sources/streams/concurrent/helpers.py +42 -0
  277. airbyte_cdk/sources/streams/concurrent/partition_enqueuer.py +64 -0
  278. airbyte_cdk/sources/streams/concurrent/partition_reader.py +45 -0
  279. airbyte_cdk/sources/streams/concurrent/partitions/__init__.py +3 -0
  280. airbyte_cdk/sources/streams/concurrent/partitions/partition.py +48 -0
  281. airbyte_cdk/sources/streams/concurrent/partitions/partition_generator.py +18 -0
  282. airbyte_cdk/sources/streams/concurrent/partitions/stream_slicer.py +21 -0
  283. airbyte_cdk/sources/streams/concurrent/partitions/types.py +38 -0
  284. airbyte_cdk/sources/streams/concurrent/state_converters/__init__.py +0 -0
  285. airbyte_cdk/sources/streams/concurrent/state_converters/abstract_stream_state_converter.py +182 -0
  286. airbyte_cdk/sources/streams/concurrent/state_converters/datetime_stream_state_converter.py +223 -0
  287. airbyte_cdk/sources/streams/concurrent/state_converters/incrementing_count_stream_state_converter.py +92 -0
  288. airbyte_cdk/sources/streams/core.py +703 -0
  289. airbyte_cdk/sources/streams/http/__init__.py +10 -0
  290. airbyte_cdk/sources/streams/http/availability_strategy.py +54 -0
  291. airbyte_cdk/sources/streams/http/error_handlers/__init__.py +22 -0
  292. airbyte_cdk/sources/streams/http/error_handlers/backoff_strategy.py +28 -0
  293. airbyte_cdk/sources/streams/http/error_handlers/default_backoff_strategy.py +17 -0
  294. airbyte_cdk/sources/streams/http/error_handlers/default_error_mapping.py +86 -0
  295. airbyte_cdk/sources/streams/http/error_handlers/error_handler.py +42 -0
  296. airbyte_cdk/sources/streams/http/error_handlers/error_message_parser.py +19 -0
  297. airbyte_cdk/sources/streams/http/error_handlers/http_status_error_handler.py +110 -0
  298. airbyte_cdk/sources/streams/http/error_handlers/json_error_message_parser.py +52 -0
  299. airbyte_cdk/sources/streams/http/error_handlers/response_models.py +65 -0
  300. airbyte_cdk/sources/streams/http/exceptions.py +61 -0
  301. airbyte_cdk/sources/streams/http/http.py +673 -0
  302. airbyte_cdk/sources/streams/http/http_client.py +531 -0
  303. airbyte_cdk/sources/streams/http/rate_limiting.py +158 -0
  304. airbyte_cdk/sources/streams/http/requests_native_auth/__init__.py +14 -0
  305. airbyte_cdk/sources/streams/http/requests_native_auth/abstract_oauth.py +479 -0
  306. airbyte_cdk/sources/streams/http/requests_native_auth/abstract_token.py +34 -0
  307. airbyte_cdk/sources/streams/http/requests_native_auth/oauth.py +436 -0
  308. airbyte_cdk/sources/streams/http/requests_native_auth/token.py +83 -0
  309. airbyte_cdk/sources/streams/permissions/identities_stream.py +75 -0
  310. airbyte_cdk/sources/streams/utils/__init__.py +3 -0
  311. airbyte_cdk/sources/types.py +169 -0
  312. airbyte_cdk/sources/utils/__init__.py +7 -0
  313. airbyte_cdk/sources/utils/casing.py +12 -0
  314. airbyte_cdk/sources/utils/files_directory.py +15 -0
  315. airbyte_cdk/sources/utils/record_helper.py +53 -0
  316. airbyte_cdk/sources/utils/schema_helpers.py +230 -0
  317. airbyte_cdk/sources/utils/slice_logger.py +57 -0
  318. airbyte_cdk/sources/utils/transform.py +277 -0
  319. airbyte_cdk/sources/utils/types.py +7 -0
  320. airbyte_cdk/sql/__init__.py +0 -0
  321. airbyte_cdk/sql/_util/__init__.py +0 -0
  322. airbyte_cdk/sql/_util/hashing.py +34 -0
  323. airbyte_cdk/sql/_util/name_normalizers.py +92 -0
  324. airbyte_cdk/sql/constants.py +32 -0
  325. airbyte_cdk/sql/exceptions.py +235 -0
  326. airbyte_cdk/sql/secrets.py +123 -0
  327. airbyte_cdk/sql/shared/__init__.py +15 -0
  328. airbyte_cdk/sql/shared/catalog_providers.py +145 -0
  329. airbyte_cdk/sql/shared/sql_processor.py +786 -0
  330. airbyte_cdk/sql/types.py +160 -0
  331. airbyte_cdk/test/__init__.py +7 -0
  332. airbyte_cdk/test/catalog_builder.py +81 -0
  333. airbyte_cdk/test/entrypoint_wrapper.py +250 -0
  334. airbyte_cdk/test/mock_http/__init__.py +6 -0
  335. airbyte_cdk/test/mock_http/matcher.py +41 -0
  336. airbyte_cdk/test/mock_http/mocker.py +185 -0
  337. airbyte_cdk/test/mock_http/request.py +103 -0
  338. airbyte_cdk/test/mock_http/response.py +28 -0
  339. airbyte_cdk/test/mock_http/response_builder.py +237 -0
  340. airbyte_cdk/test/state_builder.py +33 -0
  341. airbyte_cdk/test/utils/__init__.py +1 -0
  342. airbyte_cdk/test/utils/data.py +24 -0
  343. airbyte_cdk/test/utils/http_mocking.py +16 -0
  344. airbyte_cdk/test/utils/manifest_only_fixtures.py +59 -0
  345. airbyte_cdk/test/utils/reading.py +26 -0
  346. airbyte_cdk/utils/__init__.py +10 -0
  347. airbyte_cdk/utils/airbyte_secrets_utils.py +80 -0
  348. airbyte_cdk/utils/analytics_message.py +25 -0
  349. airbyte_cdk/utils/constants.py +5 -0
  350. airbyte_cdk/utils/datetime_format_inferrer.py +94 -0
  351. airbyte_cdk/utils/datetime_helpers.py +499 -0
  352. airbyte_cdk/utils/event_timing.py +85 -0
  353. airbyte_cdk/utils/is_cloud_environment.py +18 -0
  354. airbyte_cdk/utils/mapping_helpers.py +162 -0
  355. airbyte_cdk/utils/message_utils.py +26 -0
  356. airbyte_cdk/utils/oneof_option_config.py +33 -0
  357. airbyte_cdk/utils/print_buffer.py +75 -0
  358. airbyte_cdk/utils/schema_inferrer.py +270 -0
  359. airbyte_cdk/utils/slice_hasher.py +37 -0
  360. airbyte_cdk/utils/spec_schema_transformations.py +26 -0
  361. airbyte_cdk/utils/stream_status_utils.py +43 -0
  362. airbyte_cdk/utils/traced_exception.py +145 -0
  363. airbyte_cdk-0.0.0.dev0.dist-info/LICENSE.txt +19 -0
  364. airbyte_cdk-0.0.0.dev0.dist-info/LICENSE_SHORT +1 -0
  365. airbyte_cdk-0.0.0.dev0.dist-info/METADATA +111 -0
  366. airbyte_cdk-0.0.0.dev0.dist-info/RECORD +368 -0
  367. airbyte_cdk-0.0.0.dev0.dist-info/WHEEL +4 -0
  368. airbyte_cdk-0.0.0.dev0.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,459 @@
1
+ #
2
+ # Copyright (c) 2023 Airbyte, Inc., all rights reserved.
3
+ #
4
+
5
+ import datetime
6
+ from dataclasses import InitVar, dataclass, field
7
+ from datetime import timedelta
8
+ from typing import Any, Callable, Iterable, List, Mapping, MutableMapping, Optional, Union
9
+
10
+ from isodate import Duration, duration_isoformat, parse_duration
11
+
12
+ from airbyte_cdk.models import AirbyteLogMessage, AirbyteMessage, Level, Type
13
+ from airbyte_cdk.sources.declarative.datetime.datetime_parser import DatetimeParser
14
+ from airbyte_cdk.sources.declarative.datetime.min_max_datetime import MinMaxDatetime
15
+ from airbyte_cdk.sources.declarative.incremental.declarative_cursor import DeclarativeCursor
16
+ from airbyte_cdk.sources.declarative.interpolation.interpolated_string import InterpolatedString
17
+ from airbyte_cdk.sources.declarative.interpolation.jinja import JinjaInterpolation
18
+ from airbyte_cdk.sources.declarative.requesters.request_option import (
19
+ RequestOption,
20
+ RequestOptionType,
21
+ )
22
+ from airbyte_cdk.sources.message import MessageRepository
23
+ from airbyte_cdk.sources.types import Config, Record, StreamSlice, StreamState
24
+ from airbyte_cdk.utils.mapping_helpers import _validate_component_request_option_paths
25
+
26
+
27
+ @dataclass
28
+ class DatetimeBasedCursor(DeclarativeCursor):
29
+ """
30
+ Slices the stream over a datetime range and create a state with format {<cursor_field>: <datetime> }
31
+
32
+ Given a start time, end time, a step function, and an optional lookback window,
33
+ the stream slicer will partition the date range from start time - lookback window to end time.
34
+
35
+ The step function is defined as a string of the form ISO8601 duration
36
+
37
+ The timestamp format accepts the same format codes as datetime.strfptime, which are
38
+ all the format codes required by the 1989 C standard.
39
+ Full list of accepted format codes: https://man7.org/linux/man-pages/man3/strftime.3.html
40
+
41
+ Attributes:
42
+ start_datetime (Union[MinMaxDatetime, str]): the datetime that determines the earliest record that should be synced
43
+ end_datetime (Optional[Union[MinMaxDatetime, str]]): the datetime that determines the last record that should be synced
44
+ cursor_field (Union[InterpolatedString, str]): record's cursor field
45
+ datetime_format (str): format of the datetime
46
+ step (Optional[str]): size of the timewindow (ISO8601 duration)
47
+ cursor_granularity (Optional[str]): smallest increment the datetime_format has (ISO 8601 duration) that will be used to ensure that the start of a slice does not overlap with the end of the previous one
48
+ config (Config): connection config
49
+ start_time_option (Optional[RequestOption]): request option for start time
50
+ end_time_option (Optional[RequestOption]): request option for end time
51
+ partition_field_start (Optional[str]): partition start time field
52
+ partition_field_end (Optional[str]): stream slice end time field
53
+ lookback_window (Optional[InterpolatedString]): how many days before start_datetime to read data for (ISO8601 duration)
54
+ """
55
+
56
+ start_datetime: Union[MinMaxDatetime, str]
57
+ cursor_field: Union[InterpolatedString, str]
58
+ datetime_format: str
59
+ config: Config
60
+ parameters: InitVar[Mapping[str, Any]]
61
+ _highest_observed_cursor_field_value: Optional[str] = field(
62
+ repr=False, default=None
63
+ ) # tracks the latest observed datetime, which may not be safe to emit in the case of out-of-order records
64
+ _cursor: Optional[str] = field(
65
+ repr=False, default=None
66
+ ) # tracks the latest observed datetime that is appropriate to emit as stream state
67
+ end_datetime: Optional[Union[MinMaxDatetime, str]] = None
68
+ step: Optional[Union[InterpolatedString, str]] = None
69
+ cursor_granularity: Optional[str] = None
70
+ start_time_option: Optional[RequestOption] = None
71
+ end_time_option: Optional[RequestOption] = None
72
+ partition_field_start: Optional[str] = None
73
+ partition_field_end: Optional[str] = None
74
+ lookback_window: Optional[Union[InterpolatedString, str]] = None
75
+ message_repository: Optional[MessageRepository] = None
76
+ is_compare_strictly: Optional[bool] = False
77
+ cursor_datetime_formats: List[str] = field(default_factory=lambda: [])
78
+
79
+ def __post_init__(self, parameters: Mapping[str, Any]) -> None:
80
+ if (self.step and not self.cursor_granularity) or (
81
+ not self.step and self.cursor_granularity
82
+ ):
83
+ raise ValueError(
84
+ f"If step is defined, cursor_granularity should be as well and vice-versa. "
85
+ f"Right now, step is `{self.step}` and cursor_granularity is `{self.cursor_granularity}`"
86
+ )
87
+ self._start_datetime = MinMaxDatetime.create(self.start_datetime, parameters)
88
+ self._end_datetime = (
89
+ None if not self.end_datetime else MinMaxDatetime.create(self.end_datetime, parameters)
90
+ )
91
+
92
+ self._timezone = datetime.timezone.utc
93
+ self._interpolation = JinjaInterpolation()
94
+
95
+ self._step = (
96
+ self._parse_timedelta(
97
+ InterpolatedString.create(self.step, parameters=parameters).eval(self.config)
98
+ )
99
+ if self.step
100
+ else datetime.timedelta.max
101
+ )
102
+ self._cursor_granularity = self._parse_timedelta(self.cursor_granularity)
103
+ self.cursor_field = InterpolatedString.create(self.cursor_field, parameters=parameters)
104
+ self._lookback_window = (
105
+ InterpolatedString.create(self.lookback_window, parameters=parameters)
106
+ if self.lookback_window
107
+ else None
108
+ )
109
+ self._partition_field_start = InterpolatedString.create(
110
+ self.partition_field_start or "start_time", parameters=parameters
111
+ )
112
+ self._partition_field_end = InterpolatedString.create(
113
+ self.partition_field_end or "end_time", parameters=parameters
114
+ )
115
+ self._parser = DatetimeParser()
116
+
117
+ # If datetime format is not specified then start/end datetime should inherit it from the stream slicer
118
+ if not self._start_datetime.datetime_format:
119
+ self._start_datetime.datetime_format = self.datetime_format
120
+ if self._end_datetime and not self._end_datetime.datetime_format:
121
+ self._end_datetime.datetime_format = self.datetime_format
122
+
123
+ if not self.cursor_datetime_formats:
124
+ self.cursor_datetime_formats = [self.datetime_format]
125
+
126
+ _validate_component_request_option_paths(
127
+ self.config, self.start_time_option, self.end_time_option
128
+ )
129
+
130
+ def get_stream_state(self) -> StreamState:
131
+ return {self.cursor_field.eval(self.config): self._cursor} if self._cursor else {} # type: ignore # cursor_field is converted to an InterpolatedString in __post_init__
132
+
133
+ def set_initial_state(self, stream_state: StreamState) -> None:
134
+ """
135
+ Cursors are not initialized with their state. As state is needed in order to function properly, this method should be called
136
+ before calling anything else
137
+
138
+ :param stream_state: The state of the stream as returned by get_stream_state
139
+ """
140
+ self._cursor = (
141
+ stream_state.get(self.cursor_field.eval(self.config)) if stream_state else None # type: ignore [union-attr]
142
+ )
143
+
144
+ def observe(self, stream_slice: StreamSlice, record: Record) -> None:
145
+ """
146
+ Register a record with the cursor; the cursor instance can then use it to manage the state of the in-progress stream read.
147
+
148
+ :param stream_slice: The current slice, which may or may not contain the most recently observed record
149
+ :param record: the most recently-read record, which the cursor can use to update the stream state. Outwardly-visible changes to the
150
+ stream state may need to be deferred depending on whether the source reliably orders records by the cursor field.
151
+ """
152
+ record_cursor_value = record.get(self.cursor_field.eval(self.config)) # type: ignore # cursor_field is converted to an InterpolatedString in __post_init__
153
+ # if the current record has no cursor value, we cannot meaningfully update the state based on it, so there is nothing more to do
154
+ if not record_cursor_value:
155
+ return
156
+
157
+ start_field = self._partition_field_start.eval(self.config)
158
+ end_field = self._partition_field_end.eval(self.config)
159
+ is_highest_observed_cursor_value = (
160
+ not self._highest_observed_cursor_field_value
161
+ or self.parse_date(record_cursor_value)
162
+ > self.parse_date(self._highest_observed_cursor_field_value)
163
+ )
164
+ if (
165
+ self._is_within_daterange_boundaries(
166
+ record,
167
+ stream_slice.get(start_field), # type: ignore [arg-type]
168
+ stream_slice.get(end_field), # type: ignore [arg-type]
169
+ )
170
+ and is_highest_observed_cursor_value
171
+ ):
172
+ self._highest_observed_cursor_field_value = record_cursor_value
173
+
174
+ def close_slice(self, stream_slice: StreamSlice, *args: Any) -> None:
175
+ if stream_slice.partition:
176
+ raise ValueError(
177
+ f"Stream slice {stream_slice} should not have a partition. Got {stream_slice.partition}."
178
+ )
179
+ cursor_value_str_by_cursor_value_datetime = dict(
180
+ map(
181
+ # we need to ensure the cursor value is preserved as is in the state else the CATs might complain of something like
182
+ # 2023-01-04T17:30:19.000Z' <= '2023-01-04T17:30:19.000000Z'
183
+ lambda datetime_str: (self.parse_date(datetime_str), datetime_str), # type: ignore # because of the filter on the next line, this will only be called with a str
184
+ filter(
185
+ lambda item: item, [self._cursor, self._highest_observed_cursor_field_value]
186
+ ),
187
+ )
188
+ )
189
+ self._cursor = (
190
+ cursor_value_str_by_cursor_value_datetime[
191
+ max(cursor_value_str_by_cursor_value_datetime.keys())
192
+ ]
193
+ if cursor_value_str_by_cursor_value_datetime
194
+ else None
195
+ )
196
+
197
+ def stream_slices(self) -> Iterable[StreamSlice]:
198
+ """
199
+ Partition the daterange into slices of size = step.
200
+
201
+ The start of the window is the minimum datetime between start_datetime - lookback_window and the stream_state's datetime
202
+ The end of the window is the minimum datetime between the start of the window and end_datetime.
203
+
204
+ :return:
205
+ """
206
+ end_datetime = self.select_best_end_datetime()
207
+ start_datetime = self._calculate_earliest_possible_value(self.select_best_end_datetime())
208
+ return self._partition_daterange(start_datetime, end_datetime, self._step)
209
+
210
+ def select_state(self, stream_slice: Optional[StreamSlice] = None) -> Optional[StreamState]:
211
+ # Datetime based cursors operate over slices made up of datetime ranges. Stream state is based on the progress
212
+ # through each slice and does not belong to a specific slice. We just return stream state as it is.
213
+ return self.get_stream_state()
214
+
215
+ def _calculate_earliest_possible_value(
216
+ self, end_datetime: datetime.datetime
217
+ ) -> datetime.datetime:
218
+ lookback_delta = self._parse_timedelta(
219
+ self._lookback_window.eval(self.config) if self._lookback_window else "P0D"
220
+ )
221
+ earliest_possible_start_datetime = min(
222
+ self._start_datetime.get_datetime(self.config), end_datetime
223
+ )
224
+ try:
225
+ cursor_datetime = (
226
+ self._calculate_cursor_datetime_from_state(self.get_stream_state()) - lookback_delta
227
+ )
228
+ except OverflowError:
229
+ # cursor_datetime defers to the minimum date if it does not exist in the state. Trying to subtract
230
+ # a timedelta from the minimum datetime results in an OverflowError
231
+ cursor_datetime = self._calculate_cursor_datetime_from_state(self.get_stream_state())
232
+ return max(earliest_possible_start_datetime, cursor_datetime)
233
+
234
+ def select_best_end_datetime(self) -> datetime.datetime:
235
+ """
236
+ Returns the optimal end datetime.
237
+ This method compares the current datetime with a pre-configured end datetime
238
+ and returns the earlier of the two. If no pre-configured end datetime is set,
239
+ the current datetime is returned.
240
+
241
+ :return datetime.datetime: The best end datetime, which is either the current datetime or the pre-configured end datetime, whichever is earlier.
242
+ """
243
+ now = datetime.datetime.now(tz=self._timezone)
244
+ if not self._end_datetime:
245
+ return now
246
+ return min(self._end_datetime.get_datetime(self.config), now)
247
+
248
+ def _calculate_cursor_datetime_from_state(
249
+ self, stream_state: Mapping[str, Any]
250
+ ) -> datetime.datetime:
251
+ if self.cursor_field.eval(self.config, stream_state=stream_state) in stream_state: # type: ignore # cursor_field is converted to an InterpolatedString in __post_init__
252
+ return self.parse_date(stream_state[self.cursor_field.eval(self.config)]) # type: ignore # cursor_field is converted to an InterpolatedString in __post_init__
253
+ return datetime.datetime.min.replace(tzinfo=datetime.timezone.utc)
254
+
255
+ def _format_datetime(self, dt: datetime.datetime) -> str:
256
+ return self._parser.format(dt, self.datetime_format)
257
+
258
+ def _partition_daterange(
259
+ self,
260
+ start: datetime.datetime,
261
+ end: datetime.datetime,
262
+ step: Union[datetime.timedelta, Duration],
263
+ ) -> List[StreamSlice]:
264
+ start_field = self._partition_field_start.eval(self.config)
265
+ end_field = self._partition_field_end.eval(self.config)
266
+ dates = []
267
+
268
+ while self._is_within_date_range(start, end):
269
+ next_start = self._evaluate_next_start_date_safely(start, step)
270
+ end_date = self._get_date(next_start - self._cursor_granularity, end, min)
271
+ dates.append(
272
+ StreamSlice(
273
+ partition={},
274
+ cursor_slice={
275
+ start_field: self._format_datetime(start),
276
+ end_field: self._format_datetime(end_date),
277
+ },
278
+ )
279
+ )
280
+ start = next_start
281
+ return dates
282
+
283
+ def _is_within_date_range(self, start: datetime.datetime, end: datetime.datetime) -> bool:
284
+ if self.is_compare_strictly:
285
+ return start < end
286
+ return start <= end
287
+
288
+ def _evaluate_next_start_date_safely(
289
+ self, start: datetime.datetime, step: datetime.timedelta
290
+ ) -> datetime.datetime:
291
+ """
292
+ Given that we set the default step at datetime.timedelta.max, we will generate an OverflowError when evaluating the next start_date
293
+ This method assumes that users would never enter a step that would generate an overflow. Given that would be the case, the code
294
+ would have broken anyway.
295
+ """
296
+ try:
297
+ return start + step
298
+ except OverflowError:
299
+ return datetime.datetime.max.replace(tzinfo=datetime.timezone.utc)
300
+
301
+ def _get_date(
302
+ self,
303
+ cursor_value: datetime.datetime,
304
+ default_date: datetime.datetime,
305
+ comparator: Callable[[datetime.datetime, datetime.datetime], datetime.datetime],
306
+ ) -> datetime.datetime:
307
+ cursor_date = cursor_value or default_date
308
+ return comparator(cursor_date, default_date)
309
+
310
+ def parse_date(self, date: str) -> datetime.datetime:
311
+ for datetime_format in self.cursor_datetime_formats + [self.datetime_format]:
312
+ try:
313
+ return self._parser.parse(date, datetime_format)
314
+ except ValueError:
315
+ pass
316
+ raise ValueError(f"No format in {self.cursor_datetime_formats} matching {date}")
317
+
318
+ @classmethod
319
+ def _parse_timedelta(cls, time_str: Optional[str]) -> Union[datetime.timedelta, Duration]:
320
+ """
321
+ :return Parses an ISO 8601 durations into datetime.timedelta or Duration objects.
322
+ """
323
+ if not time_str:
324
+ return datetime.timedelta(0)
325
+ return parse_duration(time_str)
326
+
327
+ def get_request_params(
328
+ self,
329
+ *,
330
+ stream_state: Optional[StreamState] = None,
331
+ stream_slice: Optional[StreamSlice] = None,
332
+ next_page_token: Optional[Mapping[str, Any]] = None,
333
+ ) -> Mapping[str, Any]:
334
+ return self._get_request_options(RequestOptionType.request_parameter, stream_slice)
335
+
336
+ def get_request_headers(
337
+ self,
338
+ *,
339
+ stream_state: Optional[StreamState] = None,
340
+ stream_slice: Optional[StreamSlice] = None,
341
+ next_page_token: Optional[Mapping[str, Any]] = None,
342
+ ) -> Mapping[str, Any]:
343
+ return self._get_request_options(RequestOptionType.header, stream_slice)
344
+
345
+ def get_request_body_data(
346
+ self,
347
+ *,
348
+ stream_state: Optional[StreamState] = None,
349
+ stream_slice: Optional[StreamSlice] = None,
350
+ next_page_token: Optional[Mapping[str, Any]] = None,
351
+ ) -> Mapping[str, Any]:
352
+ return self._get_request_options(RequestOptionType.body_data, stream_slice)
353
+
354
+ def get_request_body_json(
355
+ self,
356
+ *,
357
+ stream_state: Optional[StreamState] = None,
358
+ stream_slice: Optional[StreamSlice] = None,
359
+ next_page_token: Optional[Mapping[str, Any]] = None,
360
+ ) -> Mapping[str, Any]:
361
+ return self._get_request_options(RequestOptionType.body_json, stream_slice)
362
+
363
+ def request_kwargs(self) -> Mapping[str, Any]:
364
+ # Never update kwargs
365
+ return {}
366
+
367
+ def _get_request_options(
368
+ self, option_type: RequestOptionType, stream_slice: Optional[StreamSlice]
369
+ ) -> Mapping[str, Any]:
370
+ options: MutableMapping[str, Any] = {}
371
+ if not stream_slice:
372
+ return options
373
+
374
+ if self.start_time_option and self.start_time_option.inject_into == option_type:
375
+ start_time_value = stream_slice.get(self._partition_field_start.eval(self.config))
376
+ self.start_time_option.inject_into_request(options, start_time_value, self.config)
377
+
378
+ if self.end_time_option and self.end_time_option.inject_into == option_type:
379
+ end_time_value = stream_slice.get(self._partition_field_end.eval(self.config))
380
+ self.end_time_option.inject_into_request(options, end_time_value, self.config)
381
+
382
+ return options
383
+
384
+ def should_be_synced(self, record: Record) -> bool:
385
+ cursor_field = self.cursor_field.eval(self.config) # type: ignore # cursor_field is converted to an InterpolatedString in __post_init__
386
+ record_cursor_value = record.get(cursor_field)
387
+ if not record_cursor_value:
388
+ self._send_log(
389
+ Level.WARN,
390
+ f"Could not find cursor field `{cursor_field}` in record. The incremental sync will assume it needs to be synced",
391
+ )
392
+ return True
393
+ latest_possible_cursor_value = self.select_best_end_datetime()
394
+ earliest_possible_cursor_value = self._calculate_earliest_possible_value(
395
+ latest_possible_cursor_value
396
+ )
397
+ return self._is_within_daterange_boundaries(
398
+ record, earliest_possible_cursor_value, latest_possible_cursor_value
399
+ )
400
+
401
+ def _is_within_daterange_boundaries(
402
+ self,
403
+ record: Record,
404
+ start_datetime_boundary: Union[datetime.datetime, str],
405
+ end_datetime_boundary: Union[datetime.datetime, str],
406
+ ) -> bool:
407
+ cursor_field = self.cursor_field.eval(self.config) # type: ignore # cursor_field is converted to an InterpolatedString in __post_init__
408
+ record_cursor_value = record.get(cursor_field)
409
+ if not record_cursor_value:
410
+ self._send_log(
411
+ Level.WARN,
412
+ f"Could not find cursor field `{cursor_field}` in record. The record will not be considered when emitting sync state",
413
+ )
414
+ return False
415
+ if isinstance(start_datetime_boundary, str):
416
+ start_datetime_boundary = self.parse_date(start_datetime_boundary)
417
+ if isinstance(end_datetime_boundary, str):
418
+ end_datetime_boundary = self.parse_date(end_datetime_boundary)
419
+ return (
420
+ start_datetime_boundary <= self.parse_date(record_cursor_value) <= end_datetime_boundary
421
+ )
422
+
423
+ def _send_log(self, level: Level, message: str) -> None:
424
+ if self.message_repository:
425
+ self.message_repository.emit_message(
426
+ AirbyteMessage(
427
+ type=Type.LOG,
428
+ log=AirbyteLogMessage(level=level, message=message),
429
+ )
430
+ )
431
+
432
+ def is_greater_than_or_equal(self, first: Record, second: Record) -> bool:
433
+ cursor_field = self.cursor_field.eval(self.config) # type: ignore # cursor_field is converted to an InterpolatedString in __post_init__
434
+ first_cursor_value = first.get(cursor_field)
435
+ second_cursor_value = second.get(cursor_field)
436
+ if first_cursor_value and second_cursor_value:
437
+ return self.parse_date(first_cursor_value) >= self.parse_date(second_cursor_value)
438
+ elif first_cursor_value:
439
+ return True
440
+ else:
441
+ return False
442
+
443
+ def set_runtime_lookback_window(self, lookback_window_in_seconds: int) -> None:
444
+ """
445
+ Updates the lookback window based on a given number of seconds if the new duration
446
+ is greater than the currently configured lookback window.
447
+
448
+ :param lookback_window_in_seconds: The lookback duration in seconds to potentially update to.
449
+ """
450
+ runtime_lookback_window = duration_isoformat(timedelta(seconds=lookback_window_in_seconds))
451
+ config_lookback = parse_duration(
452
+ self._lookback_window.eval(self.config) if self._lookback_window else "P0D"
453
+ )
454
+
455
+ # Check if the new runtime lookback window is greater than the current config lookback
456
+ if parse_duration(runtime_lookback_window) > config_lookback:
457
+ self._lookback_window = InterpolatedString.create(
458
+ runtime_lookback_window, parameters={}
459
+ )
@@ -0,0 +1,13 @@
1
+ # Copyright (c) 2024 Airbyte, Inc., all rights reserved.
2
+
3
+ from abc import ABC
4
+
5
+ from airbyte_cdk.sources.declarative.stream_slicers.stream_slicer import StreamSlicer
6
+ from airbyte_cdk.sources.streams.checkpoint.cursor import Cursor
7
+
8
+
9
+ class DeclarativeCursor(Cursor, StreamSlicer, ABC):
10
+ """
11
+ DeclarativeCursors are components that allow for checkpointing syncs. In addition to managing the fetching and updating of
12
+ state, declarative cursors also manage stream slicing and injecting slice values into outbound requests.
13
+ """