Skip to main content

cachekit/backend/
cachekitio_ttl.rs

1//! [`TtlInspectable`] implementation for the cachekit.io HTTP backend.
2
3use std::time::Duration;
4
5use async_trait::async_trait;
6use serde::{Deserialize, Serialize};
7
8use super::cachekitio::{reqwest_err_sanitized, CachekitIO};
9use super::TtlInspectable;
10use crate::error::BackendError;
11
12#[derive(Deserialize)]
13#[serde(deny_unknown_fields)]
14struct TtlResponse {
15    ttl: Option<u64>,
16}
17
18#[derive(Serialize)]
19struct RefreshTtlRequest {
20    ttl: u64,
21}
22
23#[cfg(not(target_arch = "wasm32"))]
24#[cfg_attr(not(feature = "unsync"), async_trait)]
25#[cfg_attr(feature = "unsync", async_trait(?Send))]
26impl TtlInspectable for CachekitIO {
27    async fn ttl(&self, key: &str) -> Result<Option<Duration>, BackendError> {
28        let url = self.ttl_url(key)?;
29
30        let req = self.with_standard_headers(self.client().get(&url));
31
32        let resp = req
33            .send()
34            .await
35            .map_err(|e| reqwest_err_sanitized(e, self.api_key_str()))?;
36
37        match resp.status().as_u16() {
38            200 => {
39                let body: TtlResponse = resp.json().await.map_err(|e| {
40                    BackendError::transient(format!("failed to parse TTL response: {e}"))
41                })?;
42                Ok(body.ttl.map(Duration::from_secs))
43            }
44            404 => Ok(None),
45            _ => Err(self.error_from_response(resp).await),
46        }
47    }
48
49    async fn refresh_ttl(&self, key: &str, ttl: Duration) -> Result<bool, BackendError> {
50        let secs = ttl.as_secs();
51        if secs == 0 {
52            return Err(BackendError::permanent(
53                "refresh_ttl requires at least 1 second".to_string(),
54            ));
55        }
56
57        let url = self.ttl_url(key)?;
58
59        let body = serde_json::to_vec(&RefreshTtlRequest { ttl: secs }).map_err(|e| {
60            BackendError::permanent(format!("failed to serialize refresh_ttl request: {e}"))
61        })?;
62
63        let req = self.with_standard_headers(
64            self.client()
65                .patch(&url)
66                .header("Content-Type", "application/json")
67                .body(body),
68        );
69
70        let resp = req
71            .send()
72            .await
73            .map_err(|e| reqwest_err_sanitized(e, self.api_key_str()))?;
74
75        match resp.status().as_u16() {
76            200 | 204 => Ok(true),
77            404 => Ok(false),
78            _ => Err(self.error_from_response(resp).await),
79        }
80    }
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    /// Compile-time proof that CachekitIO implements TtlInspectable.
88    fn _assert_ttl_inspectable(_b: &dyn TtlInspectable) {}
89
90    #[test]
91    fn cachekitio_is_ttl_inspectable() {
92        fn _check(backend: &CachekitIO) {
93            _assert_ttl_inspectable(backend);
94        }
95    }
96
97    /// The server answers `{"ttl": null}` for a live key with no expiry.
98    /// That must decode to `None`, never a parse error.
99    #[test]
100    fn ttl_response_decodes_null_ttl_as_none() {
101        let decode = |json: &str| serde_json::from_str::<TtlResponse>(json).map(|r| r.ttl);
102
103        assert!(matches!(decode(r#"{"ttl":null}"#), Ok(None)));
104        assert!(matches!(decode(r#"{"ttl":42}"#), Ok(Some(42))));
105        assert!(decode(r#"{"ttl":null,"extra":1}"#).is_err());
106    }
107}