Channel¶
Recording channel wrappers that intercept gRPC calls for recording and playback.
Overview¶
grpcvcr provides channel wrappers that intercept all gRPC calls, recording them during the first run and replaying them from the cassette on subsequent runs.
from grpcvcr import RecordMode, recorded_channel
# Simple usage with context manager
with recorded_channel("test.yaml", "localhost:50051", record_mode=RecordMode.ALL) as channel:
stub = MyServiceStub(channel)
response = stub.GetUser(GetUserRequest(id=1))
API Reference¶
A gRPC channel wrapper that records and plays back interactions.
Wraps a standard gRPC channel with interceptors that record all RPC calls to a cassette for later playback.
Example
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cassette
|
Cassette
|
The cassette to record to or play back from. |
required |
target
|
str
|
The gRPC server address (e.g., 'localhost:50051'). |
required |
credentials
|
ChannelCredentials | None
|
Optional channel credentials for secure channels. |
None
|
options
|
list[tuple[str, str]] | None
|
Optional gRPC channel options. |
None
|
Source code in src/grpcvcr/channel.py
channel
instance-attribute
¶
The wrapped gRPC channel with recording interceptors.
__enter__ ¶
__enter__() -> RecordingChannel
Context manager for a recorded gRPC channel.
This is the simplest API for recording and playing back gRPC calls. Creates a cassette and channel, yields the channel, then saves the cassette on exit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the cassette file. |
required |
target
|
str
|
The gRPC server address. |
required |
record_mode
|
RecordMode
|
How to handle recording vs playback. |
NEW_EPISODES
|
match_on
|
Matcher | None
|
Matcher to use for finding recorded interactions. |
None
|
credentials
|
ChannelCredentials | None
|
Optional channel credentials for secure channels. |
None
|
options
|
list[tuple[str, str]] | None
|
Optional gRPC channel options. |
None
|
Yields:
| Type | Description |
|---|---|
Channel
|
A gRPC channel that records/plays back interactions. |
Example
from grpcvcr import recorded_channel, RecordMode
# Record interactions
with recorded_channel("test.yaml", "localhost:50051", record_mode=RecordMode.ALL) as channel:
stub = MyServiceStub(channel)
response = stub.GetUser(GetUserRequest(id=1))
# Play back interactions (no network calls made)
with recorded_channel("test.yaml", "localhost:50051", record_mode=RecordMode.NONE) as channel:
stub = MyServiceStub(channel)
response = stub.GetUser(GetUserRequest(id=1))