Skip to content

Commit 4341ce9

Browse files
Added VCR replay if cassettes file is found in data/cassettes
1 parent 4357bc4 commit 4341ce9

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

src/keboola/component/base.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,15 @@ def execute_action(self):
237237
Executes action defined in the configuration.
238238
The default action is 'run'. See base._SYNC_ACTION_MAPPING
239239
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.
240+
Auto-detects two special modes (transparent to the caller):
241+
242+
- If ``{KBC_DATADIR}/cassettes/requests.json`` exists, replays HTTP
243+
interactions from the cassette instead of hitting the real API.
244+
- If ``KBC_COMPONENT_RUN_MODE=debug`` is set (platform debug mode),
245+
records HTTP interactions via keboola.vcr for later use as a cassette.
243246
"""
247+
if self._should_vcr_replay():
248+
return self._execute_with_vcr_replay()
244249
if self._should_vcr_record():
245250
return self._execute_with_vcr_recording()
246251
return self._do_execute_action()
@@ -259,6 +264,20 @@ def _do_execute_action(self):
259264
raise AttributeError(f"The defined action {action} is not implemented!") from e
260265
return action_method()
261266

267+
@staticmethod
268+
def _should_vcr_replay() -> bool:
269+
"""Check if a VCR cassette exists at the default location in data/cassettes/."""
270+
data_dir = os.environ.get("KBC_DATADIR", "/data")
271+
return (Path(data_dir) / "cassettes" / "requests.json").exists()
272+
273+
def _execute_with_vcr_replay(self) -> None:
274+
"""Replay HTTP interactions from data/cassettes/requests.json."""
275+
from keboola.vcr import VCRRecorder
276+
277+
data_dir = os.environ.get("KBC_DATADIR", "/data")
278+
recorder = VCRRecorder(cassette_dir=Path(data_dir) / "cassettes")
279+
recorder.replay(self._do_execute_action)
280+
262281
@staticmethod
263282
def _should_vcr_record():
264283
"""Check if running in platform debug mode."""

0 commit comments

Comments
 (0)