0
# Client Configuration
1
2
Client instantiation, authentication, and configuration options for both synchronous and asynchronous operation modes. This covers how to create and configure Route Optimization clients with various authentication methods and settings.
3
4
## Capabilities
5
6
### Client Classes
7
8
#### RouteOptimizationClient
9
10
Synchronous client for Route Optimization API interactions.
11
12
```python { .api }
13
class RouteOptimizationClient:
14
def __init__(
15
self,
16
*,
17
credentials: Optional[ga_credentials.Credentials] = None,
18
transport: Optional[Union[str, RouteOptimizationTransport, Callable[..., RouteOptimizationTransport]]] = None,
19
client_options: Optional[Union[client_options_lib.ClientOptions, dict]] = None,
20
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO
21
): ...
22
23
@classmethod
24
def from_service_account_info(
25
cls,
26
info: dict,
27
*args,
28
**kwargs
29
) -> "RouteOptimizationClient": ...
30
31
@classmethod
32
def from_service_account_file(
33
cls,
34
filename: str,
35
*args,
36
**kwargs
37
) -> "RouteOptimizationClient": ...
38
39
@property
40
def transport(self) -> RouteOptimizationTransport: ...
41
42
@staticmethod
43
def common_billing_account_path(billing_account: str) -> str: ...
44
45
@staticmethod
46
def parse_common_billing_account_path(path: str) -> Dict[str, str]: ...
47
48
@staticmethod
49
def common_folder_path(folder: str) -> str: ...
50
51
@staticmethod
52
def parse_common_folder_path(path: str) -> Dict[str, str]: ...
53
54
@staticmethod
55
def common_organization_path(organization: str) -> str: ...
56
57
@staticmethod
58
def parse_common_organization_path(path: str) -> Dict[str, str]: ...
59
60
@staticmethod
61
def common_project_path(project: str) -> str: ...
62
63
@staticmethod
64
def parse_common_project_path(path: str) -> Dict[str, str]: ...
65
66
@staticmethod
67
def common_location_path(project: str, location: str) -> str: ...
68
69
@staticmethod
70
def parse_common_location_path(path: str) -> Dict[str, str]: ...
71
```
72
73
#### RouteOptimizationAsyncClient
74
75
Asynchronous client for non-blocking Route Optimization API interactions.
76
77
```python { .api }
78
class RouteOptimizationAsyncClient:
79
def __init__(
80
self,
81
*,
82
credentials: Optional[ga_credentials.Credentials] = None,
83
transport: Optional[Union[str, RouteOptimizationAsyncTransport, Callable[..., RouteOptimizationAsyncTransport]]] = None,
84
client_options: Optional[Union[client_options_lib.ClientOptions, dict]] = None,
85
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO
86
): ...
87
88
@classmethod
89
def from_service_account_info(
90
cls,
91
info: dict,
92
*args,
93
**kwargs
94
) -> "RouteOptimizationAsyncClient": ...
95
96
@classmethod
97
def from_service_account_file(
98
cls,
99
filename: str,
100
*args,
101
**kwargs
102
) -> "RouteOptimizationAsyncClient": ...
103
104
@property
105
def transport(self) -> RouteOptimizationAsyncTransport: ...
106
107
# Same path helper methods as sync client
108
```
109
110
## Authentication Methods
111
112
### Application Default Credentials
113
114
```python
115
from google.maps import routeoptimization_v1
116
117
# Use Application Default Credentials (recommended)
118
client = routeoptimization_v1.RouteOptimizationClient()
119
```
120
121
### Service Account File
122
123
```python
124
from google.maps import routeoptimization_v1
125
126
# From service account file
127
client = routeoptimization_v1.RouteOptimizationClient.from_service_account_file(
128
"path/to/service-account-key.json"
129
)
130
131
# Async client from service account file
132
async_client = routeoptimization_v1.RouteOptimizationAsyncClient.from_service_account_file(
133
"path/to/service-account-key.json"
134
)
135
```
136
137
### Service Account Info
138
139
```python
140
from google.maps import routeoptimization_v1
141
142
# From service account info dictionary
143
service_account_info = {
144
"type": "service_account",
145
"project_id": "your-project-id",
146
"private_key_id": "key-id",
147
"private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
148
"client_email": "service-account@your-project.iam.gserviceaccount.com",
149
"client_id": "client-id",
150
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
151
"token_uri": "https://oauth2.googleapis.com/token"
152
}
153
154
client = routeoptimization_v1.RouteOptimizationClient.from_service_account_info(
155
service_account_info
156
)
157
```
158
159
### Custom Credentials
160
161
```python
162
from google.maps import routeoptimization_v1
163
from google.oauth2 import service_account
164
165
# Create credentials manually
166
credentials = service_account.Credentials.from_service_account_file(
167
"path/to/service-account-key.json",
168
scopes=["https://www.googleapis.com/auth/cloud-platform"]
169
)
170
171
client = routeoptimization_v1.RouteOptimizationClient(credentials=credentials)
172
```
173
174
## Client Options
175
176
### Regional Endpoints
177
178
```python
179
from google.maps import routeoptimization_v1
180
from google.api_core import client_options
181
182
# Use regional endpoint
183
options = client_options.ClientOptions(
184
api_endpoint="us-central1-routeoptimization.googleapis.com"
185
)
186
187
client = routeoptimization_v1.RouteOptimizationClient(client_options=options)
188
```
189
190
### Custom Transport
191
192
```python
193
from google.maps import routeoptimization_v1
194
195
# Use specific transport (grpc, rest)
196
client = routeoptimization_v1.RouteOptimizationClient(transport="grpc")
197
198
# For async client
199
async_client = routeoptimization_v1.RouteOptimizationAsyncClient(transport="grpc_asyncio")
200
```
201
202
### Client Info
203
204
```python
205
from google.maps import routeoptimization_v1
206
from google.api_core import gapic_v1
207
208
# Custom client info
209
client_info = gapic_v1.client_info.ClientInfo(
210
client_library_version="1.0.0",
211
user_agent="MyApp/1.0.0"
212
)
213
214
client = routeoptimization_v1.RouteOptimizationClient(client_info=client_info)
215
```
216
217
## Path Helper Methods
218
219
Utility methods for constructing and parsing Google Cloud resource paths.
220
221
### Project Paths
222
223
```python
224
from google.maps import routeoptimization_v1
225
226
client = routeoptimization_v1.RouteOptimizationClient()
227
228
# Create project path
229
project_path = client.common_project_path("my-project-id")
230
# Returns: "projects/my-project-id"
231
232
# Parse project path
233
parsed = client.parse_common_project_path("projects/my-project-id")
234
# Returns: {"project": "my-project-id"}
235
```
236
237
### Location Paths
238
239
```python
240
from google.maps import routeoptimization_v1
241
242
client = routeoptimization_v1.RouteOptimizationClient()
243
244
# Create location path
245
location_path = client.common_location_path("my-project-id", "us-central1")
246
# Returns: "projects/my-project-id/locations/us-central1"
247
248
# Parse location path
249
parsed = client.parse_common_location_path("projects/my-project-id/locations/us-central1")
250
# Returns: {"project": "my-project-id", "location": "us-central1"}
251
```
252
253
## Usage Examples
254
255
### Basic Client Setup
256
257
```python
258
from google.maps import routeoptimization_v1
259
260
# Simple client with default credentials
261
client = routeoptimization_v1.RouteOptimizationClient()
262
263
# Test connection with a simple request
264
request = routeoptimization_v1.OptimizeToursRequest(
265
parent=client.common_project_path("my-project-id")
266
)
267
```
268
269
### Production Configuration
270
271
```python
272
from google.maps import routeoptimization_v1
273
from google.api_core import client_options, retry
274
import google.auth
275
276
# Production setup with custom configuration
277
credentials, project_id = google.auth.default(
278
scopes=["https://www.googleapis.com/auth/cloud-platform"]
279
)
280
281
options = client_options.ClientOptions(
282
api_endpoint="us-central1-routeoptimization.googleapis.com",
283
quota_project_id=project_id
284
)
285
286
client = routeoptimization_v1.RouteOptimizationClient(
287
credentials=credentials,
288
client_options=options
289
)
290
291
# Use with custom retry configuration
292
custom_retry = retry.Retry(
293
initial=1.0,
294
maximum=60.0,
295
multiplier=2.0,
296
predicate=retry.if_exception_type(Exception)
297
)
298
299
response = client.optimize_tours(
300
request=request,
301
retry=custom_retry,
302
timeout=300.0 # 5 minutes
303
)
304
```
305
306
### Async Client Usage
307
308
```python
309
import asyncio
310
from google.maps import routeoptimization_v1
311
312
async def optimize_routes():
313
# Create async client
314
async_client = routeoptimization_v1.RouteOptimizationAsyncClient()
315
316
# Create request
317
request = routeoptimization_v1.OptimizeToursRequest(
318
parent="projects/my-project-id",
319
model=your_shipment_model
320
)
321
322
# Make async request
323
response = await async_client.optimize_tours(request=request)
324
325
# Process response
326
print(f"Optimized {len(response.routes)} routes")
327
328
# Close client
329
await async_client.close()
330
331
# Run async function
332
asyncio.run(optimize_routes())
333
```
334
335
### Environment-Based Configuration
336
337
```python
338
import os
339
from google.maps import routeoptimization_v1
340
from google.api_core import client_options
341
342
# Configure based on environment
343
def create_client():
344
if os.getenv("ENVIRONMENT") == "production":
345
# Production configuration
346
options = client_options.ClientOptions(
347
api_endpoint="us-central1-routeoptimization.googleapis.com"
348
)
349
return routeoptimization_v1.RouteOptimizationClient(
350
client_options=options
351
)
352
else:
353
# Development configuration
354
return routeoptimization_v1.RouteOptimizationClient()
355
356
client = create_client()
357
```
358
359
### Error Handling
360
361
```python
362
from google.maps import routeoptimization_v1
363
from google.api_core import exceptions
364
import logging
365
366
client = routeoptimization_v1.RouteOptimizationClient()
367
368
try:
369
response = client.optimize_tours(request=request)
370
except exceptions.PermissionDenied as e:
371
logging.error(f"Permission denied: {e}")
372
except exceptions.NotFound as e:
373
logging.error(f"Resource not found: {e}")
374
except exceptions.DeadlineExceeded as e:
375
logging.error(f"Request timeout: {e}")
376
except Exception as e:
377
logging.error(f"Unexpected error: {e}")
378
```