0
# Path Templates
1
2
URI template expansion and validation for Google API resource paths, supporting variable substitution and path parameter extraction for RESTful API endpoints.
3
4
## Capabilities
5
6
### Template Expansion
7
8
```python { .api }
9
def expand(tmpl, *args, **kwargs):
10
"""
11
Expand a path template with variables.
12
13
Args:
14
tmpl (str): Path template with {variable} placeholders
15
*args: Positional arguments for template variables
16
**kwargs: Named arguments for template variables
17
18
Returns:
19
str: Expanded path with variables substituted
20
"""
21
22
def validate(tmpl, path):
23
"""
24
Validate that a path matches a template pattern.
25
26
Args:
27
tmpl (str): Path template pattern
28
path (str): Path to validate
29
30
Returns:
31
dict: Extracted variables if path matches, raises ValueError if not
32
"""
33
```
34
35
### Field Manipulation
36
37
```python { .api }
38
def get_field(request, field):
39
"""
40
Get field value from request dict or protobuf Message.
41
42
Args:
43
request: Request dict or protobuf Message
44
field (str): Field path (e.g., "user.name" for nested fields)
45
46
Returns:
47
Any: Field value
48
"""
49
50
def delete_field(request, field):
51
"""
52
Delete field from request dict or protobuf Message.
53
54
Args:
55
request: Request dict or protobuf Message
56
field (str): Field path to delete
57
"""
58
59
def transcode(http_options, message=None, **request_kwargs):
60
"""
61
Transcode gRPC request to HTTP format.
62
63
Args:
64
http_options: HTTP binding configuration
65
message: gRPC request message
66
**request_kwargs: Additional request parameters
67
68
Returns:
69
dict: HTTP request components (method, uri, body)
70
"""
71
```
72
73
## Usage Examples
74
75
### Basic Path Template Usage
76
77
```python
78
from google.api_core import path_template
79
80
# Expand template with named parameters
81
template = "projects/{project}/instances/{instance}/databases/{database}"
82
path = path_template.expand(template,
83
project="my-project",
84
instance="my-instance",
85
database="my-db")
86
print(path) # "projects/my-project/instances/my-instance/databases/my-db"
87
88
# Validate and extract parameters
89
extracted = path_template.validate(template, path)
90
print(extracted) # {"project": "my-project", "instance": "my-instance", "database": "my-db"}
91
```