1# Copyright 2014 Google LLC 
    2# 
    3# Licensed under the Apache License, Version 2.0 (the "License"); 
    4# you may not use this file except in compliance with the License. 
    5# You may obtain a copy of the License at 
    6# 
    7#     http://www.apache.org/licenses/LICENSE-2.0 
    8# 
    9# Unless required by applicable law or agreed to in writing, software 
    10# distributed under the License is distributed on an "AS IS" BASIS, 
    11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    12# See the License for the specific language governing permissions and 
    13# limitations under the License. 
    14 
    15"""Create / interact with Google Cloud Storage connections.""" 
    16 
    17import functools 
    18from google.cloud import _http 
    19from google.cloud.storage import __version__ 
    20from google.cloud.storage import _helpers 
    21from google.cloud.storage._opentelemetry_tracing import create_trace_span 
    22 
    23 
    24class Connection(_http.JSONConnection): 
    25    """A connection to Google Cloud Storage via the JSON REST API. 
    26 
    27    Mutual TLS will be enabled if the "GOOGLE_API_USE_CLIENT_CERTIFICATE" 
    28    environment variable is set to the exact string "true" (case-sensitive). 
    29 
    30    Mutual TLS is not compatible with any API endpoint or universe domain 
    31    override at this time. If such settings are enabled along with 
    32    "GOOGLE_API_USE_CLIENT_CERTIFICATE", a ValueError will be raised. 
    33 
    34    :type client: :class:`~google.cloud.storage.client.Client` 
    35    :param client: The client that owns the current connection. 
    36 
    37    :type client_info: :class:`~google.api_core.client_info.ClientInfo` 
    38    :param client_info: (Optional) instance used to generate user agent. 
    39 
    40    :type api_endpoint: str 
    41    :param api_endpoint: (Optional) api endpoint to use. 
    42    """ 
    43 
    44    DEFAULT_API_ENDPOINT = _helpers._get_default_storage_base_url() 
    45    DEFAULT_API_MTLS_ENDPOINT = "https://storage.mtls.googleapis.com" 
    46 
    47    def __init__(self, client, client_info=None, api_endpoint=None): 
    48        super(Connection, self).__init__(client, client_info) 
    49        self.API_BASE_URL = api_endpoint or self.DEFAULT_API_ENDPOINT 
    50        self.API_BASE_MTLS_URL = self.DEFAULT_API_MTLS_ENDPOINT 
    51        self.ALLOW_AUTO_SWITCH_TO_MTLS_URL = api_endpoint is None 
    52        self._client_info.client_library_version = __version__ 
    53 
    54        # TODO: When metrics all use gccl, this should be removed #9552 
    55        if self._client_info.user_agent is None:  # pragma: no branch 
    56            self._client_info.user_agent = "" 
    57        agent_version = f"gcloud-python/{__version__}" 
    58        if agent_version not in self._client_info.user_agent: 
    59            self._client_info.user_agent += f" {agent_version} " 
    60 
    61    API_VERSION = _helpers._API_VERSION 
    62    """The version of the API, used in building the API call's URL.""" 
    63 
    64    API_URL_TEMPLATE = "{api_base_url}/storage/{api_version}{path}" 
    65    """A template for the URL of a particular API call.""" 
    66 
    67    def api_request(self, *args, **kwargs): 
    68        retry = kwargs.pop("retry", None) 
    69        invocation_id = _helpers._get_invocation_id() 
    70        kwargs["extra_api_info"] = invocation_id 
    71        span_attributes = { 
    72            "gccl-invocation-id": invocation_id, 
    73        } 
    74        call = functools.partial(super(Connection, self).api_request, *args, **kwargs) 
    75        with create_trace_span( 
    76            name="Storage.Connection.api_request", 
    77            attributes=span_attributes, 
    78            client=self._client, 
    79            api_request=kwargs, 
    80            retry=retry, 
    81        ): 
    82            if retry: 
    83                # If this is a ConditionalRetryPolicy, check conditions. 
    84                try: 
    85                    retry = retry.get_retry_policy_if_conditions_met(**kwargs) 
    86                except AttributeError:  # This is not a ConditionalRetryPolicy. 
    87                    pass 
    88                if retry: 
    89                    call = retry(call) 
    90            return call()