0
# Client Operations
1
2
Core client functionality for managing Cloud Tasks service connections, authentication, and transport configuration. Provides both synchronous and asynchronous clients with identical APIs.
3
4
## Capabilities
5
6
### CloudTasksClient
7
8
Synchronous client for Cloud Tasks service operations with automatic authentication and transport management.
9
10
```python { .api }
11
class CloudTasksClient:
12
def __init__(
13
self,
14
*,
15
credentials: ga_credentials.Credentials = None,
16
transport: Union[str, CloudTasksTransport] = None,
17
client_options: client_options_lib.ClientOptions = None,
18
client_info: gapic_v1.client_info.ClientInfo = None
19
):
20
"""Initialize the CloudTasksClient.
21
22
Args:
23
credentials: The authorization credentials to attach to requests.
24
transport: The transport to use for API calls.
25
client_options: Custom options for the client.
26
client_info: The client info used to send a user-agent string.
27
"""
28
29
@classmethod
30
def from_service_account_file(
31
cls,
32
filename: str,
33
*args,
34
**kwargs
35
) -> "CloudTasksClient":
36
"""Create a client from a service account JSON file.
37
38
Args:
39
filename: Path to the service account JSON file.
40
41
Returns:
42
The constructed client.
43
"""
44
45
@classmethod
46
def from_service_account_info(
47
cls,
48
info: dict,
49
*args,
50
**kwargs
51
) -> "CloudTasksClient":
52
"""Create a client from service account info.
53
54
Args:
55
info: The service account info in Google format.
56
57
Returns:
58
The constructed client.
59
"""
60
61
@classmethod
62
def get_transport_class(
63
cls,
64
label: Optional[str] = None
65
) -> Type[CloudTasksTransport]:
66
"""Return an appropriate transport class.
67
68
Args:
69
label: The name of the desired transport.
70
71
Returns:
72
The transport class to use.
73
"""
74
75
@property
76
def transport(self) -> CloudTasksTransport:
77
"""Return the transport used by the client."""
78
79
@property
80
def api_endpoint(self) -> str:
81
"""Return the API endpoint used by the client."""
82
83
@property
84
def universe_domain(self) -> str:
85
"""Return the universe domain used by the client."""
86
87
def __enter__(self) -> "CloudTasksClient":
88
"""Enter the context manager."""
89
90
def __exit__(self, type, value, traceback) -> None:
91
"""Exit the context manager."""
92
```
93
94
### CloudTasksAsyncClient
95
96
Asynchronous client for Cloud Tasks service operations with async/await support and identical API to the synchronous client.
97
98
```python { .api }
99
class CloudTasksAsyncClient:
100
def __init__(
101
self,
102
*,
103
credentials: ga_credentials.Credentials = None,
104
transport: Union[str, CloudTasksAsyncTransport] = None,
105
client_options: client_options_lib.ClientOptions = None,
106
client_info: gapic_v1.client_info.ClientInfo = None
107
):
108
"""Initialize the CloudTasksAsyncClient.
109
110
Args:
111
credentials: The authorization credentials to attach to requests.
112
transport: The transport to use for API calls.
113
client_options: Custom options for the client.
114
client_info: The client info used to send a user-agent string.
115
"""
116
117
@classmethod
118
def from_service_account_file(
119
cls,
120
filename: str,
121
*args,
122
**kwargs
123
) -> "CloudTasksAsyncClient":
124
"""Create an async client from a service account JSON file."""
125
126
@classmethod
127
def from_service_account_info(
128
cls,
129
info: dict,
130
*args,
131
**kwargs
132
) -> "CloudTasksAsyncClient":
133
"""Create an async client from service account info."""
134
135
async def __aenter__(self) -> "CloudTasksAsyncClient":
136
"""Enter the async context manager."""
137
138
async def __aexit__(self, exc_type, exc, tb) -> None:
139
"""Exit the async context manager."""
140
```
141
142
### Path Utilities
143
144
Static methods for constructing and parsing Google Cloud resource paths.
145
146
```python { .api }
147
@staticmethod
148
def queue_path(project: str, location: str, queue: str) -> str:
149
"""Return a fully-qualified queue string.
150
151
Args:
152
project: The project ID.
153
location: The location ID.
154
queue: The queue ID.
155
156
Returns:
157
The queue path string.
158
"""
159
160
@staticmethod
161
def parse_queue_path(path: str) -> Dict[str, str]:
162
"""Parse a queue path into its component segments.
163
164
Args:
165
path: A fully-qualified queue path.
166
167
Returns:
168
A dictionary with project, location, and queue keys.
169
"""
170
171
@staticmethod
172
def task_path(project: str, location: str, queue: str, task: str) -> str:
173
"""Return a fully-qualified task string.
174
175
Args:
176
project: The project ID.
177
location: The location ID.
178
queue: The queue ID.
179
task: The task ID.
180
181
Returns:
182
The task path string.
183
"""
184
185
@staticmethod
186
def parse_task_path(path: str) -> Dict[str, str]:
187
"""Parse a task path into its component segments.
188
189
Args:
190
path: A fully-qualified task path.
191
192
Returns:
193
A dictionary with project, location, queue, and task keys.
194
"""
195
196
@staticmethod
197
def common_project_path(project: str) -> str:
198
"""Return a fully-qualified project string."""
199
200
@staticmethod
201
def parse_common_project_path(path: str) -> Dict[str, str]:
202
"""Parse a project path into its component segments."""
203
204
@staticmethod
205
def common_location_path(project: str, location: str) -> str:
206
"""Return a fully-qualified location string."""
207
208
@staticmethod
209
def parse_common_location_path(path: str) -> Dict[str, str]:
210
"""Parse a location path into its component segments."""
211
```
212
213
## Usage Examples
214
215
### Basic Client Creation
216
217
```python
218
from google.cloud import tasks
219
220
# Create a synchronous client with default credentials
221
client = tasks.CloudTasksClient()
222
223
# Create from service account file
224
client = tasks.CloudTasksClient.from_service_account_file('path/to/service-account.json')
225
226
# Create from service account info dictionary
227
service_account_info = {...} # Service account JSON data
228
client = tasks.CloudTasksClient.from_service_account_info(service_account_info)
229
```
230
231
### Asynchronous Client Usage
232
233
```python
234
import asyncio
235
from google.cloud import tasks
236
237
async def main():
238
# Create an async client
239
async with tasks.CloudTasksAsyncClient() as client:
240
# Use client for async operations
241
queues = await client.list_queues(parent='projects/my-project/locations/us-central1')
242
async for queue in queues:
243
print(f'Queue: {queue.name}')
244
245
asyncio.run(main())
246
```
247
248
### Path Construction
249
250
```python
251
from google.cloud import tasks
252
253
client = tasks.CloudTasksClient()
254
255
# Construct resource paths
256
project = 'my-project-id'
257
location = 'us-central1'
258
queue_name = 'my-queue'
259
task_name = 'my-task'
260
261
# Build paths
262
parent_path = client.common_location_path(project, location)
263
queue_path = client.queue_path(project, location, queue_name)
264
task_path = client.task_path(project, location, queue_name, task_name)
265
266
print(f'Parent: {parent_path}')
267
print(f'Queue: {queue_path}')
268
print(f'Task: {task_path}')
269
270
# Parse paths
271
queue_parts = client.parse_queue_path(queue_path)
272
print(f'Project: {queue_parts["project"]}')
273
print(f'Location: {queue_parts["location"]}')
274
print(f'Queue: {queue_parts["queue"]}')
275
```