Skip to content

Commit 594d3f5

Browse files
author
Test User
committed
fix: PR comments and CI failures
* Added clarifying comment on the chunk_size logic. * Fixed an issue when logger would crash while attempting to read the text of a mocked response that has an empty body
1 parent a7a6548 commit 594d3f5

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

packages/google-api-core/google/api_core/resumable_media/_upload.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ def invalid(self) -> bool:
7272

7373
@property
7474
def chunk_size(self) -> int:
75-
"""int: The block-aligned chunk size informed by server granularity."""
75+
"""int: The block-aligned chunk size informed by server granularity.
76+
77+
All chunks (except the last one) must have a size that is a multiple
78+
of the server's chunk granularity. We round the user's requested size
79+
up to the nearest granularity multiple to ensure all intermediate
80+
chunks conform to this protocol constraint.
81+
"""
7682
if self._chunk_granularity:
7783
return (
7884
(self._chunk_size + self._chunk_granularity - 1)

packages/google-api-core/google/api_core/resumable_media/requests_upload.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,16 @@ def _log_request(
186186
def _log_response(self, response: requests.Response):
187187
if not _LOGGER.isEnabledFor(logging.DEBUG):
188188
return
189+
try:
190+
body = response.text
191+
except (ValueError, AttributeError):
192+
body = "<unavailable or closed>"
189193
_LOGGER.debug(
190194
"HTTP Response: %s %s\nHeaders: %s\nBody: %s",
191195
response.status_code,
192196
response.reason,
193197
response.headers,
194-
response.text,
198+
body,
195199
)
196200

197201
def initiate(

packages/google-api-core/tests/unit/resumable_media/test_requests_upload.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ def test_deadline_exceeded():
4343

4444

4545
@responses.activate
46-
def test_happy_path_upload():
46+
def test_happy_path_upload(caplog):
47+
import logging
48+
49+
caplog.set_level(
50+
logging.DEBUG, logger="google.api_core.resumable_media.requests_upload"
51+
)
52+
4753
initial_url = "http://example.com/start"
4854
session_url = "http://example.com/session/123"
4955
data = b"hello world"
@@ -70,7 +76,7 @@ def test_happy_path_upload():
7076
session = requests.Session()
7177
response = requests_upload.make_resumable_upload(
7278
transport=session,
73-
request_body="",
79+
request_body="test-initiate-metadata",
7480
stream=stream,
7581
upload_url=initial_url,
7682
size=len(data),
@@ -90,6 +96,13 @@ def test_happy_path_upload():
9096
== "upload, finalize"
9197
)
9298

99+
# Check that the initiation and chunk response logs are successfully written without raising exceptions
100+
assert f"HTTP Request: POST {initial_url}" in caplog.text
101+
assert "Body: test-initiate-metadata" in caplog.text
102+
assert f"HTTP Request: POST {session_url}" in caplog.text
103+
assert "HTTP Response: 200" in caplog.text
104+
assert "Body: Success" in caplog.text
105+
93106

94107
@responses.activate
95108
def test_upload_with_retry():

0 commit comments

Comments
 (0)