Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions google/genai/_replay_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,32 @@ def _redact_project_location_path(path: str) -> str:
return path


def _redact_request_body(body: dict[str, object]) -> None:
def _redact_request_body(body: Any) -> None:
"""Redacts fields in the request body in place."""
for key, value in body.items():
if isinstance(value, str):
body[key] = _redact_project_location_path(value)
if isinstance(body, dict):
for key, value in body.items():
if isinstance(value, str):
value = _redact_project_location_path(value)
value = re.sub(
r'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',
'{UUID}',
value,
)
body[key] = value
elif isinstance(value, (dict, list)):
_redact_request_body(value)
elif isinstance(body, list):
for i, value in enumerate(body):
if isinstance(value, str):
value = _redact_project_location_path(value)
value = re.sub(
r'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',
'{UUID}',
value,
)
body[i] = value
elif isinstance(value, (dict, list)):
_redact_request_body(value)


def redact_http_request(http_request: HttpRequest) -> None:
Expand Down Expand Up @@ -433,7 +454,10 @@ def _match_request(
_redact_request_body(request_data_copy)

actual_request_body = [request_data_copy]
expected_request_body = interaction.request.body_segments
expected_request_body = copy.deepcopy(interaction.request.body_segments)
for segment in expected_request_body:
if not isinstance(segment, bytes):
_redact_request_body(segment)
assert _equals_ignore_key_case(actual_request_body, expected_request_body), (
'Request body mismatch:\n'
f'Actual: {actual_request_body}\n'
Expand Down
Loading