Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/google/cloud/logging_v2/services/metrics_service_v2/transports/base.py: 57%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

72 statements  

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

2# Copyright 2025 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.logging_v2 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.logging_v2.types import logging_metrics 

30from google.longrunning import operations_pb2 # type: ignore 

31from google.protobuf import empty_pb2 # type: ignore 

32 

33DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( 

34 gapic_version=package_version.__version__ 

35) 

36 

37 

38class MetricsServiceV2Transport(abc.ABC): 

39 """Abstract transport class for MetricsServiceV2.""" 

40 

41 AUTH_SCOPES = ( 

42 "https://www.googleapis.com/auth/cloud-platform", 

43 "https://www.googleapis.com/auth/cloud-platform.read-only", 

44 "https://www.googleapis.com/auth/logging.admin", 

45 "https://www.googleapis.com/auth/logging.read", 

46 "https://www.googleapis.com/auth/logging.write", 

47 ) 

48 

49 DEFAULT_HOST: str = "logging.googleapis.com" 

50 

51 def __init__( 

52 self, 

53 *, 

54 host: str = DEFAULT_HOST, 

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

56 credentials_file: Optional[str] = None, 

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

58 quota_project_id: Optional[str] = None, 

59 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, 

60 always_use_jwt_access: Optional[bool] = False, 

61 api_audience: Optional[str] = None, 

62 **kwargs, 

63 ) -> None: 

64 """Instantiate the transport. 

65 

66 Args: 

67 host (Optional[str]): 

68 The hostname to connect to (default: 'logging.googleapis.com'). 

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

70 authorization credentials to attach to requests. These 

71 credentials identify the application to the service; if none 

72 are specified, the client will attempt to ascertain the 

73 credentials from the environment. 

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

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

76 This argument is mutually exclusive with credentials. 

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

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

79 and quota. 

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

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

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

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

84 your own client library. 

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

86 be used for service account credentials. 

87 """ 

88 

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

90 

91 # Save the scopes. 

92 self._scopes = scopes 

93 if not hasattr(self, "_ignore_credentials"): 

94 self._ignore_credentials: bool = False 

95 

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

97 # defaults. 

98 if credentials and credentials_file: 

99 raise core_exceptions.DuplicateCredentialArgs( 

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

101 ) 

102 

103 if credentials_file is not None: 

104 credentials, _ = google.auth.load_credentials_from_file( 

105 credentials_file, **scopes_kwargs, quota_project_id=quota_project_id 

106 ) 

107 elif credentials is None and not self._ignore_credentials: 

108 credentials, _ = google.auth.default( 

109 **scopes_kwargs, quota_project_id=quota_project_id 

110 ) 

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

112 if hasattr(credentials, "with_gdch_audience"): 

113 credentials = credentials.with_gdch_audience( 

114 api_audience if api_audience else host 

115 ) 

116 

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

118 if ( 

119 always_use_jwt_access 

120 and isinstance(credentials, service_account.Credentials) 

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

122 ): 

123 credentials = credentials.with_always_use_jwt_access(True) 

124 

125 # Save the credentials. 

126 self._credentials = credentials 

127 

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

129 if ":" not in host: 

130 host += ":443" 

131 self._host = host 

132 

133 @property 

134 def host(self): 

135 return self._host 

136 

137 def _prep_wrapped_messages(self, client_info): 

138 # Precompute the wrapped methods. 

139 self._wrapped_methods = { 

140 self.list_log_metrics: gapic_v1.method.wrap_method( 

141 self.list_log_metrics, 

142 default_retry=retries.Retry( 

143 initial=0.1, 

144 maximum=60.0, 

145 multiplier=1.3, 

146 predicate=retries.if_exception_type( 

147 core_exceptions.DeadlineExceeded, 

148 core_exceptions.InternalServerError, 

149 core_exceptions.ServiceUnavailable, 

150 ), 

151 deadline=60.0, 

152 ), 

153 default_timeout=60.0, 

154 client_info=client_info, 

155 ), 

156 self.get_log_metric: gapic_v1.method.wrap_method( 

157 self.get_log_metric, 

158 default_retry=retries.Retry( 

159 initial=0.1, 

160 maximum=60.0, 

161 multiplier=1.3, 

162 predicate=retries.if_exception_type( 

163 core_exceptions.DeadlineExceeded, 

164 core_exceptions.InternalServerError, 

165 core_exceptions.ServiceUnavailable, 

166 ), 

167 deadline=60.0, 

168 ), 

169 default_timeout=60.0, 

170 client_info=client_info, 

171 ), 

172 self.create_log_metric: gapic_v1.method.wrap_method( 

173 self.create_log_metric, 

174 default_timeout=60.0, 

175 client_info=client_info, 

176 ), 

177 self.update_log_metric: gapic_v1.method.wrap_method( 

178 self.update_log_metric, 

179 default_retry=retries.Retry( 

180 initial=0.1, 

181 maximum=60.0, 

182 multiplier=1.3, 

183 predicate=retries.if_exception_type( 

184 core_exceptions.DeadlineExceeded, 

185 core_exceptions.InternalServerError, 

186 core_exceptions.ServiceUnavailable, 

187 ), 

188 deadline=60.0, 

189 ), 

190 default_timeout=60.0, 

191 client_info=client_info, 

192 ), 

193 self.delete_log_metric: gapic_v1.method.wrap_method( 

194 self.delete_log_metric, 

195 default_retry=retries.Retry( 

196 initial=0.1, 

197 maximum=60.0, 

198 multiplier=1.3, 

199 predicate=retries.if_exception_type( 

200 core_exceptions.DeadlineExceeded, 

201 core_exceptions.InternalServerError, 

202 core_exceptions.ServiceUnavailable, 

203 ), 

204 deadline=60.0, 

205 ), 

206 default_timeout=60.0, 

207 client_info=client_info, 

208 ), 

209 self.cancel_operation: gapic_v1.method.wrap_method( 

210 self.cancel_operation, 

211 default_timeout=None, 

212 client_info=client_info, 

213 ), 

214 self.get_operation: gapic_v1.method.wrap_method( 

215 self.get_operation, 

216 default_timeout=None, 

217 client_info=client_info, 

218 ), 

219 self.list_operations: gapic_v1.method.wrap_method( 

220 self.list_operations, 

221 default_timeout=None, 

222 client_info=client_info, 

223 ), 

224 } 

225 

226 def close(self): 

227 """Closes resources associated with the transport. 

228 

229 .. warning:: 

230 Only call this method if the transport is NOT shared 

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

232 """ 

233 raise NotImplementedError() 

234 

235 @property 

236 def list_log_metrics( 

237 self, 

238 ) -> Callable[ 

239 [logging_metrics.ListLogMetricsRequest], 

240 Union[ 

241 logging_metrics.ListLogMetricsResponse, 

242 Awaitable[logging_metrics.ListLogMetricsResponse], 

243 ], 

244 ]: 

245 raise NotImplementedError() 

246 

247 @property 

248 def get_log_metric( 

249 self, 

250 ) -> Callable[ 

251 [logging_metrics.GetLogMetricRequest], 

252 Union[logging_metrics.LogMetric, Awaitable[logging_metrics.LogMetric]], 

253 ]: 

254 raise NotImplementedError() 

255 

256 @property 

257 def create_log_metric( 

258 self, 

259 ) -> Callable[ 

260 [logging_metrics.CreateLogMetricRequest], 

261 Union[logging_metrics.LogMetric, Awaitable[logging_metrics.LogMetric]], 

262 ]: 

263 raise NotImplementedError() 

264 

265 @property 

266 def update_log_metric( 

267 self, 

268 ) -> Callable[ 

269 [logging_metrics.UpdateLogMetricRequest], 

270 Union[logging_metrics.LogMetric, Awaitable[logging_metrics.LogMetric]], 

271 ]: 

272 raise NotImplementedError() 

273 

274 @property 

275 def delete_log_metric( 

276 self, 

277 ) -> Callable[ 

278 [logging_metrics.DeleteLogMetricRequest], 

279 Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]], 

280 ]: 

281 raise NotImplementedError() 

282 

283 @property 

284 def list_operations( 

285 self, 

286 ) -> Callable[ 

287 [operations_pb2.ListOperationsRequest], 

288 Union[ 

289 operations_pb2.ListOperationsResponse, 

290 Awaitable[operations_pb2.ListOperationsResponse], 

291 ], 

292 ]: 

293 raise NotImplementedError() 

294 

295 @property 

296 def get_operation( 

297 self, 

298 ) -> Callable[ 

299 [operations_pb2.GetOperationRequest], 

300 Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]], 

301 ]: 

302 raise NotImplementedError() 

303 

304 @property 

305 def cancel_operation( 

306 self, 

307 ) -> Callable[[operations_pb2.CancelOperationRequest], None,]: 

308 raise NotImplementedError() 

309 

310 @property 

311 def kind(self) -> str: 

312 raise NotImplementedError() 

313 

314 

315__all__ = ("MetricsServiceV2Transport",)