Skip to content

Commit accb603

Browse files
Add VCR auto-recording in platform debug mode
execute_action() now detects KBC_COMPONENT_RUN_MODE=debug and automatically records HTTP interactions via keboola.vcr. Components can define a module-level VCR_SANITIZERS list for custom sanitizers. Add keboola.vcr as a dependency. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9854034 commit accb603

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ dependencies = [
55
"pygelf",
66
"pytz<2021.0",
77
"deprecated",
8+
"keboola.vcr",
89
]
910
requires-python = ">=3.8"
1011

src/keboola/component/base.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,17 @@ def execute_action(self):
236236
"""
237237
Executes action defined in the configuration.
238238
The default action is 'run'. See base._SYNC_ACTION_MAPPING
239+
240+
When ``KBC_COMPONENT_RUN_MODE=debug`` is set (platform debug mode),
241+
the action execution is automatically wrapped with VCR recording
242+
so that HTTP interactions are captured for later replay in tests.
239243
"""
244+
if self._should_vcr_record():
245+
return self._execute_with_vcr_recording()
246+
return self._do_execute_action()
247+
248+
def _do_execute_action(self):
249+
"""Internal: runs the actual action dispatch."""
240250
action = self.configuration.action
241251
if not action:
242252
logging.warning("No action defined in the configuration, using the default run action.")
@@ -249,6 +259,22 @@ def execute_action(self):
249259
raise AttributeError(f"The defined action {action} is not implemented!") from e
250260
return action_method()
251261

262+
@staticmethod
263+
def _should_vcr_record():
264+
"""Check if running in platform debug mode."""
265+
return os.environ.get("KBC_COMPONENT_RUN_MODE", "").lower() == "debug"
266+
267+
def _execute_with_vcr_recording(self):
268+
"""Wrap action execution with VCR recording for debug runs."""
269+
import inspect
270+
from keboola.vcr import VCRRecorder
271+
272+
module = inspect.getmodule(type(self))
273+
VCRRecorder.record_debug_run(
274+
self._do_execute_action,
275+
sanitizers=getattr(module, 'VCR_SANITIZERS', None),
276+
)
277+
252278
def _generate_table_metadata_legacy(self, table_schema: ts.TableSchema) -> dao.TableMetadata:
253279
"""
254280
Generates a TableMetadata object for the table definition using a TableSchema object.

0 commit comments

Comments
 (0)