Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/tasks_v2/services/cloud_tasks/transports/base.py: 75%

95 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 07:07 +0000

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

2# Copyright 2022 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 

19import google.api_core 

20from google.api_core import exceptions as core_exceptions 

21from google.api_core import gapic_v1 

22from google.api_core import retry as retries 

23import google.auth # type: ignore 

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

25from google.iam.v1 import iam_policy_pb2 # type: ignore 

26from google.iam.v1 import policy_pb2 # type: ignore 

27from google.oauth2 import service_account # type: ignore 

28from google.protobuf import empty_pb2 # type: ignore 

29 

30from google.cloud.tasks_v2 import gapic_version as package_version 

31from google.cloud.tasks_v2.types import cloudtasks 

32from google.cloud.tasks_v2.types import queue 

33from google.cloud.tasks_v2.types import queue as gct_queue 

34from google.cloud.tasks_v2.types import task 

35from google.cloud.tasks_v2.types import task as gct_task 

36 

37DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( 

38 gapic_version=package_version.__version__ 

39) 

40 

41 

42class CloudTasksTransport(abc.ABC): 

43 """Abstract transport class for CloudTasks.""" 

44 

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

46 

47 DEFAULT_HOST: str = "cloudtasks.googleapis.com" 

48 

49 def __init__( 

50 self, 

51 *, 

52 host: str = DEFAULT_HOST, 

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

54 credentials_file: Optional[str] = None, 

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

56 quota_project_id: Optional[str] = None, 

57 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, 

58 always_use_jwt_access: Optional[bool] = False, 

59 api_audience: Optional[str] = None, 

60 **kwargs, 

61 ) -> None: 

62 """Instantiate the transport. 

63 

64 Args: 

65 host (Optional[str]): 

66 The hostname to connect to. 

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

68 authorization credentials to attach to requests. These 

69 credentials identify the application to the service; if none 

70 are specified, the client will attempt to ascertain the 

71 credentials from the environment. 

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

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

74 This argument is mutually exclusive with credentials. 

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

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

77 and quota. 

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

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

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

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

82 your own client library. 

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

84 be used for service account credentials. 

85 """ 

86 

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

88 

89 # Save the scopes. 

90 self._scopes = scopes 

91 

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

93 # defaults. 

94 if credentials and credentials_file: 

95 raise core_exceptions.DuplicateCredentialArgs( 

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

97 ) 

98 

99 if credentials_file is not None: 

100 credentials, _ = google.auth.load_credentials_from_file( 

101 credentials_file, **scopes_kwargs, quota_project_id=quota_project_id 

102 ) 

103 elif credentials is None: 

104 credentials, _ = google.auth.default( 

105 **scopes_kwargs, quota_project_id=quota_project_id 

106 ) 

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

108 if hasattr(credentials, "with_gdch_audience"): 

109 credentials = credentials.with_gdch_audience( 

110 api_audience if api_audience else host 

111 ) 

112 

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

114 if ( 

115 always_use_jwt_access 

116 and isinstance(credentials, service_account.Credentials) 

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

118 ): 

119 credentials = credentials.with_always_use_jwt_access(True) 

120 

121 # Save the credentials. 

122 self._credentials = credentials 

123 

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

125 if ":" not in host: 

126 host += ":443" 

127 self._host = host 

128 

129 def _prep_wrapped_messages(self, client_info): 

130 # Precompute the wrapped methods. 

131 self._wrapped_methods = { 

132 self.list_queues: gapic_v1.method.wrap_method( 

133 self.list_queues, 

134 default_retry=retries.Retry( 

135 initial=0.1, 

136 maximum=10.0, 

137 multiplier=1.3, 

138 predicate=retries.if_exception_type( 

139 core_exceptions.DeadlineExceeded, 

140 core_exceptions.ServiceUnavailable, 

141 ), 

142 deadline=10.0, 

143 ), 

144 default_timeout=10.0, 

145 client_info=client_info, 

146 ), 

147 self.get_queue: gapic_v1.method.wrap_method( 

148 self.get_queue, 

149 default_retry=retries.Retry( 

150 initial=0.1, 

151 maximum=10.0, 

152 multiplier=1.3, 

153 predicate=retries.if_exception_type( 

154 core_exceptions.DeadlineExceeded, 

155 core_exceptions.ServiceUnavailable, 

156 ), 

157 deadline=10.0, 

158 ), 

159 default_timeout=10.0, 

160 client_info=client_info, 

161 ), 

162 self.create_queue: gapic_v1.method.wrap_method( 

163 self.create_queue, 

164 default_timeout=10.0, 

165 client_info=client_info, 

166 ), 

167 self.update_queue: gapic_v1.method.wrap_method( 

168 self.update_queue, 

169 default_timeout=10.0, 

170 client_info=client_info, 

171 ), 

172 self.delete_queue: gapic_v1.method.wrap_method( 

173 self.delete_queue, 

174 default_retry=retries.Retry( 

175 initial=0.1, 

176 maximum=10.0, 

177 multiplier=1.3, 

178 predicate=retries.if_exception_type( 

179 core_exceptions.DeadlineExceeded, 

180 core_exceptions.ServiceUnavailable, 

181 ), 

182 deadline=10.0, 

183 ), 

184 default_timeout=10.0, 

185 client_info=client_info, 

186 ), 

187 self.purge_queue: gapic_v1.method.wrap_method( 

188 self.purge_queue, 

189 default_timeout=10.0, 

190 client_info=client_info, 

191 ), 

192 self.pause_queue: gapic_v1.method.wrap_method( 

193 self.pause_queue, 

194 default_timeout=10.0, 

195 client_info=client_info, 

196 ), 

197 self.resume_queue: gapic_v1.method.wrap_method( 

198 self.resume_queue, 

199 default_timeout=10.0, 

200 client_info=client_info, 

201 ), 

202 self.get_iam_policy: gapic_v1.method.wrap_method( 

203 self.get_iam_policy, 

204 default_retry=retries.Retry( 

205 initial=0.1, 

206 maximum=10.0, 

207 multiplier=1.3, 

208 predicate=retries.if_exception_type( 

209 core_exceptions.DeadlineExceeded, 

210 core_exceptions.ServiceUnavailable, 

211 ), 

212 deadline=10.0, 

213 ), 

214 default_timeout=10.0, 

215 client_info=client_info, 

216 ), 

217 self.set_iam_policy: gapic_v1.method.wrap_method( 

218 self.set_iam_policy, 

219 default_timeout=10.0, 

220 client_info=client_info, 

221 ), 

222 self.test_iam_permissions: gapic_v1.method.wrap_method( 

223 self.test_iam_permissions, 

224 default_retry=retries.Retry( 

225 initial=0.1, 

226 maximum=10.0, 

227 multiplier=1.3, 

228 predicate=retries.if_exception_type( 

229 core_exceptions.DeadlineExceeded, 

230 core_exceptions.ServiceUnavailable, 

231 ), 

232 deadline=10.0, 

233 ), 

234 default_timeout=10.0, 

235 client_info=client_info, 

236 ), 

237 self.list_tasks: gapic_v1.method.wrap_method( 

238 self.list_tasks, 

239 default_retry=retries.Retry( 

240 initial=0.1, 

241 maximum=10.0, 

242 multiplier=1.3, 

243 predicate=retries.if_exception_type( 

244 core_exceptions.DeadlineExceeded, 

245 core_exceptions.ServiceUnavailable, 

246 ), 

247 deadline=10.0, 

248 ), 

249 default_timeout=10.0, 

250 client_info=client_info, 

251 ), 

252 self.get_task: gapic_v1.method.wrap_method( 

253 self.get_task, 

254 default_retry=retries.Retry( 

255 initial=0.1, 

256 maximum=10.0, 

257 multiplier=1.3, 

258 predicate=retries.if_exception_type( 

259 core_exceptions.DeadlineExceeded, 

260 core_exceptions.ServiceUnavailable, 

261 ), 

262 deadline=10.0, 

263 ), 

264 default_timeout=10.0, 

265 client_info=client_info, 

266 ), 

267 self.create_task: gapic_v1.method.wrap_method( 

268 self.create_task, 

269 default_timeout=10.0, 

270 client_info=client_info, 

271 ), 

272 self.delete_task: gapic_v1.method.wrap_method( 

273 self.delete_task, 

274 default_retry=retries.Retry( 

275 initial=0.1, 

276 maximum=10.0, 

277 multiplier=1.3, 

278 predicate=retries.if_exception_type( 

279 core_exceptions.DeadlineExceeded, 

280 core_exceptions.ServiceUnavailable, 

281 ), 

282 deadline=10.0, 

283 ), 

284 default_timeout=10.0, 

285 client_info=client_info, 

286 ), 

287 self.run_task: gapic_v1.method.wrap_method( 

288 self.run_task, 

289 default_timeout=10.0, 

290 client_info=client_info, 

291 ), 

292 } 

293 

294 def close(self): 

295 """Closes resources associated with the transport. 

296 

297 .. warning:: 

298 Only call this method if the transport is NOT shared 

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

300 """ 

301 raise NotImplementedError() 

302 

303 @property 

304 def list_queues( 

305 self, 

306 ) -> Callable[ 

307 [cloudtasks.ListQueuesRequest], 

308 Union[cloudtasks.ListQueuesResponse, Awaitable[cloudtasks.ListQueuesResponse]], 

309 ]: 

310 raise NotImplementedError() 

311 

312 @property 

313 def get_queue( 

314 self, 

315 ) -> Callable[ 

316 [cloudtasks.GetQueueRequest], Union[queue.Queue, Awaitable[queue.Queue]] 

317 ]: 

318 raise NotImplementedError() 

319 

320 @property 

321 def create_queue( 

322 self, 

323 ) -> Callable[ 

324 [cloudtasks.CreateQueueRequest], 

325 Union[gct_queue.Queue, Awaitable[gct_queue.Queue]], 

326 ]: 

327 raise NotImplementedError() 

328 

329 @property 

330 def update_queue( 

331 self, 

332 ) -> Callable[ 

333 [cloudtasks.UpdateQueueRequest], 

334 Union[gct_queue.Queue, Awaitable[gct_queue.Queue]], 

335 ]: 

336 raise NotImplementedError() 

337 

338 @property 

339 def delete_queue( 

340 self, 

341 ) -> Callable[ 

342 [cloudtasks.DeleteQueueRequest], 

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

344 ]: 

345 raise NotImplementedError() 

346 

347 @property 

348 def purge_queue( 

349 self, 

350 ) -> Callable[ 

351 [cloudtasks.PurgeQueueRequest], Union[queue.Queue, Awaitable[queue.Queue]] 

352 ]: 

353 raise NotImplementedError() 

354 

355 @property 

356 def pause_queue( 

357 self, 

358 ) -> Callable[ 

359 [cloudtasks.PauseQueueRequest], Union[queue.Queue, Awaitable[queue.Queue]] 

360 ]: 

361 raise NotImplementedError() 

362 

363 @property 

364 def resume_queue( 

365 self, 

366 ) -> Callable[ 

367 [cloudtasks.ResumeQueueRequest], Union[queue.Queue, Awaitable[queue.Queue]] 

368 ]: 

369 raise NotImplementedError() 

370 

371 @property 

372 def get_iam_policy( 

373 self, 

374 ) -> Callable[ 

375 [iam_policy_pb2.GetIamPolicyRequest], 

376 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]], 

377 ]: 

378 raise NotImplementedError() 

379 

380 @property 

381 def set_iam_policy( 

382 self, 

383 ) -> Callable[ 

384 [iam_policy_pb2.SetIamPolicyRequest], 

385 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]], 

386 ]: 

387 raise NotImplementedError() 

388 

389 @property 

390 def test_iam_permissions( 

391 self, 

392 ) -> Callable[ 

393 [iam_policy_pb2.TestIamPermissionsRequest], 

394 Union[ 

395 iam_policy_pb2.TestIamPermissionsResponse, 

396 Awaitable[iam_policy_pb2.TestIamPermissionsResponse], 

397 ], 

398 ]: 

399 raise NotImplementedError() 

400 

401 @property 

402 def list_tasks( 

403 self, 

404 ) -> Callable[ 

405 [cloudtasks.ListTasksRequest], 

406 Union[cloudtasks.ListTasksResponse, Awaitable[cloudtasks.ListTasksResponse]], 

407 ]: 

408 raise NotImplementedError() 

409 

410 @property 

411 def get_task( 

412 self, 

413 ) -> Callable[[cloudtasks.GetTaskRequest], Union[task.Task, Awaitable[task.Task]]]: 

414 raise NotImplementedError() 

415 

416 @property 

417 def create_task( 

418 self, 

419 ) -> Callable[ 

420 [cloudtasks.CreateTaskRequest], Union[gct_task.Task, Awaitable[gct_task.Task]] 

421 ]: 

422 raise NotImplementedError() 

423 

424 @property 

425 def delete_task( 

426 self, 

427 ) -> Callable[ 

428 [cloudtasks.DeleteTaskRequest], 

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

430 ]: 

431 raise NotImplementedError() 

432 

433 @property 

434 def run_task( 

435 self, 

436 ) -> Callable[[cloudtasks.RunTaskRequest], Union[task.Task, Awaitable[task.Task]]]: 

437 raise NotImplementedError() 

438 

439 @property 

440 def kind(self) -> str: 

441 raise NotImplementedError() 

442 

443 

444__all__ = ("CloudTasksTransport",)