Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/storage/_http.py: 36%

33 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-25 06:17 +0000

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 

21 

22 

23class Connection(_http.JSONConnection): 

24 """A connection to Google Cloud Storage via the JSON REST API. Mutual TLS feature will be 

25 enabled if `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true". 

26 

27 :type client: :class:`~google.cloud.storage.client.Client` 

28 :param client: The client that owns the current connection. 

29 

30 :type client_info: :class:`~google.api_core.client_info.ClientInfo` 

31 :param client_info: (Optional) instance used to generate user agent. 

32 

33 :type api_endpoint: str 

34 :param api_endpoint: (Optional) api endpoint to use. 

35 """ 

36 

37 DEFAULT_API_ENDPOINT = _helpers._DEFAULT_STORAGE_HOST 

38 DEFAULT_API_MTLS_ENDPOINT = "https://storage.mtls.googleapis.com" 

39 

40 def __init__(self, client, client_info=None, api_endpoint=None): 

41 super(Connection, self).__init__(client, client_info) 

42 self.API_BASE_URL = api_endpoint or self.DEFAULT_API_ENDPOINT 

43 self.API_BASE_MTLS_URL = self.DEFAULT_API_MTLS_ENDPOINT 

44 self.ALLOW_AUTO_SWITCH_TO_MTLS_URL = api_endpoint is None 

45 self._client_info.client_library_version = __version__ 

46 

47 # TODO: When metrics all use gccl, this should be removed #9552 

48 if self._client_info.user_agent is None: # pragma: no branch 

49 self._client_info.user_agent = "" 

50 agent_version = f"gcloud-python/{__version__}" 

51 if agent_version not in self._client_info.user_agent: 

52 self._client_info.user_agent += f" {agent_version} " 

53 

54 API_VERSION = _helpers._API_VERSION 

55 """The version of the API, used in building the API call's URL.""" 

56 

57 API_URL_TEMPLATE = "{api_base_url}/storage/{api_version}{path}" 

58 """A template for the URL of a particular API call.""" 

59 

60 def api_request(self, *args, **kwargs): 

61 retry = kwargs.pop("retry", None) 

62 kwargs["extra_api_info"] = _helpers._get_invocation_id() 

63 call = functools.partial(super(Connection, self).api_request, *args, **kwargs) 

64 if retry: 

65 # If this is a ConditionalRetryPolicy, check conditions. 

66 try: 

67 retry = retry.get_retry_policy_if_conditions_met(**kwargs) 

68 except AttributeError: # This is not a ConditionalRetryPolicy. 

69 pass 

70 if retry: 

71 call = retry(call) 

72 return call()