|
27 | 27 | import io |
28 | 28 | import json |
29 | 29 | import logging |
| 30 | +import math |
30 | 31 | import os |
31 | 32 | import sys |
32 | 33 | import time |
@@ -97,6 +98,14 @@ def _patch_http_options( |
97 | 98 | return copy_option |
98 | 99 |
|
99 | 100 |
|
| 101 | +def _populate_server_timeout_header( |
| 102 | + headers: dict[str, str], timeout_in_seconds: Optional[Union[float, int]] |
| 103 | +) -> None: |
| 104 | + """Populates the server timeout header in the headers dict.""" |
| 105 | + if timeout_in_seconds and 'X-Server-Timeout' not in headers: |
| 106 | + headers['X-Server-Timeout'] = str(math.ceil(timeout_in_seconds)) |
| 107 | + |
| 108 | + |
100 | 109 | def _join_url_path(base_url: str, path: str) -> str: |
101 | 110 | parsed_base = urlparse(base_url) |
102 | 111 | base_path = ( |
@@ -539,6 +548,9 @@ def _build_request( |
539 | 548 |
|
540 | 549 | if patched_http_options.headers is None: |
541 | 550 | raise ValueError('Request headers must be set.') |
| 551 | + _populate_server_timeout_header( |
| 552 | + patched_http_options.headers, timeout_in_seconds |
| 553 | + ) |
542 | 554 | return HttpRequest( |
543 | 555 | method=http_method, |
544 | 556 | url=url, |
@@ -795,14 +807,16 @@ def _upload_fd( |
795 | 807 | else self._http_options.timeout |
796 | 808 | ) |
797 | 809 | timeout_in_seconds = _get_timeout_in_seconds(timeout) |
| 810 | + upload_headers = { |
| 811 | + 'X-Goog-Upload-Command': upload_command, |
| 812 | + 'X-Goog-Upload-Offset': str(offset), |
| 813 | + 'Content-Length': str(chunk_size), |
| 814 | + } |
| 815 | + _populate_server_timeout_header(upload_headers, timeout_in_seconds) |
798 | 816 | response = self._httpx_client.request( |
799 | 817 | method='POST', |
800 | 818 | url=upload_url, |
801 | | - headers={ |
802 | | - 'X-Goog-Upload-Command': upload_command, |
803 | | - 'X-Goog-Upload-Offset': str(offset), |
804 | | - 'Content-Length': str(chunk_size), |
805 | | - }, |
| 819 | + headers=upload_headers, |
806 | 820 | content=file_chunk, |
807 | 821 | timeout=timeout_in_seconds, |
808 | 822 | ) |
@@ -941,15 +955,17 @@ async def _async_upload_fd( |
941 | 955 | else self._http_options.timeout |
942 | 956 | ) |
943 | 957 | timeout_in_seconds = _get_timeout_in_seconds(timeout) |
| 958 | + upload_headers = { |
| 959 | + 'X-Goog-Upload-Command': upload_command, |
| 960 | + 'X-Goog-Upload-Offset': str(offset), |
| 961 | + 'Content-Length': str(chunk_size), |
| 962 | + } |
| 963 | + _populate_server_timeout_header(upload_headers, timeout_in_seconds) |
944 | 964 | response = await self._async_httpx_client.request( |
945 | 965 | method='POST', |
946 | 966 | url=upload_url, |
947 | 967 | content=file_chunk, |
948 | | - headers={ |
949 | | - 'X-Goog-Upload-Command': upload_command, |
950 | | - 'X-Goog-Upload-Offset': str(offset), |
951 | | - 'Content-Length': str(chunk_size), |
952 | | - }, |
| 968 | + headers=upload_headers, |
953 | 969 | timeout=timeout_in_seconds, |
954 | 970 | ) |
955 | 971 | offset += chunk_size |
|
0 commit comments