0
# Redis Client Management
1
2
Core client functionality for authentication, configuration, and connection management to the Google Cloud Redis API.
3
4
## Capabilities
5
6
### Client Initialization
7
8
The CloudRedisClient provides synchronous access to the Cloud Redis API with automatic authentication and connection management.
9
10
```python { .api }
11
class CloudRedisClient:
12
def __init__(
13
self,
14
*,
15
credentials: Optional[ga_credentials.Credentials] = None,
16
transport: Optional[Union[str, CloudRedisTransport, Callable]] = None,
17
client_options: Optional[Union[ClientOptions, dict]] = None,
18
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
19
) -> None:
20
"""
21
Initialize the Cloud Redis client.
22
23
Args:
24
credentials: The authorization credentials to attach to requests
25
transport: The transport to use for communication ('grpc', 'rest', or transport instance)
26
client_options: Client configuration options (endpoint, scopes, etc.)
27
client_info: Client information for user agent strings
28
29
Raises:
30
google.auth.exceptions.MutualTLSChannelError: If mutual TLS transport creation fails
31
"""
32
```
33
34
### Async Client Initialization
35
36
The CloudRedisAsyncClient provides asynchronous access with the same interface as the synchronous client.
37
38
```python { .api }
39
class CloudRedisAsyncClient:
40
def __init__(
41
self,
42
*,
43
credentials: Optional[ga_credentials.Credentials] = None,
44
transport: Optional[Union[str, CloudRedisTransport, Callable]] = None,
45
client_options: Optional[Union[ClientOptions, dict]] = None,
46
client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO,
47
) -> None:
48
"""
49
Initialize the async Cloud Redis client.
50
51
Args:
52
credentials: The authorization credentials to attach to requests
53
transport: The transport to use ('grpc_asyncio', 'rest', or transport instance)
54
client_options: Client configuration options
55
client_info: Client information for user agent strings
56
"""
57
```
58
59
### Client Factory Methods
60
61
Create clients from service account credentials stored in files or dictionaries.
62
63
```python { .api }
64
@classmethod
65
def from_service_account_info(
66
cls,
67
info: dict,
68
**kwargs
69
) -> CloudRedisClient:
70
"""
71
Create a client from service account info dictionary.
72
73
Args:
74
info: Service account info in Google format
75
**kwargs: Additional arguments for client initialization
76
77
Returns:
78
CloudRedisClient: The constructed client
79
"""
80
81
@classmethod
82
def from_service_account_file(
83
cls,
84
filename: str,
85
**kwargs
86
) -> CloudRedisClient:
87
"""
88
Create a client from a service account json file.
89
90
Args:
91
filename: Path to the service account json file
92
**kwargs: Additional arguments for client initialization
93
94
Returns:
95
CloudRedisClient: The constructed client
96
"""
97
```
98
99
### Transport Configuration
100
101
Access and configure the underlying transport layer for advanced use cases.
102
103
```python { .api }
104
@classmethod
105
def get_transport_class(
106
cls,
107
label: Optional[str] = None,
108
) -> Type[CloudRedisTransport]:
109
"""
110
Return an appropriate transport class.
111
112
Args:
113
label: The name of the desired transport ('grpc', 'rest')
114
115
Returns:
116
Type[CloudRedisTransport]: The transport class
117
"""
118
119
@property
120
def transport(self) -> CloudRedisTransport:
121
"""
122
Access the transport used by the client.
123
124
Returns:
125
CloudRedisTransport: The transport instance
126
"""
127
128
@property
129
def api_endpoint(self) -> str:
130
"""
131
The API endpoint used by the client.
132
133
Returns:
134
str: The API endpoint URL
135
"""
136
137
@property
138
def universe_domain(self) -> str:
139
"""
140
The universe domain used by the client.
141
142
Returns:
143
str: The universe domain
144
"""
145
```
146
147
### Resource Path Helpers
148
149
Utility methods for constructing and parsing Google Cloud resource paths.
150
151
```python { .api }
152
@staticmethod
153
def instance_path(project: str, location: str, instance: str) -> str:
154
"""
155
Return a fully-qualified instance string.
156
157
Args:
158
project: Project ID
159
location: Location/region
160
instance: Instance ID
161
162
Returns:
163
str: The instance resource path
164
"""
165
166
@staticmethod
167
def parse_instance_path(path: str) -> Dict[str, str]:
168
"""
169
Parse an instance path into its component segments.
170
171
Args:
172
path: Instance resource path
173
174
Returns:
175
Dict[str, str]: Dictionary with 'project', 'location', 'instance' keys
176
"""
177
178
@staticmethod
179
def common_billing_account_path(billing_account: str) -> str:
180
"""Return a billing account string."""
181
182
@staticmethod
183
def common_folder_path(folder: str) -> str:
184
"""Return a folder string."""
185
186
@staticmethod
187
def common_organization_path(organization: str) -> str:
188
"""Return an organization string."""
189
190
@staticmethod
191
def common_project_path(project: str) -> str:
192
"""Return a project string."""
193
194
@staticmethod
195
def common_location_path(project: str, location: str) -> str:
196
"""Return a location string."""
197
```
198
199
### Context Management
200
201
Both client classes support context manager protocol for automatic cleanup.
202
203
```python { .api }
204
def __enter__(self) -> CloudRedisClient:
205
"""Enter context manager."""
206
207
def __exit__(self, type, value, traceback) -> None:
208
"""Exit context manager and cleanup resources."""
209
```
210
211
## Usage Examples
212
213
### Basic Client Setup
214
215
```python
216
from google.cloud.redis import CloudRedisClient
217
218
# Use default credentials (ADC)
219
client = CloudRedisClient()
220
221
# Use service account file
222
client = CloudRedisClient.from_service_account_file(
223
"path/to/service-account.json"
224
)
225
226
# Use service account info
227
service_account_info = {
228
"type": "service_account",
229
"project_id": "your-project",
230
# ... other service account fields
231
}
232
client = CloudRedisClient.from_service_account_info(service_account_info)
233
```
234
235
### Custom Transport Configuration
236
237
```python
238
from google.cloud.redis import CloudRedisClient
239
from google.cloud.redis_v1.services.cloud_redis.transports import CloudRedisRestTransport
240
241
# Use REST transport instead of gRPC
242
transport = CloudRedisRestTransport()
243
client = CloudRedisClient(transport=transport)
244
245
# Configure transport with custom options
246
from google.api_core import client_options
247
248
options = client_options.ClientOptions(
249
api_endpoint="https://redis.googleapis.com",
250
scopes=["https://www.googleapis.com/auth/cloud-platform"]
251
)
252
client = CloudRedisClient(client_options=options)
253
```
254
255
### Async Client Usage
256
257
```python
258
import asyncio
259
from google.cloud.redis import CloudRedisAsyncClient
260
261
async def main():
262
async with CloudRedisAsyncClient() as client:
263
# Use async client methods
264
parent = "projects/my-project/locations/us-central1"
265
instances = await client.list_instances(parent=parent)
266
267
async for instance in instances:
268
print(f"Instance: {instance.name}")
269
270
asyncio.run(main())
271
```
272
273
### Resource Path Construction
274
275
```python
276
from google.cloud.redis import CloudRedisClient
277
278
# Build resource paths
279
instance_path = CloudRedisClient.instance_path(
280
project="my-project",
281
location="us-central1",
282
instance="my-redis-instance"
283
)
284
print(instance_path) # "projects/my-project/locations/us-central1/instances/my-redis-instance"
285
286
# Parse resource paths
287
path_components = CloudRedisClient.parse_instance_path(instance_path)
288
print(path_components) # {'project': 'my-project', 'location': 'us-central1', 'instance': 'my-redis-instance'}
289
```