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

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

73 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 

28import google.protobuf 

29 

30from google.cloud.logging_v2.types import logging_metrics 

31from google.longrunning import operations_pb2 # type: ignore 

32from google.protobuf import empty_pb2 # type: ignore 

33 

34DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( 

35 gapic_version=package_version.__version__ 

36) 

37 

38if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"): # pragma: NO COVER 

39 DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__ 

40 

41 

42class MetricsServiceV2Transport(abc.ABC): 

43 """Abstract transport class for MetricsServiceV2.""" 

44 

45 AUTH_SCOPES = ( 

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

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

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

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

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

51 ) 

52 

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

54 

55 def __init__( 

56 self, 

57 *, 

58 host: str = DEFAULT_HOST, 

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

60 credentials_file: Optional[str] = None, 

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

62 quota_project_id: Optional[str] = None, 

63 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, 

64 always_use_jwt_access: Optional[bool] = False, 

65 api_audience: Optional[str] = None, 

66 **kwargs, 

67 ) -> None: 

68 """Instantiate the transport. 

69 

70 Args: 

71 host (Optional[str]): 

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

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

74 authorization credentials to attach to requests. These 

75 credentials identify the application to the service; if none 

76 are specified, the client will attempt to ascertain the 

77 credentials from the environment. 

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

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

80 This argument is mutually exclusive with credentials. 

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

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

83 and quota. 

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

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

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

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

88 your own client library. 

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

90 be used for service account credentials. 

91 """ 

92 

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

94 

95 # Save the scopes. 

96 self._scopes = scopes 

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

98 self._ignore_credentials: bool = False 

99 

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

101 # defaults. 

102 if credentials and credentials_file: 

103 raise core_exceptions.DuplicateCredentialArgs( 

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

105 ) 

106 

107 if credentials_file is not None: 

108 credentials, _ = google.auth.load_credentials_from_file( 

109 credentials_file, **scopes_kwargs, quota_project_id=quota_project_id 

110 ) 

111 elif credentials is None and not self._ignore_credentials: 

112 credentials, _ = google.auth.default( 

113 **scopes_kwargs, quota_project_id=quota_project_id 

114 ) 

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

116 if hasattr(credentials, "with_gdch_audience"): 

117 credentials = credentials.with_gdch_audience( 

118 api_audience if api_audience else host 

119 ) 

120 

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

122 if ( 

123 always_use_jwt_access 

124 and isinstance(credentials, service_account.Credentials) 

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

126 ): 

127 credentials = credentials.with_always_use_jwt_access(True) 

128 

129 # Save the credentials. 

130 self._credentials = credentials 

131 

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

133 if ":" not in host: 

134 host += ":443" 

135 self._host = host 

136 

137 @property 

138 def host(self): 

139 return self._host 

140 

141 def _prep_wrapped_messages(self, client_info): 

142 # Precompute the wrapped methods. 

143 self._wrapped_methods = { 

144 self.list_log_metrics: gapic_v1.method.wrap_method( 

145 self.list_log_metrics, 

146 default_retry=retries.Retry( 

147 initial=0.1, 

148 maximum=60.0, 

149 multiplier=1.3, 

150 predicate=retries.if_exception_type( 

151 core_exceptions.DeadlineExceeded, 

152 core_exceptions.InternalServerError, 

153 core_exceptions.ServiceUnavailable, 

154 ), 

155 deadline=60.0, 

156 ), 

157 default_timeout=60.0, 

158 client_info=client_info, 

159 ), 

160 self.get_log_metric: gapic_v1.method.wrap_method( 

161 self.get_log_metric, 

162 default_retry=retries.Retry( 

163 initial=0.1, 

164 maximum=60.0, 

165 multiplier=1.3, 

166 predicate=retries.if_exception_type( 

167 core_exceptions.DeadlineExceeded, 

168 core_exceptions.InternalServerError, 

169 core_exceptions.ServiceUnavailable, 

170 ), 

171 deadline=60.0, 

172 ), 

173 default_timeout=60.0, 

174 client_info=client_info, 

175 ), 

176 self.create_log_metric: gapic_v1.method.wrap_method( 

177 self.create_log_metric, 

178 default_timeout=60.0, 

179 client_info=client_info, 

180 ), 

181 self.update_log_metric: gapic_v1.method.wrap_method( 

182 self.update_log_metric, 

183 default_retry=retries.Retry( 

184 initial=0.1, 

185 maximum=60.0, 

186 multiplier=1.3, 

187 predicate=retries.if_exception_type( 

188 core_exceptions.DeadlineExceeded, 

189 core_exceptions.InternalServerError, 

190 core_exceptions.ServiceUnavailable, 

191 ), 

192 deadline=60.0, 

193 ), 

194 default_timeout=60.0, 

195 client_info=client_info, 

196 ), 

197 self.delete_log_metric: gapic_v1.method.wrap_method( 

198 self.delete_log_metric, 

199 default_retry=retries.Retry( 

200 initial=0.1, 

201 maximum=60.0, 

202 multiplier=1.3, 

203 predicate=retries.if_exception_type( 

204 core_exceptions.DeadlineExceeded, 

205 core_exceptions.InternalServerError, 

206 core_exceptions.ServiceUnavailable, 

207 ), 

208 deadline=60.0, 

209 ), 

210 default_timeout=60.0, 

211 client_info=client_info, 

212 ), 

213 self.cancel_operation: gapic_v1.method.wrap_method( 

214 self.cancel_operation, 

215 default_timeout=None, 

216 client_info=client_info, 

217 ), 

218 self.get_operation: gapic_v1.method.wrap_method( 

219 self.get_operation, 

220 default_timeout=None, 

221 client_info=client_info, 

222 ), 

223 self.list_operations: gapic_v1.method.wrap_method( 

224 self.list_operations, 

225 default_timeout=None, 

226 client_info=client_info, 

227 ), 

228 } 

229 

230 def close(self): 

231 """Closes resources associated with the transport. 

232 

233 .. warning:: 

234 Only call this method if the transport is NOT shared 

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

236 """ 

237 raise NotImplementedError() 

238 

239 @property 

240 def list_log_metrics( 

241 self, 

242 ) -> Callable[ 

243 [logging_metrics.ListLogMetricsRequest], 

244 Union[ 

245 logging_metrics.ListLogMetricsResponse, 

246 Awaitable[logging_metrics.ListLogMetricsResponse], 

247 ], 

248 ]: 

249 raise NotImplementedError() 

250 

251 @property 

252 def get_log_metric( 

253 self, 

254 ) -> Callable[ 

255 [logging_metrics.GetLogMetricRequest], 

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

257 ]: 

258 raise NotImplementedError() 

259 

260 @property 

261 def create_log_metric( 

262 self, 

263 ) -> Callable[ 

264 [logging_metrics.CreateLogMetricRequest], 

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

266 ]: 

267 raise NotImplementedError() 

268 

269 @property 

270 def update_log_metric( 

271 self, 

272 ) -> Callable[ 

273 [logging_metrics.UpdateLogMetricRequest], 

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

275 ]: 

276 raise NotImplementedError() 

277 

278 @property 

279 def delete_log_metric( 

280 self, 

281 ) -> Callable[ 

282 [logging_metrics.DeleteLogMetricRequest], 

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

284 ]: 

285 raise NotImplementedError() 

286 

287 @property 

288 def list_operations( 

289 self, 

290 ) -> Callable[ 

291 [operations_pb2.ListOperationsRequest], 

292 Union[ 

293 operations_pb2.ListOperationsResponse, 

294 Awaitable[operations_pb2.ListOperationsResponse], 

295 ], 

296 ]: 

297 raise NotImplementedError() 

298 

299 @property 

300 def get_operation( 

301 self, 

302 ) -> Callable[ 

303 [operations_pb2.GetOperationRequest], 

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

305 ]: 

306 raise NotImplementedError() 

307 

308 @property 

309 def cancel_operation( 

310 self, 

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

312 raise NotImplementedError() 

313 

314 @property 

315 def kind(self) -> str: 

316 raise NotImplementedError() 

317 

318 

319__all__ = ("MetricsServiceV2Transport",)