Skip to content

Commit 82c3452

Browse files
author
Test User
committed
feat: phase 3 implementation
1 parent 0435830 commit 82c3452

File tree

9 files changed

+2318
-9
lines changed

9 files changed

+2318
-9
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@
1313
# limitations under the License.
1414

1515
"""Resumable media library for Google APIs."""
16+
17+
from .requests_upload import make_resumable_upload
18+
from .requests_upload import RequestsResumableUpload
19+
from .requests_upload import ResumableUploadStatus
20+
21+
__all__ = [
22+
"make_resumable_upload",
23+
"RequestsResumableUpload",
24+
"ResumableUploadStatus",
25+
]

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,10 @@ class UploadStatus:
7878
ACTIVE = "active"
7979
FINAL = "final"
8080
CANCELLED = "cancelled"
81+
82+
83+
# HTTP status codes that indicate a transient error and can be retried.
84+
RETRYABLE_STATUS_CODES = (408, 429, 500, 502, 503, 504)
85+
86+
# HTTP status codes that indicate a state mismatch and require modification (recovery).
87+
RECOVERABLE_STATUS_CODES = (400, 412, 416)

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
"""
1919

2020
import logging
21-
from typing import Any, Dict, Optional, Sequence, Tuple, Union
21+
from typing import Dict, Optional, Tuple
2222

23+
from google.api_core import exceptions
2324
from google.api_core.resumable_media import _common
2425

2526
_LOGGER = logging.getLogger(__name__)
@@ -80,7 +81,7 @@ def chunk_size(self) -> int:
8081
) * self._chunk_granularity
8182
return actual_chunk_size
8283

83-
def initiate_request(
84+
def build_initiate_request(
8485
self,
8586
stream_metadata: Optional[Dict[str, str]] = None,
8687
content_type: Optional[str] = None,
@@ -90,8 +91,8 @@ def initiate_request(
9091
9192
Args:
9293
stream_metadata (Optional[Dict[str, str]]): Additional headers for
93-
the upload initiation request. These headers are ONLY applied to
94-
the initial request and will NOT be included in subsequent chunk
94+
the upload initiation request. These headers are ONLY applied to
95+
the initial request and will NOT be included in subsequent chunk
9596
upload requests. If not specified, no additional headers will be appended.
9697
content_type (Optional[str]): MIME type of the uploaded content.
9798
If not specified, the `X-Goog-Upload-Header-Content-Type` header
@@ -147,6 +148,14 @@ def process_initiate_response(
147148
if granularity:
148149
self._chunk_granularity = int(granularity)
149150

151+
def process_initiate_error(self, exc: Exception) -> None:
152+
"""Processes an error from the initiation request.
153+
154+
Args:
155+
exc (Exception): The exception raised during initiation.
156+
"""
157+
self._invalid = True
158+
150159
def build_chunk_request(
151160
self, data: bytes, final: bool = False
152161
) -> Tuple[str, str, Dict[str, str], bytes]:
@@ -212,6 +221,23 @@ def process_chunk_response(
212221
elif status == _common.UploadStatus.CANCELLED:
213222
self._invalid = True
214223

224+
def process_chunk_error(self, exc: Exception) -> bool:
225+
"""Processes an error from the chunk upload request.
226+
227+
Args:
228+
exc (Exception): The exception raised during chunk upload.
229+
230+
Returns:
231+
bool: True if the error is recoverable and requires a recovery query,
232+
False otherwise.
233+
"""
234+
if isinstance(exc, exceptions.GoogleAPICallError):
235+
if exc.code in _common.RECOVERABLE_STATUS_CODES:
236+
return True
237+
238+
self._invalid = True
239+
return False
240+
215241
def build_recovery_request(self) -> Tuple[str, str, Dict[str, str], bytes]:
216242
"""Constructs a request to query the server's current upload state.
217243
@@ -255,3 +281,11 @@ def process_recovery_response(
255281
raise RuntimeError("Upload was cancelled by server")
256282

257283
return self._bytes_uploaded
284+
285+
def process_recovery_error(self, exc: Exception) -> None:
286+
"""Processes an error from the recovery query request.
287+
288+
Args:
289+
exc (Exception): The exception raised during recovery.
290+
"""
291+
self._invalid = True

0 commit comments

Comments
 (0)