0
# Resource Path Helpers
1
2
Utility methods for constructing and parsing Google Cloud resource paths. These helpers ensure correct resource identification and are essential for working with Google Cloud APIs, following the standard resource naming conventions.
3
4
## Conversion Source Paths
5
6
### conversion_source_path
7
8
Constructs a conversion source resource path from account and conversion source identifiers.
9
10
```python { .api }
11
@classmethod
12
def conversion_source_path(cls, account: str, conversion_source: str) -> str: ...
13
```
14
15
**Parameters:**
16
- `account`: The account ID
17
- `conversion_source`: The conversion source ID
18
19
**Returns:** Formatted resource path string
20
21
**Usage Example:**
22
```python
23
from google.shopping import merchant_conversions_v1
24
25
# Construct conversion source path
26
path = merchant_conversions_v1.ConversionSourcesServiceClient.conversion_source_path(
27
account="123456789",
28
conversion_source="abc123"
29
)
30
print(path) # accounts/123456789/conversionSources/abc123
31
32
# Use in API calls
33
client = merchant_conversions_v1.ConversionSourcesServiceClient()
34
request = merchant_conversions_v1.GetConversionSourceRequest(name=path)
35
response = client.get_conversion_source(request=request)
36
```
37
38
### parse_conversion_source_path
39
40
Parses a conversion source resource path into its component parts.
41
42
```python { .api }
43
@staticmethod
44
def parse_conversion_source_path(path: str) -> Dict[str, str]: ...
45
```
46
47
**Parameters:**
48
- `path`: The resource path to parse
49
50
**Returns:** Dictionary with 'account' and 'conversion_source' keys
51
52
**Usage Example:**
53
```python
54
from google.shopping import merchant_conversions_v1
55
56
# Parse conversion source path
57
path = "accounts/123456789/conversionSources/abc123"
58
parsed = merchant_conversions_v1.ConversionSourcesServiceClient.parse_conversion_source_path(path)
59
print(parsed) # {'account': '123456789', 'conversion_source': 'abc123'}
60
61
# Extract components
62
account_id = parsed['account']
63
source_id = parsed['conversion_source']
64
```
65
66
## Common Resource Paths
67
68
### Billing Account Paths
69
70
```python { .api }
71
@classmethod
72
def common_billing_account_path(cls, billing_account: str) -> str: ...
73
74
@staticmethod
75
def parse_common_billing_account_path(path: str) -> Dict[str, str]: ...
76
```
77
78
**Usage Example:**
79
```python
80
from google.shopping import merchant_conversions_v1
81
82
# Construct billing account path
83
path = merchant_conversions_v1.ConversionSourcesServiceClient.common_billing_account_path("123456789")
84
print(path) # billingAccounts/123456789
85
86
# Parse billing account path
87
parsed = merchant_conversions_v1.ConversionSourcesServiceClient.parse_common_billing_account_path(path)
88
print(parsed) # {'billing_account': '123456789'}
89
```
90
91
### Folder Paths
92
93
```python { .api }
94
@classmethod
95
def common_folder_path(cls, folder: str) -> str: ...
96
97
@staticmethod
98
def parse_common_folder_path(path: str) -> Dict[str, str]: ...
99
```
100
101
**Usage Example:**
102
```python
103
from google.shopping import merchant_conversions_v1
104
105
# Construct folder path
106
path = merchant_conversions_v1.ConversionSourcesServiceClient.common_folder_path("987654321")
107
print(path) # folders/987654321
108
109
# Parse folder path
110
parsed = merchant_conversions_v1.ConversionSourcesServiceClient.parse_common_folder_path(path)
111
print(parsed) # {'folder': '987654321'}
112
```
113
114
### Organization Paths
115
116
```python { .api }
117
@classmethod
118
def common_organization_path(cls, organization: str) -> str: ...
119
120
@staticmethod
121
def parse_common_organization_path(path: str) -> Dict[str, str]: ...
122
```
123
124
**Usage Example:**
125
```python
126
from google.shopping import merchant_conversions_v1
127
128
# Construct organization path
129
path = merchant_conversions_v1.ConversionSourcesServiceClient.common_organization_path("456789123")
130
print(path) # organizations/456789123
131
132
# Parse organization path
133
parsed = merchant_conversions_v1.ConversionSourcesServiceClient.parse_common_organization_path(path)
134
print(parsed) # {'organization': '456789123'}
135
```
136
137
### Project Paths
138
139
```python { .api }
140
@classmethod
141
def common_project_path(cls, project: str) -> str: ...
142
143
@staticmethod
144
def parse_common_project_path(path: str) -> Dict[str, str]: ...
145
```
146
147
**Usage Example:**
148
```python
149
from google.shopping import merchant_conversions_v1
150
151
# Construct project path
152
path = merchant_conversions_v1.ConversionSourcesServiceClient.common_project_path("my-project-id")
153
print(path) # projects/my-project-id
154
155
# Parse project path
156
parsed = merchant_conversions_v1.ConversionSourcesServiceClient.parse_common_project_path(path)
157
print(parsed) # {'project': 'my-project-id'}
158
```
159
160
### Location Paths
161
162
```python { .api }
163
@classmethod
164
def common_location_path(cls, project: str, location: str) -> str: ...
165
166
@staticmethod
167
def parse_common_location_path(path: str) -> Dict[str, str]: ...
168
```
169
170
**Parameters for common_location_path:**
171
- `project`: The project ID
172
- `location`: The location/region identifier
173
174
**Usage Example:**
175
```python
176
from google.shopping import merchant_conversions_v1
177
178
# Construct location path
179
path = merchant_conversions_v1.ConversionSourcesServiceClient.common_location_path(
180
project="my-project-id",
181
location="us-central1"
182
)
183
print(path) # projects/my-project-id/locations/us-central1
184
185
# Parse location path
186
parsed = merchant_conversions_v1.ConversionSourcesServiceClient.parse_common_location_path(path)
187
print(parsed) # {'project': 'my-project-id', 'location': 'us-central1'}
188
```
189
190
## Path Validation and Error Handling
191
192
Resource path helpers include validation and will raise exceptions for invalid inputs:
193
194
```python
195
from google.shopping import merchant_conversions_v1
196
197
try:
198
# Valid path construction
199
path = merchant_conversions_v1.ConversionSourcesServiceClient.conversion_source_path(
200
account="123456789",
201
conversion_source="valid-id"
202
)
203
print(f"Valid path: {path}")
204
205
# Valid path parsing
206
parsed = merchant_conversions_v1.ConversionSourcesServiceClient.parse_conversion_source_path(path)
207
print(f"Parsed: {parsed}")
208
209
except ValueError as e:
210
print(f"Invalid path format: {e}")
211
except TypeError as e:
212
print(f"Invalid parameter type: {e}")
213
214
try:
215
# Invalid path parsing
216
invalid_path = "invalid/path/format"
217
parsed = merchant_conversions_v1.ConversionSourcesServiceClient.parse_conversion_source_path(invalid_path)
218
except ValueError as e:
219
print(f"Failed to parse invalid path: {e}")
220
```
221
222
## Practical Usage Patterns
223
224
### Dynamic Path Construction
225
226
```python
227
from google.shopping import merchant_conversions_v1
228
229
def manage_conversion_source(account_id: str, source_id: str = None):
230
client = merchant_conversions_v1.ConversionSourcesServiceClient()
231
232
if source_id:
233
# Work with existing source
234
path = client.conversion_source_path(account_id, source_id)
235
request = merchant_conversions_v1.GetConversionSourceRequest(name=path)
236
source = client.get_conversion_source(request=request)
237
return source
238
else:
239
# List all sources for account
240
parent = f"accounts/{account_id}"
241
request = merchant_conversions_v1.ListConversionSourcesRequest(parent=parent)
242
response = client.list_conversion_sources(request=request)
243
return response.conversion_sources
244
245
# Usage
246
sources = manage_conversion_source("123456789") # List all
247
specific_source = manage_conversion_source("123456789", "abc123") # Get specific
248
```
249
250
### Path Extraction from Resources
251
252
```python
253
from google.shopping import merchant_conversions_v1
254
255
def extract_ids_from_source(conversion_source: merchant_conversions_v1.ConversionSource):
256
"""Extract account and source IDs from a ConversionSource object."""
257
client = merchant_conversions_v1.ConversionSourcesServiceClient()
258
259
# Parse the resource name
260
parsed = client.parse_conversion_source_path(conversion_source.name)
261
262
return {
263
'account_id': parsed['account'],
264
'source_id': parsed['conversion_source'],
265
'full_path': conversion_source.name
266
}
267
268
# Usage with API response
269
client = merchant_conversions_v1.ConversionSourcesServiceClient()
270
request = merchant_conversions_v1.ListConversionSourcesRequest(parent="accounts/123456789")
271
response = client.list_conversion_sources(request=request)
272
273
for source in response.conversion_sources:
274
ids = extract_ids_from_source(source)
275
print(f"Account: {ids['account_id']}, Source: {ids['source_id']}")
276
```
277
278
### Batch Operations with Path Helpers
279
280
```python
281
from google.shopping import merchant_conversions_v1
282
283
def batch_delete_sources(account_id: str, source_ids: list):
284
"""Delete multiple conversion sources."""
285
client = merchant_conversions_v1.ConversionSourcesServiceClient()
286
results = []
287
288
for source_id in source_ids:
289
try:
290
# Construct path for each source
291
path = client.conversion_source_path(account_id, source_id)
292
request = merchant_conversions_v1.DeleteConversionSourceRequest(name=path)
293
client.delete_conversion_source(request=request)
294
results.append({'source_id': source_id, 'status': 'deleted'})
295
except Exception as e:
296
results.append({'source_id': source_id, 'status': 'error', 'error': str(e)})
297
298
return results
299
300
# Usage
301
results = batch_delete_sources("123456789", ["source1", "source2", "source3"])
302
for result in results:
303
print(f"Source {result['source_id']}: {result['status']}")
304
```