Skip to content

Commit 91fc6d9

Browse files
authored
fix: update batch connection to request api endpoint info from client (#392)
* bug: let Batch Connection request api endpoint info from Client * add unit test coverage Co-authored-by: Cathy Ouyang <[email protected]>
1 parent d346c94 commit 91fc6d9

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

google/cloud/storage/batch.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ class Batch(Connection):
147147
_MAX_BATCH_SIZE = 1000
148148

149149
def __init__(self, client):
150-
super(Batch, self).__init__(client)
150+
api_endpoint = client._connection.API_BASE_URL
151+
client_info = client._connection._client_info
152+
super(Batch, self).__init__(
153+
client, client_info=client_info, api_endpoint=api_endpoint
154+
)
151155
self._requests = []
152156
self._target_objects = []
153157

tests/unit/test_batch.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ def test__make_request_GET_normal(self):
136136
url = "http://example.com/api"
137137
http = _make_requests_session([])
138138
connection = _Connection(http=http)
139-
batch = self._make_one(connection)
139+
client = _Client(connection)
140+
batch = self._make_one(client)
140141
target = _MockObject()
141142

142143
response = batch._make_request("GET", url, target_object=target)
@@ -164,7 +165,8 @@ def test__make_request_POST_normal(self):
164165
url = "http://example.com/api"
165166
http = _make_requests_session([])
166167
connection = _Connection(http=http)
167-
batch = self._make_one(connection)
168+
client = _Client(connection)
169+
batch = self._make_one(client)
168170
data = {"foo": 1}
169171
target = _MockObject()
170172

@@ -191,7 +193,8 @@ def test__make_request_PATCH_normal(self):
191193
url = "http://example.com/api"
192194
http = _make_requests_session([])
193195
connection = _Connection(http=http)
194-
batch = self._make_one(connection)
196+
client = _Client(connection)
197+
batch = self._make_one(client)
195198
data = {"foo": 1}
196199
target = _MockObject()
197200

@@ -218,7 +221,8 @@ def test__make_request_DELETE_normal(self):
218221
url = "http://example.com/api"
219222
http = _make_requests_session([])
220223
connection = _Connection(http=http)
221-
batch = self._make_one(connection)
224+
client = _Client(connection)
225+
batch = self._make_one(client)
222226
target = _MockObject()
223227

224228
response = batch._make_request("DELETE", url, target_object=target)
@@ -243,7 +247,8 @@ def test__make_request_POST_too_many_requests(self):
243247
url = "http://example.com/api"
244248
http = _make_requests_session([])
245249
connection = _Connection(http=http)
246-
batch = self._make_one(connection)
250+
client = _Client(connection)
251+
batch = self._make_one(client)
247252

248253
batch._MAX_BATCH_SIZE = 1
249254
batch._requests.append(("POST", url, {}, {"bar": 2}))
@@ -254,7 +259,8 @@ def test__make_request_POST_too_many_requests(self):
254259
def test_finish_empty(self):
255260
http = _make_requests_session([])
256261
connection = _Connection(http=http)
257-
batch = self._make_one(connection)
262+
client = _Client(connection)
263+
batch = self._make_one(client)
258264

259265
with self.assertRaises(ValueError):
260266
batch.finish()
@@ -518,6 +524,25 @@ def test_as_context_mgr_w_error(self):
518524
self.assertIsInstance(target2._properties, _FutureDict)
519525
self.assertIsInstance(target3._properties, _FutureDict)
520526

527+
def test_respect_client_existing_connection(self):
528+
client_endpoint = "http://localhost:9023"
529+
http = _make_requests_session([])
530+
connection = _Connection(http=http, api_endpoint=client_endpoint)
531+
client = _Client(connection)
532+
batch = self._make_one(client)
533+
self.assertEqual(batch.API_BASE_URL, client_endpoint)
534+
self.assertEqual(batch._client._connection.API_BASE_URL, client_endpoint)
535+
536+
def test_use_default_api_without_existing_connection(self):
537+
default_api_endpoint = "https://storage.googleapis.com"
538+
http = _make_requests_session([])
539+
connection = _Connection(http=http)
540+
client = _Client(connection)
541+
batch = self._make_one(client)
542+
self.assertEqual(batch.API_BASE_URL, default_api_endpoint)
543+
self.assertIsNone(batch._client._connection.API_BASE_URL)
544+
self.assertIsNone(batch._client._connection._client_info)
545+
521546

522547
class Test__unpack_batch_response(unittest.TestCase):
523548
def _call_fut(self, headers, content):
@@ -633,6 +658,8 @@ class _Connection(object):
633658

634659
def __init__(self, **kw):
635660
self.__dict__.update(kw)
661+
self._client_info = kw.get("client_info", None)
662+
self.API_BASE_URL = kw.get("api_endpoint", None)
636663

637664
def _make_request(self, method, url, data=None, headers=None, timeout=None):
638665
return self.http.request(
@@ -647,3 +674,4 @@ class _MockObject(object):
647674
class _Client(object):
648675
def __init__(self, connection):
649676
self._base_connection = connection
677+
self._connection = connection

0 commit comments

Comments
 (0)