Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 54 additions & 28 deletions packages/google-cloud-storage/google/cloud/storage/grpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ def _resolve_universe_domain(client_options) -> str:
)


def _resolve_direct_path(universe_domain: str, attempt_direct_path: bool) -> bool:
"""Disables DirectPath outside the default universe."""
if universe_domain.lower() != _DEFAULT_UNIVERSE_DOMAIN.lower():
return False
return attempt_direct_path


def _resolve_grpc_config(client_options, attempt_direct_path):
"""Resolves universe domain, target host, quota project, and DirectPath flag.

DirectPath is only attempted in the default universe.
"""
universe_domain = _resolve_universe_domain(client_options)
if universe_domain.lower() != _DEFAULT_UNIVERSE_DOMAIN.lower():
attempt_direct_path = False
attempt_direct_path = _resolve_direct_path(universe_domain, attempt_direct_path)
Comment thread
nidhiii-27 marked this conversation as resolved.
Resolved

host = _get_option(client_options, "api_endpoint") or f"storage.{universe_domain}"
return (
Expand Down Expand Up @@ -132,8 +138,6 @@ def __init__(
api_key=None,
attempt_direct_path=True,
):
super(GrpcClient, self).__init__(project=project, credentials=credentials)

if isinstance(client_options, dict):
if api_key:
client_options["api_key"] = api_key
Expand All @@ -142,14 +146,38 @@ def __init__(
elif api_key:
client_options.api_key = api_key

effective_credentials = (
uses_option_credentials = bool(
_get_option(client_options, "api_key")
or _get_option(client_options, "credentials_file")
)
super_kwargs = {"project": project, "credentials": credentials}
if credentials is None:
if uses_option_credentials:
super_kwargs["client_options"] = {
key: val
for key in ("api_key", "credentials_file", "scopes")
if (val := _get_option(client_options, key))
}
elif scopes := _get_option(client_options, "scopes"):
super_kwargs["client_options"] = {"scopes": scopes}

super(GrpcClient, self).__init__(**super_kwargs)

self._universe_domain = _resolve_universe_domain(client_options)
resolved_credentials = (
credentials
if credentials is not None
else getattr(self, "_credentials", None)
)
self._universe_domain = _resolve_universe_domain(client_options)
_validate_universe_domain(self._universe_domain, resolved_credentials)

gapic_credentials = (
None
if credentials is None and uses_option_credentials
else resolved_credentials
)
self._grpc_client = self._create_gapic_client(
credentials=effective_credentials,
credentials=gapic_credentials,
client_info=client_info,
client_options=client_options,
attempt_direct_path=attempt_direct_path,
Expand All @@ -168,31 +196,29 @@ def _create_gapic_client(
attempt_direct_path=True,
):
"""Creates and configures the low-level GAPIC `storage_v2` client."""
(
universe_domain,
host,
quota_project_id,
attempt_direct_path,
) = _resolve_grpc_config(client_options, attempt_direct_path)
_validate_universe_domain(universe_domain, credentials)

direct_path = _resolve_direct_path(self._universe_domain, attempt_direct_path)
transport_cls = storage_v2.StorageClient.get_transport_class("grpc")
channel_kwargs = {
"host": host,

def channel_factory(*args, **channel_kwargs):
channel_kwargs["attempt_direct_path"] = (
False
if channel_kwargs.get("ssl_credentials") is not None
else direct_path
)
return transport_cls.create_channel(*args, **channel_kwargs)

def transport_factory(**transport_kwargs):
return transport_cls(channel=channel_factory, **transport_kwargs)

client_kwargs = {
"credentials": credentials,
"attempt_direct_path": attempt_direct_path,
"transport": transport_factory,
"client_options": client_options,
}
if quota_project_id is not None:
channel_kwargs["quota_project_id"] = quota_project_id
if client_info is not None:
client_kwargs["client_info"] = client_info

channel = transport_cls.create_channel(**channel_kwargs)
transport = transport_cls(host=host, channel=channel)

return storage_v2.StorageClient(
transport=transport,
client_info=client_info,
client_options=client_options,
)
return storage_v2.StorageClient(**client_kwargs)

@property
def grpc_client(self):
Expand Down
Loading
Loading