Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/google/cloud/logging_v2/services/logging_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

76 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 

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 LoggingServiceV2Transport(abc.ABC): 

43 """Abstract transport class for LoggingServiceV2.""" 

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.delete_log: gapic_v1.method.wrap_method( 

145 self.delete_log, 

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.write_log_entries: gapic_v1.method.wrap_method( 

161 self.write_log_entries, 

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.list_log_entries: gapic_v1.method.wrap_method( 

177 self.list_log_entries, 

178 default_retry=retries.Retry( 

179 initial=0.1, 

180 maximum=60.0, 

181 multiplier=1.3, 

182 predicate=retries.if_exception_type( 

183 core_exceptions.DeadlineExceeded, 

184 core_exceptions.InternalServerError, 

185 core_exceptions.ServiceUnavailable, 

186 ), 

187 deadline=60.0, 

188 ), 

189 default_timeout=60.0, 

190 client_info=client_info, 

191 ), 

192 self.list_monitored_resource_descriptors: gapic_v1.method.wrap_method( 

193 self.list_monitored_resource_descriptors, 

194 default_retry=retries.Retry( 

195 initial=0.1, 

196 maximum=60.0, 

197 multiplier=1.3, 

198 predicate=retries.if_exception_type( 

199 core_exceptions.DeadlineExceeded, 

200 core_exceptions.InternalServerError, 

201 core_exceptions.ServiceUnavailable, 

202 ), 

203 deadline=60.0, 

204 ), 

205 default_timeout=60.0, 

206 client_info=client_info, 

207 ), 

208 self.list_logs: gapic_v1.method.wrap_method( 

209 self.list_logs, 

210 default_retry=retries.Retry( 

211 initial=0.1, 

212 maximum=60.0, 

213 multiplier=1.3, 

214 predicate=retries.if_exception_type( 

215 core_exceptions.DeadlineExceeded, 

216 core_exceptions.InternalServerError, 

217 core_exceptions.ServiceUnavailable, 

218 ), 

219 deadline=60.0, 

220 ), 

221 default_timeout=60.0, 

222 client_info=client_info, 

223 ), 

224 self.tail_log_entries: gapic_v1.method.wrap_method( 

225 self.tail_log_entries, 

226 default_retry=retries.Retry( 

227 initial=0.1, 

228 maximum=60.0, 

229 multiplier=1.3, 

230 predicate=retries.if_exception_type( 

231 core_exceptions.DeadlineExceeded, 

232 core_exceptions.InternalServerError, 

233 core_exceptions.ServiceUnavailable, 

234 ), 

235 deadline=3600.0, 

236 ), 

237 default_timeout=3600.0, 

238 client_info=client_info, 

239 ), 

240 self.cancel_operation: gapic_v1.method.wrap_method( 

241 self.cancel_operation, 

242 default_timeout=None, 

243 client_info=client_info, 

244 ), 

245 self.get_operation: gapic_v1.method.wrap_method( 

246 self.get_operation, 

247 default_timeout=None, 

248 client_info=client_info, 

249 ), 

250 self.list_operations: gapic_v1.method.wrap_method( 

251 self.list_operations, 

252 default_timeout=None, 

253 client_info=client_info, 

254 ), 

255 } 

256 

257 def close(self): 

258 """Closes resources associated with the transport. 

259 

260 .. warning:: 

261 Only call this method if the transport is NOT shared 

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

263 """ 

264 raise NotImplementedError() 

265 

266 @property 

267 def delete_log( 

268 self, 

269 ) -> Callable[ 

270 [logging.DeleteLogRequest], Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]] 

271 ]: 

272 raise NotImplementedError() 

273 

274 @property 

275 def write_log_entries( 

276 self, 

277 ) -> Callable[ 

278 [logging.WriteLogEntriesRequest], 

279 Union[ 

280 logging.WriteLogEntriesResponse, Awaitable[logging.WriteLogEntriesResponse] 

281 ], 

282 ]: 

283 raise NotImplementedError() 

284 

285 @property 

286 def list_log_entries( 

287 self, 

288 ) -> Callable[ 

289 [logging.ListLogEntriesRequest], 

290 Union[ 

291 logging.ListLogEntriesResponse, Awaitable[logging.ListLogEntriesResponse] 

292 ], 

293 ]: 

294 raise NotImplementedError() 

295 

296 @property 

297 def list_monitored_resource_descriptors( 

298 self, 

299 ) -> Callable[ 

300 [logging.ListMonitoredResourceDescriptorsRequest], 

301 Union[ 

302 logging.ListMonitoredResourceDescriptorsResponse, 

303 Awaitable[logging.ListMonitoredResourceDescriptorsResponse], 

304 ], 

305 ]: 

306 raise NotImplementedError() 

307 

308 @property 

309 def list_logs( 

310 self, 

311 ) -> Callable[ 

312 [logging.ListLogsRequest], 

313 Union[logging.ListLogsResponse, Awaitable[logging.ListLogsResponse]], 

314 ]: 

315 raise NotImplementedError() 

316 

317 @property 

318 def tail_log_entries( 

319 self, 

320 ) -> Callable[ 

321 [logging.TailLogEntriesRequest], 

322 Union[ 

323 logging.TailLogEntriesResponse, Awaitable[logging.TailLogEntriesResponse] 

324 ], 

325 ]: 

326 raise NotImplementedError() 

327 

328 @property 

329 def list_operations( 

330 self, 

331 ) -> Callable[ 

332 [operations_pb2.ListOperationsRequest], 

333 Union[ 

334 operations_pb2.ListOperationsResponse, 

335 Awaitable[operations_pb2.ListOperationsResponse], 

336 ], 

337 ]: 

338 raise NotImplementedError() 

339 

340 @property 

341 def get_operation( 

342 self, 

343 ) -> Callable[ 

344 [operations_pb2.GetOperationRequest], 

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

346 ]: 

347 raise NotImplementedError() 

348 

349 @property 

350 def cancel_operation( 

351 self, 

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

353 raise NotImplementedError() 

354 

355 @property 

356 def kind(self) -> str: 

357 raise NotImplementedError() 

358 

359 

360__all__ = ("LoggingServiceV2Transport",)