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 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_queues: gapic_v1.method.wrap_method(
145 self.list_queues,
146 default_retry=retries.Retry(
147 initial=0.1,
148 maximum=10.0,
149 multiplier=1.3,
150 predicate=retries.if_exception_type(
151 core_exceptions.DeadlineExceeded,
152 core_exceptions.ServiceUnavailable,
153 ),
154 deadline=20.0,
155 ),
156 default_timeout=20.0,
157 client_info=client_info,
158 ),
159 self.get_queue: gapic_v1.method.wrap_method(
160 self.get_queue,
161 default_retry=retries.Retry(
162 initial=0.1,
163 maximum=10.0,
164 multiplier=1.3,
165 predicate=retries.if_exception_type(
166 core_exceptions.DeadlineExceeded,
167 core_exceptions.ServiceUnavailable,
168 ),
169 deadline=20.0,
170 ),
171 default_timeout=20.0,
172 client_info=client_info,
173 ),
174 self.create_queue: gapic_v1.method.wrap_method(
175 self.create_queue,
176 default_timeout=20.0,
177 client_info=client_info,
178 ),
179 self.update_queue: gapic_v1.method.wrap_method(
180 self.update_queue,
181 default_timeout=20.0,
182 client_info=client_info,
183 ),
184 self.delete_queue: gapic_v1.method.wrap_method(
185 self.delete_queue,
186 default_retry=retries.Retry(
187 initial=0.1,
188 maximum=10.0,
189 multiplier=1.3,
190 predicate=retries.if_exception_type(
191 core_exceptions.DeadlineExceeded,
192 core_exceptions.ServiceUnavailable,
193 ),
194 deadline=20.0,
195 ),
196 default_timeout=20.0,
197 client_info=client_info,
198 ),
199 self.purge_queue: gapic_v1.method.wrap_method(
200 self.purge_queue,
201 default_timeout=20.0,
202 client_info=client_info,
203 ),
204 self.pause_queue: gapic_v1.method.wrap_method(
205 self.pause_queue,
206 default_timeout=20.0,
207 client_info=client_info,
208 ),
209 self.resume_queue: gapic_v1.method.wrap_method(
210 self.resume_queue,
211 default_timeout=20.0,
212 client_info=client_info,
213 ),
214 self.get_iam_policy: gapic_v1.method.wrap_method(
215 self.get_iam_policy,
216 default_retry=retries.Retry(
217 initial=0.1,
218 maximum=10.0,
219 multiplier=1.3,
220 predicate=retries.if_exception_type(
221 core_exceptions.DeadlineExceeded,
222 core_exceptions.ServiceUnavailable,
223 ),
224 deadline=20.0,
225 ),
226 default_timeout=20.0,
227 client_info=client_info,
228 ),
229 self.set_iam_policy: gapic_v1.method.wrap_method(
230 self.set_iam_policy,
231 default_timeout=20.0,
232 client_info=client_info,
233 ),
234 self.test_iam_permissions: gapic_v1.method.wrap_method(
235 self.test_iam_permissions,
236 default_retry=retries.Retry(
237 initial=0.1,
238 maximum=10.0,
239 multiplier=1.3,
240 predicate=retries.if_exception_type(
241 core_exceptions.DeadlineExceeded,
242 core_exceptions.ServiceUnavailable,
243 ),
244 deadline=20.0,
245 ),
246 default_timeout=20.0,
247 client_info=client_info,
248 ),
249 self.list_tasks: gapic_v1.method.wrap_method(
250 self.list_tasks,
251 default_retry=retries.Retry(
252 initial=0.1,
253 maximum=10.0,
254 multiplier=1.3,
255 predicate=retries.if_exception_type(
256 core_exceptions.DeadlineExceeded,
257 core_exceptions.ServiceUnavailable,
258 ),
259 deadline=20.0,
260 ),
261 default_timeout=20.0,
262 client_info=client_info,
263 ),
264 self.get_task: gapic_v1.method.wrap_method(
265 self.get_task,
266 default_retry=retries.Retry(
267 initial=0.1,
268 maximum=10.0,
269 multiplier=1.3,
270 predicate=retries.if_exception_type(
271 core_exceptions.DeadlineExceeded,
272 core_exceptions.ServiceUnavailable,
273 ),
274 deadline=20.0,
275 ),
276 default_timeout=20.0,
277 client_info=client_info,
278 ),
279 self.create_task: gapic_v1.method.wrap_method(
280 self.create_task,
281 default_timeout=20.0,
282 client_info=client_info,
283 ),
284 self.delete_task: gapic_v1.method.wrap_method(
285 self.delete_task,
286 default_retry=retries.Retry(
287 initial=0.1,
288 maximum=10.0,
289 multiplier=1.3,
290 predicate=retries.if_exception_type(
291 core_exceptions.DeadlineExceeded,
292 core_exceptions.ServiceUnavailable,
293 ),
294 deadline=20.0,
295 ),
296 default_timeout=20.0,
297 client_info=client_info,
298 ),
299 self.run_task: gapic_v1.method.wrap_method(
300 self.run_task,
301 default_timeout=20.0,
302 client_info=client_info,
303 ),
304 self.get_location: gapic_v1.method.wrap_method(
305 self.get_location,
306 default_timeout=None,
307 client_info=client_info,
308 ),
309 self.list_locations: gapic_v1.method.wrap_method(
310 self.list_locations,
311 default_timeout=None,
312 client_info=client_info,
313 ),
314 }
315
316 def close(self):
317 """Closes resources associated with the transport.
318
319 .. warning::
320 Only call this method if the transport is NOT shared
321 with other clients - this may cause errors in other clients!
322 """
323 raise NotImplementedError()
324
325 @property
326 def list_queues(
327 self,
328 ) -> Callable[
329 [cloudtasks.ListQueuesRequest],
330 Union[cloudtasks.ListQueuesResponse, Awaitable[cloudtasks.ListQueuesResponse]],
331 ]:
332 raise NotImplementedError()
333
334 @property
335 def get_queue(
336 self,
337 ) -> Callable[
338 [cloudtasks.GetQueueRequest], Union[queue.Queue, Awaitable[queue.Queue]]
339 ]:
340 raise NotImplementedError()
341
342 @property
343 def create_queue(
344 self,
345 ) -> Callable[
346 [cloudtasks.CreateQueueRequest],
347 Union[gct_queue.Queue, Awaitable[gct_queue.Queue]],
348 ]:
349 raise NotImplementedError()
350
351 @property
352 def update_queue(
353 self,
354 ) -> Callable[
355 [cloudtasks.UpdateQueueRequest],
356 Union[gct_queue.Queue, Awaitable[gct_queue.Queue]],
357 ]:
358 raise NotImplementedError()
359
360 @property
361 def delete_queue(
362 self,
363 ) -> Callable[
364 [cloudtasks.DeleteQueueRequest],
365 Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]],
366 ]:
367 raise NotImplementedError()
368
369 @property
370 def purge_queue(
371 self,
372 ) -> Callable[
373 [cloudtasks.PurgeQueueRequest], Union[queue.Queue, Awaitable[queue.Queue]]
374 ]:
375 raise NotImplementedError()
376
377 @property
378 def pause_queue(
379 self,
380 ) -> Callable[
381 [cloudtasks.PauseQueueRequest], Union[queue.Queue, Awaitable[queue.Queue]]
382 ]:
383 raise NotImplementedError()
384
385 @property
386 def resume_queue(
387 self,
388 ) -> Callable[
389 [cloudtasks.ResumeQueueRequest], Union[queue.Queue, Awaitable[queue.Queue]]
390 ]:
391 raise NotImplementedError()
392
393 @property
394 def get_iam_policy(
395 self,
396 ) -> Callable[
397 [iam_policy_pb2.GetIamPolicyRequest],
398 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
399 ]:
400 raise NotImplementedError()
401
402 @property
403 def set_iam_policy(
404 self,
405 ) -> Callable[
406 [iam_policy_pb2.SetIamPolicyRequest],
407 Union[policy_pb2.Policy, Awaitable[policy_pb2.Policy]],
408 ]:
409 raise NotImplementedError()
410
411 @property
412 def test_iam_permissions(
413 self,
414 ) -> Callable[
415 [iam_policy_pb2.TestIamPermissionsRequest],
416 Union[
417 iam_policy_pb2.TestIamPermissionsResponse,
418 Awaitable[iam_policy_pb2.TestIamPermissionsResponse],
419 ],
420 ]:
421 raise NotImplementedError()
422
423 @property
424 def list_tasks(
425 self,
426 ) -> Callable[
427 [cloudtasks.ListTasksRequest],
428 Union[cloudtasks.ListTasksResponse, Awaitable[cloudtasks.ListTasksResponse]],
429 ]:
430 raise NotImplementedError()
431
432 @property
433 def get_task(
434 self,
435 ) -> Callable[[cloudtasks.GetTaskRequest], Union[task.Task, Awaitable[task.Task]]]:
436 raise NotImplementedError()
437
438 @property
439 def create_task(
440 self,
441 ) -> Callable[
442 [cloudtasks.CreateTaskRequest], Union[gct_task.Task, Awaitable[gct_task.Task]]
443 ]:
444 raise NotImplementedError()
445
446 @property
447 def delete_task(
448 self,
449 ) -> Callable[
450 [cloudtasks.DeleteTaskRequest],
451 Union[empty_pb2.Empty, Awaitable[empty_pb2.Empty]],
452 ]:
453 raise NotImplementedError()
454
455 @property
456 def run_task(
457 self,
458 ) -> Callable[[cloudtasks.RunTaskRequest], Union[task.Task, Awaitable[task.Task]]]:
459 raise NotImplementedError()
460
461 @property
462 def get_location(
463 self,
464 ) -> Callable[
465 [locations_pb2.GetLocationRequest],
466 Union[locations_pb2.Location, Awaitable[locations_pb2.Location]],
467 ]:
468 raise NotImplementedError()
469
470 @property
471 def list_locations(
472 self,
473 ) -> Callable[
474 [locations_pb2.ListLocationsRequest],
475 Union[
476 locations_pb2.ListLocationsResponse,
477 Awaitable[locations_pb2.ListLocationsResponse],
478 ],
479 ]:
480 raise NotImplementedError()
481
482 @property
483 def kind(self) -> str:
484 raise NotImplementedError()
485
486
487__all__ = ("CloudTasksTransport",)