Skip to content

Commit 561e4a8

Browse files
committed
feat(storage): Move common methods to abstract class
1 parent ee7f830 commit 561e4a8

File tree

2 files changed

+99
-99
lines changed

2 files changed

+99
-99
lines changed

google/cloud/storage/abstracts/base_client.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from google.cloud.storage._helpers import _DEFAULT_SCHEME
2020
from google.cloud.storage._helpers import _STORAGE_HOST_TEMPLATE
2121
from google.auth.credentials import AnonymousCredentials
22+
from google.cloud.storage._helpers import _DEFAULT_UNIVERSE_DOMAIN
2223
from google.cloud.client import ClientWithProject
2324
from google.cloud._helpers import _LocalStack
2425
from google.auth.transport import mtls
@@ -192,3 +193,101 @@ def __init__(
192193

193194
self.connection_kw_args = connection_kw_args
194195
self._batch_stack = _LocalStack()
196+
197+
@property
198+
def universe_domain(self):
199+
return self._universe_domain or _DEFAULT_UNIVERSE_DOMAIN
200+
201+
@classmethod
202+
def create_anonymous_client(cls):
203+
"""Factory: return client with anonymous credentials.
204+
205+
.. note::
206+
207+
Such a client has only limited access to "public" buckets:
208+
listing their contents and downloading their blobs.
209+
210+
:rtype: :class:`google.cloud.storage.client.Client`
211+
:returns: Instance w/ anonymous credentials and no project.
212+
"""
213+
client = cls(project="<none>", credentials=AnonymousCredentials())
214+
client.project = None
215+
return client
216+
217+
@property
218+
def api_endpoint(self):
219+
"""Returns the API_BASE_URL from connection"""
220+
return self._connection.API_BASE_URL
221+
222+
def update_user_agent(self, user_agent):
223+
"""Update the user-agent string for this client.
224+
225+
:type user_agent: str
226+
:param user_agent: The string to add to the user-agent.
227+
"""
228+
existing_user_agent = self._connection._client_info.user_agent
229+
if existing_user_agent is None:
230+
self._connection.user_agent = user_agent
231+
else:
232+
self._connection.user_agent = f"{user_agent} {existing_user_agent}"
233+
234+
@property
235+
def _connection(self):
236+
"""Get connection or batch on the client.
237+
238+
:rtype: :class:`google.cloud.storage._http.Connection`
239+
:returns: The connection set on the client, or the batch
240+
if one is set.
241+
"""
242+
if self.current_batch is not None:
243+
return self.current_batch
244+
else:
245+
return self._base_connection
246+
247+
@_connection.setter
248+
def _connection(self, value):
249+
"""Set connection on the client.
250+
251+
Intended to be used by constructor (since the base class calls)
252+
self._connection = connection
253+
Will raise if the connection is set more than once.
254+
255+
:type value: :class:`google.cloud.storage._http.Connection`
256+
:param value: The connection set on the client.
257+
258+
:raises: :class:`ValueError` if connection has already been set.
259+
"""
260+
if self._base_connection is not None:
261+
raise ValueError("Connection already set on client")
262+
self._base_connection = value
263+
264+
def _push_batch(self, batch):
265+
"""Push a batch onto our stack.
266+
267+
"Protected", intended for use by batch context mgrs.
268+
269+
:type batch: :class:`google.cloud.storage.batch.Batch`
270+
:param batch: newly-active batch
271+
"""
272+
self._batch_stack.push(batch)
273+
274+
def _pop_batch(self):
275+
"""Pop a batch from our stack.
276+
277+
"Protected", intended for use by batch context mgrs.
278+
279+
:raises: IndexError if the stack is empty.
280+
:rtype: :class:`google.cloud.storage.batch.Batch`
281+
:returns: the top-most batch/transaction, after removing it.
282+
"""
283+
return self._batch_stack.pop()
284+
285+
@property
286+
def current_batch(self):
287+
"""Currently-active batch.
288+
289+
:rtype: :class:`google.cloud.storage.batch.Batch` or ``NoneType`` (if
290+
no batch is active).
291+
:returns: The batch at the top of the batch stack.
292+
"""
293+
return self._batch_stack.top

google/cloud/storage/client.py

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@
2323
import os
2424
import warnings
2525

26-
from google.auth.credentials import AnonymousCredentials
2726
from google.api_core import page_iterator
2827
from google.cloud.exceptions import NotFound
2928
from google.cloud.storage._helpers import _add_generation_match_parameters
3029
from google.cloud.storage._helpers import _bucket_bound_hostname_url
3130
from google.cloud.storage._helpers import _get_environ_project
3231
from google.cloud.storage._helpers import _virtual_hosted_style_base_url
33-
from google.cloud.storage._helpers import _DEFAULT_UNIVERSE_DOMAIN
3432
from google.cloud.storage._helpers import _NOW
3533
from google.cloud.storage._helpers import _UTC
3634
from google.cloud.storage._opentelemetry_tracing import create_trace_span
@@ -151,103 +149,6 @@ def __init__(
151149
self._connection = connection
152150

153151

154-
@classmethod
155-
def create_anonymous_client(cls):
156-
"""Factory: return client with anonymous credentials.
157-
158-
.. note::
159-
160-
Such a client has only limited access to "public" buckets:
161-
listing their contents and downloading their blobs.
162-
163-
:rtype: :class:`google.cloud.storage.client.Client`
164-
:returns: Instance w/ anonymous credentials and no project.
165-
"""
166-
client = cls(project="<none>", credentials=AnonymousCredentials())
167-
client.project = None
168-
return client
169-
170-
@property
171-
def universe_domain(self):
172-
return self._universe_domain or _DEFAULT_UNIVERSE_DOMAIN
173-
174-
@property
175-
def api_endpoint(self):
176-
return self._connection.API_BASE_URL
177-
178-
def update_user_agent(self, user_agent):
179-
"""Update the user-agent string for this client.
180-
181-
:type user_agent: str
182-
:param user_agent: The string to add to the user-agent.
183-
"""
184-
existing_user_agent = self._connection._client_info.user_agent
185-
if existing_user_agent is None:
186-
self._connection.user_agent = user_agent
187-
else:
188-
self._connection.user_agent = f"{user_agent} {existing_user_agent}"
189-
190-
@property
191-
def _connection(self):
192-
"""Get connection or batch on the client.
193-
194-
:rtype: :class:`google.cloud.storage._http.Connection`
195-
:returns: The connection set on the client, or the batch
196-
if one is set.
197-
"""
198-
if self.current_batch is not None:
199-
return self.current_batch
200-
else:
201-
return self._base_connection
202-
203-
@_connection.setter
204-
def _connection(self, value):
205-
"""Set connection on the client.
206-
207-
Intended to be used by constructor (since the base class calls)
208-
self._connection = connection
209-
Will raise if the connection is set more than once.
210-
211-
:type value: :class:`google.cloud.storage._http.Connection`
212-
:param value: The connection set on the client.
213-
214-
:raises: :class:`ValueError` if connection has already been set.
215-
"""
216-
if self._base_connection is not None:
217-
raise ValueError("Connection already set on client")
218-
self._base_connection = value
219-
220-
def _push_batch(self, batch):
221-
"""Push a batch onto our stack.
222-
223-
"Protected", intended for use by batch context mgrs.
224-
225-
:type batch: :class:`google.cloud.storage.batch.Batch`
226-
:param batch: newly-active batch
227-
"""
228-
self._batch_stack.push(batch)
229-
230-
def _pop_batch(self):
231-
"""Pop a batch from our stack.
232-
233-
"Protected", intended for use by batch context mgrs.
234-
235-
:raises: IndexError if the stack is empty.
236-
:rtype: :class:`google.cloud.storage.batch.Batch`
237-
:returns: the top-most batch/transaction, after removing it.
238-
"""
239-
return self._batch_stack.pop()
240-
241-
@property
242-
def current_batch(self):
243-
"""Currently-active batch.
244-
245-
:rtype: :class:`google.cloud.storage.batch.Batch` or ``NoneType`` (if
246-
no batch is active).
247-
:returns: The batch at the top of the batch stack.
248-
"""
249-
return self._batch_stack.top
250-
251152
def get_service_account_email(
252153
self, project=None, timeout=_DEFAULT_TIMEOUT, retry=DEFAULT_RETRY
253154
):

0 commit comments

Comments
 (0)