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
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.cloud.location import locations_pb2 # type: ignore
26from google.iam.v1 import iam_policy_pb2 # type: ignore
27from google.iam.v1 import policy_pb2 # type: ignore
28from google.oauth2 import service_account # type: ignore
29import google.protobuf
30from google.protobuf import empty_pb2 # type: ignore
31
32from google.cloud.tasks_v2 import gapic_version as package_version
33from google.cloud.tasks_v2.types import cloudtasks
34from google.cloud.tasks_v2.types import queue
35from google.cloud.tasks_v2.types import queue as gct_queue
36from google.cloud.tasks_v2.types import task
37from google.cloud.tasks_v2.types import task as gct_task
38
39DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo(
40 gapic_version=package_version.__version__
41)
42
43if hasattr(DEFAULT_CLIENT_INFO, "protobuf_runtime_version"): # pragma: NO COVER
44 DEFAULT_CLIENT_INFO.protobuf_runtime_version = google.protobuf.__version__
45
46
47class CloudTasksTransport(abc.ABC):
48 """Abstract transport class for CloudTasks."""
49
50 AUTH_SCOPES = ("https://www.googleapis.com/auth/cloud-platform",)
51
52 DEFAULT_HOST: str = "cloudtasks.googleapis.com"
53
54 def __init__(
55 self,
56 *,
57 host: str = DEFAULT_HOST,
58 credentials: Optional[ga_credentials.Credentials] = None,
59 credentials_file: Optional[str] = None,
60 scopes: Optional[Sequence[str]] = None,
61 quota_project_id: Optional[str] = None,
62 client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
63 always_use_jwt_access: Optional[bool] = False,
64 api_audience: Optional[str] = None,
65 **kwargs,
66 ) -> None:
67 """Instantiate the transport.
68
69 Args:
70 host (Optional[str]):
71 The hostname to connect to (default: 'cloudtasks.googleapis.com').
72 credentials (Optional[google.auth.credentials.Credentials]): The
73 authorization credentials to attach to requests. These
74 credentials identify the application to the service; if none
75 are specified, the client will attempt to ascertain the
76 credentials from the environment.
77 credentials_file (Optional[str]): Deprecated. A file with credentials that can
78 be loaded with :func:`google.auth.load_credentials_from_file`.
79 This argument is mutually exclusive with credentials. This argument will be
80 removed in the next major version of this library.
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 # Save the scopes.
94 self._scopes = scopes
95 if not hasattr(self, "_ignore_credentials"):
96 self._ignore_credentials: bool = False
97
98 # If no credentials are provided, then determine the appropriate
99 # defaults.
100 if credentials and credentials_file:
101 raise core_exceptions.DuplicateCredentialArgs(
102 "'credentials_file' and 'credentials' are mutually exclusive"
103 )
104
105 if credentials_file is not None:
106 credentials, _ = google.auth.load_credentials_from_file(
107 credentials_file,
108 scopes=scopes,
109 quota_project_id=quota_project_id,
110 default_scopes=self.AUTH_SCOPES,
111 )
112 elif credentials is None and not self._ignore_credentials:
113 credentials, _ = google.auth.default(
114 scopes=scopes,
115 quota_project_id=quota_project_id,
116 default_scopes=self.AUTH_SCOPES,
117 )
118 # Don't apply audience if the credentials file passed from user.
119 if hasattr(credentials, "with_gdch_audience"):
120 credentials = credentials.with_gdch_audience(
121 api_audience if api_audience else host
122 )
123
124 # If the credentials are service account credentials, then always try to use self signed JWT.
125 if (
126 always_use_jwt_access
127 and isinstance(credentials, service_account.Credentials)
128 and hasattr(service_account.Credentials, "with_always_use_jwt_access")
129 ):
130 credentials = credentials.with_always_use_jwt_access(True)
131
132 # Save the credentials.
133 self._credentials = credentials
134
135 # Save the hostname. Default to port 443 (HTTPS) if none is specified.
136 if ":" not in host:
137 host += ":443"
138 self._host = host
139
140 @property
141 def host(self):
142 return self._host
143
144 def _prep_wrapped_messages(self, client_info):
145 # Precompute the wrapped methods.
146 self._wrapped_methods = {
147 self.list_queues: gapic_v1.method.wrap_method(
148 self.list_queues,
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=20.0,
158 ),
159 default_timeout=20.0,
160 client_info=client_info,
161 ),
162 self.get_queue: gapic_v1.method.wrap_method(
163 self.get_queue,
164 default_retry=retries.Retry(
165 initial=0.1,
166 maximum=10.0,
167 multiplier=1.3,
168 predicate=retries.if_exception_type(
169 core_exceptions.DeadlineExceeded,
170 core_exceptions.ServiceUnavailable,
171 ),
172 deadline=20.0,
173 ),
174 default_timeout=20.0,
175 client_info=client_info,
176 ),
177 self.create_queue: gapic_v1.method.wrap_method(
178 self.create_queue,
179 default_timeout=20.0,
180 client_info=client_info,
181 ),
182 self.update_queue: gapic_v1.method.wrap_method(
183 self.update_queue,
184 default_timeout=20.0,
185 client_info=client_info,
186 ),
187 self.delete_queue: gapic_v1.method.wrap_method(
188 self.delete_queue,
189 default_retry=retries.Retry(
190 initial=0.1,
191 maximum=10.0,
192 multiplier=1.3,
193 predicate=retries.if_exception_type(
194 core_exceptions.DeadlineExceeded,
195 core_exceptions.ServiceUnavailable,
196 ),
197 deadline=20.0,
198 ),
199 default_timeout=20.0,
200 client_info=client_info,
201 ),
202 self.purge_queue: gapic_v1.method.wrap_method(
203 self.purge_queue,
204 default_timeout=20.0,
205 client_info=client_info,
206 ),
207 self.pause_queue: gapic_v1.method.wrap_method(
208 self.pause_queue,
209 default_timeout=20.0,
210 client_info=client_info,
211 ),
212 self.resume_queue: gapic_v1.method.wrap_method(
213 self.resume_queue,
214 default_timeout=20.0,
215 client_info=client_info,
216 ),
217 self.get_iam_policy: gapic_v1.method.wrap_method(
218 self.get_iam_policy,
219 default_retry=retries.Retry(
220 initial=0.1,
221 maximum=10.0,
222 multiplier=1.3,
223 predicate=retries.if_exception_type(
224 core_exceptions.DeadlineExceeded,
225 core_exceptions.ServiceUnavailable,
226 ),
227 deadline=20.0,
228 ),
229 default_timeout=20.0,
230 client_info=client_info,
231 ),
232 self.set_iam_policy: gapic_v1.method.wrap_method(
233 self.set_iam_policy,
234 default_timeout=20.0,
235 client_info=client_info,
236 ),
237 self.test_iam_permissions: gapic_v1.method.wrap_method(
238 self.test_iam_permissions,
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=20.0,
248 ),
249 default_timeout=20.0,
250 client_info=client_info,
251 ),
252 self.list_tasks: gapic_v1.method.wrap_method(
253 self.list_tasks,
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=20.0,
263 ),
264 default_timeout=20.0,
265 client_info=client_info,
266 ),
267 self.get_task: gapic_v1.method.wrap_method(
268 self.get_task,
269 default_retry=retries.Retry(
270 initial=0.1,
271 maximum=10.0,
272 multiplier=1.3,
273 predicate=retries.if_exception_type(
274 core_exceptions.DeadlineExceeded,
275 core_exceptions.ServiceUnavailable,
276 ),
277 deadline=20.0,
278 ),
279 default_timeout=20.0,
280 client_info=client_info,
281 ),
282 self.create_task: gapic_v1.method.wrap_method(
283 self.create_task,
284 default_timeout=20.0,
285 client_info=client_info,
286 ),
287 self.delete_task: gapic_v1.method.wrap_method(
288 self.delete_task,
289 default_retry=retries.Retry(
290 initial=0.1,
291 maximum=10.0,
292 multiplier=1.3,
293 predicate=retries.if_exception_type(
294 core_exceptions.DeadlineExceeded,
295 core_exceptions.ServiceUnavailable,
296 ),
297 deadline=20.0,
298 ),
299 default_timeout=20.0,
300 client_info=client_info,
301 ),
302 self.run_task: gapic_v1.method.wrap_method(
303 self.run_task,
304 default_timeout=20.0,
305 client_info=client_info,
306 ),
307 self.get_location: gapic_v1.method.wrap_method(
308 self.get_location,
309 default_timeout=None,
310 client_info=client_info,
311 ),
312 self.list_locations: gapic_v1.method.wrap_method(
313 self.list_locations,
314 default_timeout=None,
315 client_info=client_info,
316 ),
317 }
318
319 def close(self):
320 """Closes resources associated with the transport.
321
322 .. warning::
323 Only call this method if the transport is NOT shared
324 with other clients - this may cause errors in other clients!
325 """
326 raise NotImplementedError()
327
328 @property
329 def list_queues(
330 self,
331 ) -> Callable[
332 [cloudtasks.ListQueuesRequest],
333 Union[cloudtasks.ListQueuesResponse, Awaitable[cloudtasks.ListQueuesResponse]],
334 ]:
335 raise NotImplementedError()
336
337 @property
338 def get_queue(
339 self,
340 ) -> Callable[
341 [cloudtasks.GetQueueRequest], Union[queue.Queue, Awaitable[queue.Queue]]
342 ]:
343 raise NotImplementedError()
344
345 @property
346 def create_queue(
347 self,
348 ) -> Callable[
349 [cloudtasks.CreateQueueRequest],
350 Union[gct_queue.Queue, Awaitable[gct_queue.Queue]],
351 ]:
352 raise NotImplementedError()
353
354 @property
355 def update_queue(
356 self,
357 ) -> Callable[
358 [cloudtasks.UpdateQueueRequest],
359 Union[gct_queue.Queue, Awaitable[gct_queue.Queue]],
360 ]:
361 raise NotImplementedError()
362
363 @property
364 def delete_queue(
365 self,
366 ) -> Callable[
367 [cloudtasks.DeleteQueueRequest],
368 Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]],
369 ]:
370 raise NotImplementedError()
371
372 @property
373 def purge_queue(
374 self,
375 ) -> Callable[
376 [cloudtasks.PurgeQueueRequest], Union[queue.Queue, Awaitable[queue.Queue]]
377 ]:
378 raise NotImplementedError()
379
380 @property
381 def pause_queue(
382 self,
383 ) -> Callable[
384 [cloudtasks.PauseQueueRequest], Union[queue.Queue, Awaitable[queue.Queue]]
385 ]:
386 raise NotImplementedError()
387
388 @property
389 def resume_queue(
390 self,
391 ) -> Callable[
392 [cloudtasks.ResumeQueueRequest], Union[queue.Queue, Awaitable[queue.Queue]]
393 ]:
394 raise NotImplementedError()
395
396 @property
397 def get_iam_policy(
398 self,
399 ) -> Callable[
400 [iam_policy_pb2.GetIamPolicyRequest],
401 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
402 ]:
403 raise NotImplementedError()
404
405 @property
406 def set_iam_policy(
407 self,
408 ) -> Callable[
409 [iam_policy_pb2.SetIamPolicyRequest],
410 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
411 ]:
412 raise NotImplementedError()
413
414 @property
415 def test_iam_permissions(
416 self,
417 ) -> Callable[
418 [iam_policy_pb2.TestIamPermissionsRequest],
419 Union[
420 iam_policy_pb2.TestIamPermissionsResponse,
421 Awaitable[iam_policy_pb2.TestIamPermissionsResponse],
422 ],
423 ]:
424 raise NotImplementedError()
425
426 @property
427 def list_tasks(
428 self,
429 ) -> Callable[
430 [cloudtasks.ListTasksRequest],
431 Union[cloudtasks.ListTasksResponse, Awaitable[cloudtasks.ListTasksResponse]],
432 ]:
433 raise NotImplementedError()
434
435 @property
436 def get_task(
437 self,
438 ) -> Callable[[cloudtasks.GetTaskRequest], Union[task.Task, Awaitable[task.Task]]]:
439 raise NotImplementedError()
440
441 @property
442 def create_task(
443 self,
444 ) -> Callable[
445 [cloudtasks.CreateTaskRequest], Union[gct_task.Task, Awaitable[gct_task.Task]]
446 ]:
447 raise NotImplementedError()
448
449 @property
450 def delete_task(
451 self,
452 ) -> Callable[
453 [cloudtasks.DeleteTaskRequest],
454 Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]],
455 ]:
456 raise NotImplementedError()
457
458 @property
459 def run_task(
460 self,
461 ) -> Callable[[cloudtasks.RunTaskRequest], Union[task.Task, Awaitable[task.Task]]]:
462 raise NotImplementedError()
463
464 @property
465 def get_location(
466 self,
467 ) -> Callable[
468 [locations_pb2.GetLocationRequest],
469 Union[locations_pb2.Location, Awaitable[locations_pb2.Location]],
470 ]:
471 raise NotImplementedError()
472
473 @property
474 def list_locations(
475 self,
476 ) -> Callable[
477 [locations_pb2.ListLocationsRequest],
478 Union[
479 locations_pb2.ListLocationsResponse,
480 Awaitable[locations_pb2.ListLocationsResponse],
481 ],
482 ]:
483 raise NotImplementedError()
484
485 @property
486 def kind(self) -> str:
487 raise NotImplementedError()
488
489
490__all__ = ("CloudTasksTransport",)