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

75 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 

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

39 """Abstract transport class for LoggingServiceV2.""" 

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

141 self.delete_log, 

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

157 self.write_log_entries, 

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

173 self.list_log_entries, 

174 default_retry=retries.Retry( 

175 initial=0.1, 

176 maximum=60.0, 

177 multiplier=1.3, 

178 predicate=retries.if_exception_type( 

179 core_exceptions.DeadlineExceeded, 

180 core_exceptions.InternalServerError, 

181 core_exceptions.ServiceUnavailable, 

182 ), 

183 deadline=60.0, 

184 ), 

185 default_timeout=60.0, 

186 client_info=client_info, 

187 ), 

188 self.list_monitored_resource_descriptors: gapic_v1.method.wrap_method( 

189 self.list_monitored_resource_descriptors, 

190 default_retry=retries.Retry( 

191 initial=0.1, 

192 maximum=60.0, 

193 multiplier=1.3, 

194 predicate=retries.if_exception_type( 

195 core_exceptions.DeadlineExceeded, 

196 core_exceptions.InternalServerError, 

197 core_exceptions.ServiceUnavailable, 

198 ), 

199 deadline=60.0, 

200 ), 

201 default_timeout=60.0, 

202 client_info=client_info, 

203 ), 

204 self.list_logs: gapic_v1.method.wrap_method( 

205 self.list_logs, 

206 default_retry=retries.Retry( 

207 initial=0.1, 

208 maximum=60.0, 

209 multiplier=1.3, 

210 predicate=retries.if_exception_type( 

211 core_exceptions.DeadlineExceeded, 

212 core_exceptions.InternalServerError, 

213 core_exceptions.ServiceUnavailable, 

214 ), 

215 deadline=60.0, 

216 ), 

217 default_timeout=60.0, 

218 client_info=client_info, 

219 ), 

220 self.tail_log_entries: gapic_v1.method.wrap_method( 

221 self.tail_log_entries, 

222 default_retry=retries.Retry( 

223 initial=0.1, 

224 maximum=60.0, 

225 multiplier=1.3, 

226 predicate=retries.if_exception_type( 

227 core_exceptions.DeadlineExceeded, 

228 core_exceptions.InternalServerError, 

229 core_exceptions.ServiceUnavailable, 

230 ), 

231 deadline=3600.0, 

232 ), 

233 default_timeout=3600.0, 

234 client_info=client_info, 

235 ), 

236 self.cancel_operation: gapic_v1.method.wrap_method( 

237 self.cancel_operation, 

238 default_timeout=None, 

239 client_info=client_info, 

240 ), 

241 self.get_operation: gapic_v1.method.wrap_method( 

242 self.get_operation, 

243 default_timeout=None, 

244 client_info=client_info, 

245 ), 

246 self.list_operations: gapic_v1.method.wrap_method( 

247 self.list_operations, 

248 default_timeout=None, 

249 client_info=client_info, 

250 ), 

251 } 

252 

253 def close(self): 

254 """Closes resources associated with the transport. 

255 

256 .. warning:: 

257 Only call this method if the transport is NOT shared 

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

259 """ 

260 raise NotImplementedError() 

261 

262 @property 

263 def delete_log( 

264 self, 

265 ) -> Callable[ 

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

267 ]: 

268 raise NotImplementedError() 

269 

270 @property 

271 def write_log_entries( 

272 self, 

273 ) -> Callable[ 

274 [logging.WriteLogEntriesRequest], 

275 Union[ 

276 logging.WriteLogEntriesResponse, Awaitable[logging.WriteLogEntriesResponse] 

277 ], 

278 ]: 

279 raise NotImplementedError() 

280 

281 @property 

282 def list_log_entries( 

283 self, 

284 ) -> Callable[ 

285 [logging.ListLogEntriesRequest], 

286 Union[ 

287 logging.ListLogEntriesResponse, Awaitable[logging.ListLogEntriesResponse] 

288 ], 

289 ]: 

290 raise NotImplementedError() 

291 

292 @property 

293 def list_monitored_resource_descriptors( 

294 self, 

295 ) -> Callable[ 

296 [logging.ListMonitoredResourceDescriptorsRequest], 

297 Union[ 

298 logging.ListMonitoredResourceDescriptorsResponse, 

299 Awaitable[logging.ListMonitoredResourceDescriptorsResponse], 

300 ], 

301 ]: 

302 raise NotImplementedError() 

303 

304 @property 

305 def list_logs( 

306 self, 

307 ) -> Callable[ 

308 [logging.ListLogsRequest], 

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

310 ]: 

311 raise NotImplementedError() 

312 

313 @property 

314 def tail_log_entries( 

315 self, 

316 ) -> Callable[ 

317 [logging.TailLogEntriesRequest], 

318 Union[ 

319 logging.TailLogEntriesResponse, Awaitable[logging.TailLogEntriesResponse] 

320 ], 

321 ]: 

322 raise NotImplementedError() 

323 

324 @property 

325 def list_operations( 

326 self, 

327 ) -> Callable[ 

328 [operations_pb2.ListOperationsRequest], 

329 Union[ 

330 operations_pb2.ListOperationsResponse, 

331 Awaitable[operations_pb2.ListOperationsResponse], 

332 ], 

333 ]: 

334 raise NotImplementedError() 

335 

336 @property 

337 def get_operation( 

338 self, 

339 ) -> Callable[ 

340 [operations_pb2.GetOperationRequest], 

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

342 ]: 

343 raise NotImplementedError() 

344 

345 @property 

346 def cancel_operation( 

347 self, 

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

349 raise NotImplementedError() 

350 

351 @property 

352 def kind(self) -> str: 

353 raise NotImplementedError() 

354 

355 

356__all__ = ("LoggingServiceV2Transport",)