redis-cloud 0.9.5

Redis Cloud REST API client library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use redis_cloud::{CloudClient, FixedSubscriptionsHandler};
use serde_json::json;
use wiremock::matchers::{header, method, path, query_param};
use wiremock::{Mock, MockServer, ResponseTemplate};

#[tokio::test]
async fn test_get_all_fixed_subscriptions_plans() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/fixed/plans"))
        .and(header("x-api-key", "test-key"))
        .and(header("x-api-secret-key", "test-secret"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "plans": [
                {
                    "id": "plan-1",
                    "name": "Cache 250MB",
                    "size": 250,
                    "sizeMeasurementUnit": "MB",
                    "price": 10,
                    "region": "us-east-1"
                },
                {
                    "id": "plan-2",
                    "name": "Cache 1GB",
                    "size": 1,
                    "sizeMeasurementUnit": "GB",
                    "price": 25,
                    "region": "us-west-2"
                }
            ]
        })))
        .mount(&mock_server)
        .await;

    let client = CloudClient::builder()
        .api_key("test-key".to_string())
        .api_secret("test-secret".to_string())
        .base_url(mock_server.uri())
        .build()
        .unwrap();

    let handler = FixedSubscriptionsHandler::new(client);
    let result = handler.list_plans(None, None).await.unwrap();

    // Verify the response was successfully parsed
    // Note: plans data would need typed fields added to FixedSubscriptionsPlans to be accessible
    assert!(result.links.is_none()); // No links in the mock response
}

#[tokio::test]
async fn test_get_fixed_subscriptions_plans_by_subscription_id() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/fixed/plans/subscriptions/123"))
        .and(header("x-api-key", "test-key"))
        .and(header("x-api-secret-key", "test-secret"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "subscription": {
                "subscriptionId": 123,
                "planId": "plan-1"
            },
            "plans": [
                {
                    "id": "plan-1",
                    "name": "Current Plan",
                    "size": 500,
                    "price": 15
                }
            ]
        })))
        .mount(&mock_server)
        .await;

    let client = CloudClient::builder()
        .api_key("test-key".to_string())
        .api_secret("test-secret".to_string())
        .base_url(mock_server.uri())
        .build()
        .unwrap();

    let handler = FixedSubscriptionsHandler::new(client);
    let result = handler.get_plans_by_subscription_id(123).await.unwrap();

    // Verify the response was successfully parsed
    // Note: subscription and plans data would need typed fields to be accessible
    assert!(result.links.is_none()); // No links in the mock response
}

#[tokio::test]
async fn test_get_fixed_subscriptions_plan_by_id() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/fixed/plans/123"))
        .and(header("x-api-key", "test-key"))
        .and(header("x-api-secret-key", "test-secret"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": 123,
            "name": "Cache 2GB",
            "size": 2,
            "sizeMeasurementUnit": "GB",
            "price": 50,
            "region": "eu-west-1"
        })))
        .mount(&mock_server)
        .await;

    let client = CloudClient::builder()
        .api_key("test-key".to_string())
        .api_secret("test-secret".to_string())
        .base_url(mock_server.uri())
        .build()
        .unwrap();

    let handler = FixedSubscriptionsHandler::new(client);
    let result = handler.get_plan_by_id(123).await.unwrap();

    assert_eq!(result.id, Some(123));
    assert_eq!(result.name, Some("Cache 2GB".to_string()));
}

#[tokio::test]
async fn test_get_redis_versions() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/fixed/redis-versions"))
        .and(query_param("subscriptionId", "123"))
        .and(header("x-api-key", "test-key"))
        .and(header("x-api-secret-key", "test-secret"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "redisVersions": [
                {
                    "version": "7.2",
                    "isDefault": true
                },
                {
                    "version": "7.0",
                    "isDefault": false
                },
                {
                    "version": "6.2",
                    "isDefault": false
                }
            ]
        })))
        .mount(&mock_server)
        .await;

    let client = CloudClient::builder()
        .api_key("test-key".to_string())
        .api_secret("test-secret".to_string())
        .base_url(mock_server.uri())
        .build()
        .unwrap();

    let handler = FixedSubscriptionsHandler::new(client);
    let result = handler.get_redis_versions(123).await.unwrap();

    assert!(result.redis_versions.is_some());
    let versions = result.redis_versions.unwrap();
    assert_eq!(versions.len(), 3);
}

#[tokio::test]
async fn test_get_all_subscriptions() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/fixed/subscriptions"))
        .and(header("x-api-key", "test-key"))
        .and(header("x-api-secret-key", "test-secret"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "accountId": 456,
            "subscriptions": [
                {
                    "id": 123,
                    "name": "Production Fixed",
                    "status": "active",
                    "paymentMethod": "credit-card"
                },
                {
                    "id": 124,
                    "name": "Staging Fixed",
                    "status": "active",
                    "paymentMethod": "marketplace"
                }
            ]
        })))
        .mount(&mock_server)
        .await;

    let client = CloudClient::builder()
        .api_key("test-key".to_string())
        .api_secret("test-secret".to_string())
        .base_url(mock_server.uri())
        .build()
        .unwrap();

    let handler = FixedSubscriptionsHandler::new(client);
    let result = handler.list().await.unwrap();

    assert_eq!(result.account_id, Some(456));
    // Note: subscriptions data would need a typed field to be accessible
}

#[tokio::test]
async fn test_create_subscription() {
    let mock_server = MockServer::start().await;

    Mock::given(method("POST"))
        .and(path("/fixed/subscriptions"))
        .and(header("x-api-key", "test-key"))
        .and(header("x-api-secret-key", "test-secret"))
        .respond_with(ResponseTemplate::new(202).set_body_json(json!({
            "taskId": "task-create-fixed-sub",
            "commandType": "CREATE_FIXED_SUBSCRIPTION",
            "status": "processing",
            "description": "Creating fixed subscription",
            "timestamp": "2024-01-01T00:00:00Z",
            "response": {
                "resourceId": 125
            }
        })))
        .mount(&mock_server)
        .await;

    let client = CloudClient::builder()
        .api_key("test-key".to_string())
        .api_secret("test-secret".to_string())
        .base_url(mock_server.uri())
        .build()
        .unwrap();

    let handler = FixedSubscriptionsHandler::new(client);
    let request = redis_cloud::fixed_subscriptions::FixedSubscriptionCreateRequest {
        name: "New Fixed Subscription".to_string(),
        plan_id: 123,
        payment_method: Some("credit-card".to_string()),
        payment_method_id: Some(1001),
        command_type: None,
    };

    let result = handler.create(&request).await.unwrap();
    assert_eq!(result.task_id, Some("task-create-fixed-sub".to_string()));
}

#[tokio::test]
async fn test_delete_subscription_by_id() {
    let mock_server = MockServer::start().await;

    Mock::given(method("DELETE"))
        .and(path("/fixed/subscriptions/123"))
        .and(header("x-api-key", "test-key"))
        .and(header("x-api-secret-key", "test-secret"))
        .respond_with(ResponseTemplate::new(202).set_body_json(json!({
            "taskId": "task-delete-fixed-sub",
            "commandType": "DELETE_FIXED_SUBSCRIPTION",
            "status": "processing",
            "description": "Deleting fixed subscription"
        })))
        .mount(&mock_server)
        .await;

    let client = CloudClient::builder()
        .api_key("test-key".to_string())
        .api_secret("test-secret".to_string())
        .base_url(mock_server.uri())
        .build()
        .unwrap();

    let handler = FixedSubscriptionsHandler::new(client);
    let result = handler.delete_by_id(123).await.unwrap();

    assert_eq!(result.task_id, Some("task-delete-fixed-sub".to_string()));
}

#[tokio::test]
async fn test_get_subscription_by_id() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/fixed/subscriptions/123"))
        .and(header("x-api-key", "test-key"))
        .and(header("x-api-secret-key", "test-secret"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "id": 123,
            "name": "Production Fixed",
            "status": "active",
            "paymentMethod": "credit-card",
            "numberOfDatabases": 5,
            "planId": 1,
            "createdDate": "2024-01-01T00:00:00Z"
        })))
        .mount(&mock_server)
        .await;

    let client = CloudClient::builder()
        .api_key("test-key".to_string())
        .api_secret("test-secret".to_string())
        .base_url(mock_server.uri())
        .build()
        .unwrap();

    let handler = FixedSubscriptionsHandler::new(client);
    let result = handler.get_by_id(123).await.unwrap();

    assert_eq!(result.id, Some(123));
    assert_eq!(result.name, Some("Production Fixed".to_string()));
}

#[tokio::test]
async fn test_update_subscription() {
    let mock_server = MockServer::start().await;

    Mock::given(method("PUT"))
        .and(path("/fixed/subscriptions/123"))
        .and(header("x-api-key", "test-key"))
        .and(header("x-api-secret-key", "test-secret"))
        .respond_with(ResponseTemplate::new(202).set_body_json(json!({
            "taskId": "task-update-fixed-sub",
            "commandType": "UPDATE_FIXED_SUBSCRIPTION",
            "status": "processing",
            "description": "Updating fixed subscription"
        })))
        .mount(&mock_server)
        .await;

    let client = CloudClient::builder()
        .api_key("test-key".to_string())
        .api_secret("test-secret".to_string())
        .base_url(mock_server.uri())
        .build()
        .unwrap();

    let handler = FixedSubscriptionsHandler::new(client);
    let request = redis_cloud::fixed_subscriptions::FixedSubscriptionUpdateRequest {
        name: Some("Updated Fixed Subscription".to_string()),
        plan_id: Some(124),
        payment_method: Some("credit-card".to_string()),
        payment_method_id: Some(1002),
        subscription_id: None,
        command_type: None,
    };

    let result = handler.update(123, &request).await.unwrap();
    assert_eq!(result.task_id, Some("task-update-fixed-sub".to_string()));
}

#[tokio::test]
async fn test_error_handling_401() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/fixed/subscriptions"))
        .respond_with(ResponseTemplate::new(401).set_body_json(json!({
            "error": "Invalid API credentials"
        })))
        .mount(&mock_server)
        .await;

    let client = CloudClient::builder()
        .api_key("wrong-key".to_string())
        .api_secret("wrong-secret".to_string())
        .base_url(mock_server.uri())
        .build()
        .unwrap();

    let handler = FixedSubscriptionsHandler::new(client);
    let result = handler.list().await;

    assert!(result.is_err());
    match result {
        Err(redis_cloud::CloudError::AuthenticationFailed { .. }) => {}
        _ => panic!("Expected AuthenticationFailed error"),
    }
}

#[tokio::test]
async fn test_error_handling_404() {
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/fixed/subscriptions/999"))
        .and(header("x-api-key", "test-key"))
        .and(header("x-api-secret-key", "test-secret"))
        .respond_with(ResponseTemplate::new(404).set_body_json(json!({
            "error": "Subscription not found"
        })))
        .mount(&mock_server)
        .await;

    let client = CloudClient::builder()
        .api_key("test-key".to_string())
        .api_secret("test-secret".to_string())
        .base_url(mock_server.uri())
        .build()
        .unwrap();

    let handler = FixedSubscriptionsHandler::new(client);
    let result = handler.get_by_id(999).await;

    assert!(result.is_err());
    if let Err(redis_cloud::CloudError::NotFound { message }) = result {
        assert!(message.contains("not found") || message.contains("404"));
    } else {
        panic!("Expected NotFound error");
    }
}

#[tokio::test]
async fn test_error_handling_500() {
    let mock_server = MockServer::start().await;

    Mock::given(method("POST"))
        .and(path("/fixed/subscriptions"))
        .and(header("x-api-key", "test-key"))
        .and(header("x-api-secret-key", "test-secret"))
        .respond_with(ResponseTemplate::new(500).set_body_json(json!({
            "error": "Internal server error"
        })))
        .mount(&mock_server)
        .await;

    let client = CloudClient::builder()
        .api_key("test-key".to_string())
        .api_secret("test-secret".to_string())
        .base_url(mock_server.uri())
        .build()
        .unwrap();

    let handler = FixedSubscriptionsHandler::new(client);
    let request = redis_cloud::fixed_subscriptions::FixedSubscriptionCreateRequest {
        name: "Test Subscription".to_string(),
        plan_id: 100,
        payment_method: Some("credit-card".to_string()),
        payment_method_id: None,
        command_type: None,
    };

    let result = handler.create(&request).await;

    assert!(result.is_err());
    match result {
        Err(redis_cloud::CloudError::InternalServerError { .. }) => {}
        _ => panic!("Expected InternalServerError error"),
    }
}