Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/errorreporting_v1beta1/services/report_errors_service/transports/base.py: 53%

43 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-08 06:45 +0000

1# -*- coding: utf-8 -*- 

2# Copyright 2023 Google LLC 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, 

12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

13# See the License for the specific language governing permissions and 

14# limitations under the License. 

15# 

16import abc 

17from typing import Awaitable, Callable, Dict, Optional, Sequence, Union 

18 

19from google.cloud.errorreporting_v1beta1 import gapic_version as package_version 

20 

21import google.auth # type: ignore 

22import google.api_core 

23from google.api_core import exceptions as core_exceptions 

24from google.api_core import gapic_v1 

25from google.api_core import retry as retries 

26from google.auth import credentials as ga_credentials # type: ignore 

27from google.oauth2 import service_account # type: ignore 

28 

29from google.cloud.errorreporting_v1beta1.types import report_errors_service 

30 

31DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( 

32 gapic_version=package_version.__version__ 

33) 

34 

35 

36class ReportErrorsServiceTransport(abc.ABC): 

37 """Abstract transport class for ReportErrorsService.""" 

38 

39 AUTH_SCOPES = ("https://www.googleapis.com/auth/cloud-platform",) 

40 

41 DEFAULT_HOST: str = "clouderrorreporting.googleapis.com" 

42 

43 def __init__( 

44 self, 

45 *, 

46 host: str = DEFAULT_HOST, 

47 credentials: Optional[ga_credentials.Credentials] = None, 

48 credentials_file: Optional[str] = None, 

49 scopes: Optional[Sequence[str]] = None, 

50 quota_project_id: Optional[str] = None, 

51 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, 

52 always_use_jwt_access: Optional[bool] = False, 

53 api_audience: Optional[str] = None, 

54 **kwargs, 

55 ) -> None: 

56 """Instantiate the transport. 

57 

58 Args: 

59 host (Optional[str]): 

60 The hostname to connect to. 

61 credentials (Optional[google.auth.credentials.Credentials]): The 

62 authorization credentials to attach to requests. These 

63 credentials identify the application to the service; if none 

64 are specified, the client will attempt to ascertain the 

65 credentials from the environment. 

66 credentials_file (Optional[str]): A file with credentials that can 

67 be loaded with :func:`google.auth.load_credentials_from_file`. 

68 This argument is mutually exclusive with credentials. 

69 scopes (Optional[Sequence[str]]): A list of scopes. 

70 quota_project_id (Optional[str]): An optional project to use for billing 

71 and quota. 

72 client_info (google.api_core.gapic_v1.client_info.ClientInfo): 

73 The client info used to send a user-agent string along with 

74 API requests. If ``None``, then default info will be used. 

75 Generally, you only need to set this if you're developing 

76 your own client library. 

77 always_use_jwt_access (Optional[bool]): Whether self signed JWT should 

78 be used for service account credentials. 

79 """ 

80 

81 scopes_kwargs = {"scopes": scopes, "default_scopes": self.AUTH_SCOPES} 

82 

83 # Save the scopes. 

84 self._scopes = scopes 

85 

86 # If no credentials are provided, then determine the appropriate 

87 # defaults. 

88 if credentials and credentials_file: 

89 raise core_exceptions.DuplicateCredentialArgs( 

90 "'credentials_file' and 'credentials' are mutually exclusive" 

91 ) 

92 

93 if credentials_file is not None: 

94 credentials, _ = google.auth.load_credentials_from_file( 

95 credentials_file, **scopes_kwargs, quota_project_id=quota_project_id 

96 ) 

97 elif credentials is None: 

98 credentials, _ = google.auth.default( 

99 **scopes_kwargs, quota_project_id=quota_project_id 

100 ) 

101 # Don't apply audience if the credentials file passed from user. 

102 if hasattr(credentials, "with_gdch_audience"): 

103 credentials = credentials.with_gdch_audience( 

104 api_audience if api_audience else host 

105 ) 

106 

107 # If the credentials are service account credentials, then always try to use self signed JWT. 

108 if ( 

109 always_use_jwt_access 

110 and isinstance(credentials, service_account.Credentials) 

111 and hasattr(service_account.Credentials, "with_always_use_jwt_access") 

112 ): 

113 credentials = credentials.with_always_use_jwt_access(True) 

114 

115 # Save the credentials. 

116 self._credentials = credentials 

117 

118 # Save the hostname. Default to port 443 (HTTPS) if none is specified. 

119 if ":" not in host: 

120 host += ":443" 

121 self._host = host 

122 

123 def _prep_wrapped_messages(self, client_info): 

124 # Precompute the wrapped methods. 

125 self._wrapped_methods = { 

126 self.report_error_event: gapic_v1.method.wrap_method( 

127 self.report_error_event, 

128 default_timeout=None, 

129 client_info=client_info, 

130 ), 

131 } 

132 

133 def close(self): 

134 """Closes resources associated with the transport. 

135 

136 .. warning:: 

137 Only call this method if the transport is NOT shared 

138 with other clients - this may cause errors in other clients! 

139 """ 

140 raise NotImplementedError() 

141 

142 @property 

143 def report_error_event( 

144 self, 

145 ) -> Callable[ 

146 [report_errors_service.ReportErrorEventRequest], 

147 Union[ 

148 report_errors_service.ReportErrorEventResponse, 

149 Awaitable[report_errors_service.ReportErrorEventResponse], 

150 ], 

151 ]: 

152 raise NotImplementedError() 

153 

154 @property 

155 def kind(self) -> str: 

156 raise NotImplementedError() 

157 

158 

159__all__ = ("ReportErrorsServiceTransport",)