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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
//! Redis Cloud API client core implementation
//!
//! This module contains the core HTTP client for interacting with the Redis Cloud REST API.
//! It provides authentication handling, request/response processing, and error management.
//!
//! The client is designed around a builder pattern for flexible configuration and supports
//! both typed and untyped API interactions.

use crate::{CloudError as RestError, Result};
use reqwest::Client;
use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT};
use serde::Serialize;
use std::sync::Arc;
use tracing::{debug, instrument, trace};

/// Default user agent for the Redis Cloud client
const DEFAULT_USER_AGENT: &str = concat!("redis-cloud/", env!("CARGO_PKG_VERSION"));

/// Builder for constructing a `CloudClient` with custom configuration
///
/// Provides a fluent interface for configuring API credentials, base URL, timeouts,
/// and other client settings before creating the final `CloudClient` instance.
///
/// # Examples
///
/// ```rust,no_run
/// use redis_cloud::CloudClient;
///
/// // Basic configuration
/// let client = CloudClient::builder()
///     .api_key("your-api-key")
///     .api_secret("your-api-secret")
///     .build()?;
///
/// // Advanced configuration
/// let client = CloudClient::builder()
///     .api_key("your-api-key")
///     .api_secret("your-api-secret")
///     .base_url("https://api.redislabs.com/v1".to_string())
///     .timeout(std::time::Duration::from_secs(120))
///     .build()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone)]
pub struct CloudClientBuilder {
    api_key: Option<String>,
    api_secret: Option<String>,
    base_url: String,
    timeout: std::time::Duration,
    user_agent: String,
}

impl Default for CloudClientBuilder {
    fn default() -> Self {
        Self {
            api_key: None,
            api_secret: None,
            base_url: "https://api.redislabs.com/v1".to_string(),
            timeout: std::time::Duration::from_secs(30),
            user_agent: DEFAULT_USER_AGENT.to_string(),
        }
    }
}

impl CloudClientBuilder {
    /// Create a new builder
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the API key
    #[must_use]
    pub fn api_key(mut self, key: impl Into<String>) -> Self {
        self.api_key = Some(key.into());
        self
    }

    /// Set the API secret
    #[must_use]
    pub fn api_secret(mut self, secret: impl Into<String>) -> Self {
        self.api_secret = Some(secret.into());
        self
    }

    /// Set the base URL
    #[must_use]
    pub fn base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = url.into();
        self
    }

    /// Set the timeout
    #[must_use]
    pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Set the user agent string for HTTP requests
    ///
    /// The default user agent is `redis-cloud/{version}`.
    /// This can be overridden to identify specific clients, for example:
    /// `redisctl/1.2.3` or `my-app/1.0.0`.
    #[must_use]
    pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
        self.user_agent = user_agent.into();
        self
    }

    /// Build the client
    pub fn build(self) -> Result<CloudClient> {
        let api_key = self
            .api_key
            .ok_or_else(|| RestError::ConnectionError("API key is required".to_string()))?;
        let api_secret = self
            .api_secret
            .ok_or_else(|| RestError::ConnectionError("API secret is required".to_string()))?;

        let mut default_headers = HeaderMap::new();
        default_headers.insert(
            USER_AGENT,
            HeaderValue::from_str(&self.user_agent)
                .map_err(|e| RestError::ConnectionError(format!("Invalid user agent: {e}")))?,
        );

        let client = Client::builder()
            .timeout(self.timeout)
            .default_headers(default_headers)
            .build()
            .map_err(|e| RestError::ConnectionError(e.to_string()))?;

        Ok(CloudClient {
            api_key,
            api_secret,
            base_url: self.base_url,
            timeout: self.timeout,
            client: Arc::new(client),
        })
    }
}

/// Redis Cloud API client
#[derive(Clone)]
pub struct CloudClient {
    pub(crate) api_key: String,
    pub(crate) api_secret: String,
    pub(crate) base_url: String,
    pub(crate) timeout: std::time::Duration,
    pub(crate) client: Arc<Client>,
}

impl CloudClient {
    /// Create a new builder for the client
    #[must_use]
    pub fn builder() -> CloudClientBuilder {
        CloudClientBuilder::new()
    }

    /// Get the configured request timeout
    ///
    /// Returns the timeout duration that was set when building the client.
    /// This timeout is applied to all HTTP requests made by this client.
    #[must_use]
    pub fn timeout(&self) -> std::time::Duration {
        self.timeout
    }

    // ========================================================================
    // Fluent API - Handler accessors
    // ========================================================================

    /// Get an account handler for account management operations
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use redis_cloud::CloudClient;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = CloudClient::builder()
    ///     .api_key("key")
    ///     .api_secret("secret")
    ///     .build()?;
    ///
    /// let account = client.account().get_current_account().await?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn account(&self) -> crate::AccountHandler {
        crate::AccountHandler::new(self.clone())
    }

    /// Get a subscription handler for Pro subscription operations
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use redis_cloud::CloudClient;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = CloudClient::builder()
    ///     .api_key("key")
    ///     .api_secret("secret")
    ///     .build()?;
    ///
    /// let subscriptions = client.subscriptions().get_all_subscriptions().await?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn subscriptions(&self) -> crate::SubscriptionHandler {
        crate::SubscriptionHandler::new(self.clone())
    }

    /// Get a database handler for Pro database operations
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use redis_cloud::CloudClient;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = CloudClient::builder()
    ///     .api_key("key")
    ///     .api_secret("secret")
    ///     .build()?;
    ///
    /// let databases = client.databases().get_subscription_databases(123, None, None).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn databases(&self) -> crate::DatabaseHandler {
        crate::DatabaseHandler::new(self.clone())
    }

    /// Get a fixed subscription handler for Essentials subscription operations
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use redis_cloud::CloudClient;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = CloudClient::builder()
    ///     .api_key("key")
    ///     .api_secret("secret")
    ///     .build()?;
    ///
    /// let subscriptions = client.fixed_subscriptions().list().await?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn fixed_subscriptions(&self) -> crate::FixedSubscriptionHandler {
        crate::FixedSubscriptionHandler::new(self.clone())
    }

    /// Get a fixed database handler for Essentials database operations
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use redis_cloud::CloudClient;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = CloudClient::builder()
    ///     .api_key("key")
    ///     .api_secret("secret")
    ///     .build()?;
    ///
    /// let databases = client.fixed_databases().list(123, None, None).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn fixed_databases(&self) -> crate::FixedDatabaseHandler {
        crate::FixedDatabaseHandler::new(self.clone())
    }

    /// Get an ACL handler for access control operations
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use redis_cloud::CloudClient;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = CloudClient::builder()
    ///     .api_key("key")
    ///     .api_secret("secret")
    ///     .build()?;
    ///
    /// let users = client.acl().get_all_acl_users().await?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn acl(&self) -> crate::AclHandler {
        crate::AclHandler::new(self.clone())
    }

    /// Get a users handler for user management operations
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use redis_cloud::CloudClient;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = CloudClient::builder()
    ///     .api_key("key")
    ///     .api_secret("secret")
    ///     .build()?;
    ///
    /// let users = client.users().get_all_users().await?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn users(&self) -> crate::UserHandler {
        crate::UserHandler::new(self.clone())
    }

    /// Get a tasks handler for async operation tracking
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use redis_cloud::CloudClient;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = CloudClient::builder()
    ///     .api_key("key")
    ///     .api_secret("secret")
    ///     .build()?;
    ///
    /// let tasks = client.tasks().get_all_tasks().await?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn tasks(&self) -> crate::TaskHandler {
        crate::TaskHandler::new(self.clone())
    }

    /// Get a cloud accounts handler for cloud provider integration
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use redis_cloud::CloudClient;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = CloudClient::builder()
    ///     .api_key("key")
    ///     .api_secret("secret")
    ///     .build()?;
    ///
    /// let accounts = client.cloud_accounts().get_cloud_accounts().await?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn cloud_accounts(&self) -> crate::CloudAccountHandler {
        crate::CloudAccountHandler::new(self.clone())
    }

    /// Get a VPC peering handler for VPC peering operations
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use redis_cloud::CloudClient;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = CloudClient::builder()
    ///     .api_key("key")
    ///     .api_secret("secret")
    ///     .build()?;
    ///
    /// let peering = client.vpc_peering().get(123).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn vpc_peering(&self) -> crate::VpcPeeringHandler {
        crate::VpcPeeringHandler::new(self.clone())
    }

    /// Get a transit gateway handler for AWS Transit Gateway operations
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use redis_cloud::CloudClient;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = CloudClient::builder()
    ///     .api_key("key")
    ///     .api_secret("secret")
    ///     .build()?;
    ///
    /// let attachments = client.transit_gateway().get_attachments(123).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn transit_gateway(&self) -> crate::TransitGatewayHandler {
        crate::TransitGatewayHandler::new(self.clone())
    }

    /// Get a Private Service Connect handler for GCP PSC operations
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use redis_cloud::CloudClient;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = CloudClient::builder()
    ///     .api_key("key")
    ///     .api_secret("secret")
    ///     .build()?;
    ///
    /// let service = client.psc().get_service(123).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn psc(&self) -> crate::PscHandler {
        crate::PscHandler::new(self.clone())
    }

    /// Get a `PrivateLink` handler for AWS `PrivateLink` operations
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use redis_cloud::CloudClient;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = CloudClient::builder()
    ///     .api_key("key")
    ///     .api_secret("secret")
    ///     .build()?;
    ///
    /// let config = client.private_link().get(123).await?;
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn private_link(&self) -> crate::PrivateLinkHandler {
        crate::PrivateLinkHandler::new(self.clone())
    }

    /// Get a cost report handler for generating cost reports
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use redis_cloud::CloudClient;
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = CloudClient::builder()
    ///     .api_key("key")
    ///     .api_secret("secret")
    ///     .build()?;
    ///
    /// let handler = client.cost_reports();
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn cost_reports(&self) -> crate::CostReportHandler {
        crate::CostReportHandler::new(self.clone())
    }

    /// Normalize URL path concatenation to avoid double slashes
    fn normalize_url(&self, path: &str) -> String {
        let base = self.base_url.trim_end_matches('/');
        let path = path.trim_start_matches('/');
        format!("{base}/{path}")
    }

    /// Convert HTTP status code and response text to appropriate error
    ///
    /// This is a helper to avoid duplicating the error handling pattern
    /// across multiple methods.
    fn status_to_error(status: reqwest::StatusCode, text: String) -> RestError {
        match status.as_u16() {
            400 => RestError::BadRequest { message: text },
            401 => RestError::AuthenticationFailed { message: text },
            403 => RestError::Forbidden { message: text },
            404 => RestError::NotFound { message: text },
            412 => RestError::PreconditionFailed,
            429 => RestError::RateLimited { message: text },
            500 => RestError::InternalServerError { message: text },
            503 => RestError::ServiceUnavailable { message: text },
            _ => RestError::ApiError {
                code: status.as_u16(),
                message: text,
            },
        }
    }

    /// Make a GET request with API key authentication
    #[instrument(skip(self), fields(method = "GET"))]
    pub async fn get<T: serde::de::DeserializeOwned>(&self, path: &str) -> Result<T> {
        let url = self.normalize_url(path);
        debug!("GET {}", url);

        // Redis Cloud API uses these headers for authentication
        let response = self
            .client
            .get(&url)
            .header("x-api-key", &self.api_key)
            .header("x-api-secret-key", &self.api_secret)
            .send()
            .await?;

        trace!("Response status: {}", response.status());
        self.handle_response(response).await
    }

    /// Make a POST request
    #[instrument(skip(self, body), fields(method = "POST"))]
    pub async fn post<B: Serialize, T: serde::de::DeserializeOwned>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T> {
        let url = self.normalize_url(path);
        debug!("POST {}", url);
        trace!("Request body: {:?}", serde_json::to_value(body).ok());

        // Same backwards header naming as GET
        let response = self
            .client
            .post(&url)
            .header("x-api-key", &self.api_key)
            .header("x-api-secret-key", &self.api_secret)
            .json(body)
            .send()
            .await?;

        trace!("Response status: {}", response.status());
        self.handle_response(response).await
    }

    /// Make a PUT request
    #[instrument(skip(self, body), fields(method = "PUT"))]
    pub async fn put<B: Serialize, T: serde::de::DeserializeOwned>(
        &self,
        path: &str,
        body: &B,
    ) -> Result<T> {
        let url = self.normalize_url(path);
        debug!("PUT {}", url);
        trace!("Request body: {:?}", serde_json::to_value(body).ok());

        // Same backwards header naming as GET
        let response = self
            .client
            .put(&url)
            .header("x-api-key", &self.api_key)
            .header("x-api-secret-key", &self.api_secret)
            .json(body)
            .send()
            .await?;

        trace!("Response status: {}", response.status());
        self.handle_response(response).await
    }

    /// Make a DELETE request
    #[instrument(skip(self), fields(method = "DELETE"))]
    pub async fn delete(&self, path: &str) -> Result<()> {
        let url = self.normalize_url(path);
        debug!("DELETE {}", url);

        // Same backwards header naming as GET
        let response = self
            .client
            .delete(&url)
            .header("x-api-key", &self.api_key)
            .header("x-api-secret-key", &self.api_secret)
            .send()
            .await?;

        trace!("Response status: {}", response.status());
        if response.status().is_success() {
            Ok(())
        } else {
            let status = response.status();
            let text = response
                .text()
                .await
                .unwrap_or_else(|e| format!("(failed to read response body: {e})"));
            Err(Self::status_to_error(status, text))
        }
    }

    /// Execute raw GET request returning JSON Value
    #[instrument(skip(self), fields(method = "GET"))]
    pub async fn get_raw(&self, path: &str) -> Result<serde_json::Value> {
        self.get(path).await
    }

    /// Execute GET request returning raw bytes
    ///
    /// Useful for downloading binary content like cost reports or other files.
    #[instrument(skip(self), fields(method = "GET"))]
    pub async fn get_bytes(&self, path: &str) -> Result<Vec<u8>> {
        let url = self.normalize_url(path);
        debug!("GET {} (bytes)", url);

        let response = self
            .client
            .get(&url)
            .header("x-api-key", &self.api_key)
            .header("x-api-secret-key", &self.api_secret)
            .send()
            .await?;

        trace!("Response status: {}", response.status());
        let status = response.status();

        if status.is_success() {
            response
                .bytes()
                .await
                .map(|b| b.to_vec())
                .map_err(|e| RestError::ConnectionError(format!("Failed to read response: {e}")))
        } else {
            let text = response
                .text()
                .await
                .unwrap_or_else(|e| format!("(failed to read response body: {e})"));
            Err(Self::status_to_error(status, text))
        }
    }

    /// Execute raw POST request with JSON body
    #[instrument(skip(self, body), fields(method = "POST"))]
    pub async fn post_raw(&self, path: &str, body: serde_json::Value) -> Result<serde_json::Value> {
        self.post(path, &body).await
    }

    /// Execute raw PUT request with JSON body
    #[instrument(skip(self, body), fields(method = "PUT"))]
    pub async fn put_raw(&self, path: &str, body: serde_json::Value) -> Result<serde_json::Value> {
        self.put(path, &body).await
    }

    /// Execute raw PATCH request with JSON body
    #[instrument(skip(self, body), fields(method = "PATCH"))]
    pub async fn patch_raw(
        &self,
        path: &str,
        body: serde_json::Value,
    ) -> Result<serde_json::Value> {
        let url = self.normalize_url(path);
        debug!("PATCH {}", url);
        trace!("Request body: {:?}", body);

        // Use backwards header names for compatibility
        let response = self
            .client
            .patch(&url)
            .header("x-api-key", &self.api_key)
            .header("x-api-secret-key", &self.api_secret)
            .json(&body)
            .send()
            .await?;

        trace!("Response status: {}", response.status());
        self.handle_response(response).await
    }

    /// Execute raw DELETE request returning any response body
    #[instrument(skip(self), fields(method = "DELETE"))]
    pub async fn delete_raw(&self, path: &str) -> Result<serde_json::Value> {
        let url = self.normalize_url(path);
        debug!("DELETE {}", url);

        // Use backwards header names for compatibility
        let response = self
            .client
            .delete(&url)
            .header("x-api-key", &self.api_key)
            .header("x-api-secret-key", &self.api_secret)
            .send()
            .await?;

        trace!("Response status: {}", response.status());
        if response.status().is_success() {
            if response.content_length() == Some(0) {
                Ok(serde_json::json!({"status": "deleted"}))
            } else {
                response.json().await.map_err(Into::into)
            }
        } else {
            let status = response.status();
            let text = response
                .text()
                .await
                .unwrap_or_else(|e| format!("(failed to read response body: {e})"));
            Err(Self::status_to_error(status, text))
        }
    }

    /// Execute DELETE request with JSON body (used by some endpoints like `PrivateLink` principals)
    #[instrument(skip(self, body), fields(method = "DELETE"))]
    pub async fn delete_with_body<T: serde::de::DeserializeOwned>(
        &self,
        path: &str,
        body: serde_json::Value,
    ) -> Result<T> {
        let url = self.normalize_url(path);
        debug!("DELETE {} (with body)", url);
        trace!("Request body: {:?}", body);

        let response = self
            .client
            .delete(&url)
            .header("x-api-key", &self.api_key)
            .header("x-api-secret-key", &self.api_secret)
            .json(&body)
            .send()
            .await?;

        trace!("Response status: {}", response.status());
        self.handle_response(response).await
    }

    /// Handle HTTP response and return both status code and body as JSON
    ///
    /// This is used internally by the Tower service implementation to preserve
    /// the actual HTTP status code in responses.
    #[cfg(feature = "tower-integration")]
    async fn handle_response_with_status(
        &self,
        response: reqwest::Response,
    ) -> Result<(u16, serde_json::Value)> {
        let status = response.status();
        let status_code = status.as_u16();

        if status.is_success() {
            let bytes = response
                .bytes()
                .await
                .map_err(|e| RestError::ConnectionError(format!("Failed to read response: {e}")))?;

            let value: serde_json::Value = serde_json::from_slice(&bytes).map_err(|e| {
                RestError::ConnectionError(format!("Failed to parse JSON response: {e}"))
            })?;

            Ok((status_code, value))
        } else {
            let text = response
                .text()
                .await
                .unwrap_or_else(|e| format!("(failed to read response body: {e})"));
            Err(Self::status_to_error(status, text))
        }
    }

    /// Handle HTTP response
    async fn handle_response<T: serde::de::DeserializeOwned>(
        &self,
        response: reqwest::Response,
    ) -> Result<T> {
        let status = response.status();

        if status.is_success() {
            // Get the response bytes for better error reporting
            let bytes = response
                .bytes()
                .await
                .map_err(|e| RestError::ConnectionError(format!("Failed to read response: {e}")))?;

            // Use serde_path_to_error for better deserialization error messages
            let deserializer = &mut serde_json::Deserializer::from_slice(&bytes);
            serde_path_to_error::deserialize(deserializer).map_err(|err| {
                let path = err.path().to_string();
                // Use ConnectionError to provide detailed error message with field path
                RestError::ConnectionError(format!(
                    "Failed to deserialize field '{}': {}",
                    path,
                    err.inner()
                ))
            })
        } else {
            let text = response
                .text()
                .await
                .unwrap_or_else(|e| format!("(failed to read response body: {e})"));
            Err(Self::status_to_error(status, text))
        }
    }
}

/// Tower Service integration for `CloudClient`
///
/// This module provides Tower Service implementations for `CloudClient`, enabling
/// middleware composition with patterns like circuit breakers, retry, and rate limiting.
///
/// # Feature Flag
///
/// This module is only available when the `tower-integration` feature is enabled.
///
/// # Examples
///
/// ```rust,ignore
/// use redis_cloud::CloudClient;
/// use redis_cloud::tower_support::ApiRequest;
/// use tower::ServiceExt;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let client = CloudClient::builder()
///     .api_key("your-key")
///     .api_secret("your-secret")
///     .build()?;
///
/// // Convert to a Tower service
/// let mut service = client.into_service();
///
/// // Use the service
/// let response = service.oneshot(ApiRequest::get("/subscriptions")).await?;
/// println!("Status: {}", response.status);
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "tower-integration")]
pub mod tower_support {
    use super::{CloudClient, RestError, Result};
    use std::future::Future;
    use std::pin::Pin;
    use std::task::{Context, Poll};
    use tower::Service;

    /// HTTP method for API requests
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum Method {
        /// GET request
        Get,
        /// POST request
        Post,
        /// PUT request
        Put,
        /// PATCH request
        Patch,
        /// DELETE request
        Delete,
    }

    /// Tower-compatible request type for Redis Cloud API
    ///
    /// This wraps the essential components of an API request in a format
    /// suitable for Tower middleware composition.
    #[derive(Debug, Clone)]
    pub struct ApiRequest {
        /// HTTP method
        pub method: Method,
        /// API endpoint path (e.g., "/subscriptions")
        pub path: String,
        /// Optional JSON body for POST/PUT/PATCH requests
        pub body: Option<serde_json::Value>,
    }

    impl ApiRequest {
        /// Create a GET request
        pub fn get(path: impl Into<String>) -> Self {
            Self {
                method: Method::Get,
                path: path.into(),
                body: None,
            }
        }

        /// Create a POST request with a JSON body
        pub fn post(path: impl Into<String>, body: serde_json::Value) -> Self {
            Self {
                method: Method::Post,
                path: path.into(),
                body: Some(body),
            }
        }

        /// Create a PUT request with a JSON body
        pub fn put(path: impl Into<String>, body: serde_json::Value) -> Self {
            Self {
                method: Method::Put,
                path: path.into(),
                body: Some(body),
            }
        }

        /// Create a PATCH request with a JSON body
        pub fn patch(path: impl Into<String>, body: serde_json::Value) -> Self {
            Self {
                method: Method::Patch,
                path: path.into(),
                body: Some(body),
            }
        }

        /// Create a DELETE request
        pub fn delete(path: impl Into<String>) -> Self {
            Self {
                method: Method::Delete,
                path: path.into(),
                body: None,
            }
        }
    }

    /// Tower-compatible response type
    ///
    /// Contains the HTTP status code and response body as JSON.
    #[derive(Debug, Clone)]
    pub struct ApiResponse {
        /// HTTP status code
        pub status: u16,
        /// Response body as JSON
        pub body: serde_json::Value,
    }

    impl CloudClient {
        /// Convert this client into a Tower service
        ///
        /// This consumes the client and returns it wrapped in a Tower service
        /// implementation, enabling middleware composition.
        ///
        /// # Examples
        ///
        /// ```rust,ignore
        /// use redis_cloud::CloudClient;
        /// use tower::ServiceExt;
        /// use redis_cloud::tower_support::ApiRequest;
        ///
        /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
        /// let client = CloudClient::builder()
        ///     .api_key("key")
        ///     .api_secret("secret")
        ///     .build()?;
        ///
        /// let mut service = client.into_service();
        /// let response = service.oneshot(ApiRequest::get("/subscriptions")).await?;
        /// # Ok(())
        /// # }
        /// ```
        #[must_use]
        pub fn into_service(self) -> Self {
            self
        }
    }

    impl Service<ApiRequest> for CloudClient {
        type Response = ApiResponse;
        type Error = RestError;
        type Future = Pin<Box<dyn Future<Output = Result<Self::Response>> + Send>>;

        fn poll_ready(
            &mut self,
            _cx: &mut Context<'_>,
        ) -> Poll<std::result::Result<(), Self::Error>> {
            // CloudClient is always ready since it uses an internal connection pool
            Poll::Ready(Ok(()))
        }

        fn call(&mut self, req: ApiRequest) -> Self::Future {
            let client = self.clone();
            Box::pin(async move {
                let url = client.normalize_url(&req.path);

                let request_builder = match req.method {
                    Method::Get => client.client.get(&url),
                    Method::Post => {
                        let body = req.body.ok_or_else(|| RestError::BadRequest {
                            message: "POST request requires a body".to_string(),
                        })?;
                        client.client.post(&url).json(&body)
                    }
                    Method::Put => {
                        let body = req.body.ok_or_else(|| RestError::BadRequest {
                            message: "PUT request requires a body".to_string(),
                        })?;
                        client.client.put(&url).json(&body)
                    }
                    Method::Patch => {
                        let body = req.body.ok_or_else(|| RestError::BadRequest {
                            message: "PATCH request requires a body".to_string(),
                        })?;
                        client.client.patch(&url).json(&body)
                    }
                    Method::Delete => client.client.delete(&url),
                };

                let response = request_builder
                    .header("x-api-key", &client.api_key)
                    .header("x-api-secret-key", &client.api_secret)
                    .send()
                    .await?;

                let (status, body) = client.handle_response_with_status(response).await?;

                Ok(ApiResponse { status, body })
            })
        }
    }
}