gemini_rust/cache/
handle.rs

1use snafu::ResultExt;
2use std::sync::Arc;
3
4use super::model::*;
5use super::*;
6use crate::client::GeminiClient;
7
8/// Represents a cached content resource, providing methods to manage its lifecycle.
9///
10/// A `CachedContentHandle` object is a handle to a cached content resource on the Gemini API.
11/// It allows you to retrieve, update, or delete the cached content.
12pub struct CachedContentHandle {
13    /// The unique resource name of the cached content, e.g., `cachedContents/cache-xxxxxxxx`.
14    pub name: String,
15    client: Arc<GeminiClient>,
16}
17
18impl CachedContentHandle {
19    /// Creates a new CachedContentHandle instance.
20    pub(crate) fn new(name: String, client: Arc<GeminiClient>) -> Self {
21        Self { name, client }
22    }
23
24    /// Returns the unique resource name of the cached content.
25    pub fn name(&self) -> &str {
26        &self.name
27    }
28
29    /// Retrieves the cached content configuration by making an API call.
30    pub async fn get(&self) -> Result<CachedContent, Error> {
31        self.client
32            .get_cached_content(&self.name)
33            .await
34            .map_err(Box::new)
35            .context(ClientSnafu)
36    }
37
38    /// Updates the cached content configuration (typically the TTL).
39    pub async fn update(&self, expiration: CacheExpirationRequest) -> Result<CachedContent, Error> {
40        self.client
41            .update_cached_content(&self.name, expiration)
42            .await
43            .map_err(Box::new)
44            .context(ClientSnafu)
45    }
46
47    /// Deletes the cached content resource from the server.
48    pub async fn delete(self) -> Result<(), (Self, Error)> {
49        match self
50            .client
51            .delete_cached_content(&self.name)
52            .await
53            .map_err(Box::new)
54            .context(ClientSnafu)
55        {
56            Ok(response) => Ok(response),
57            Err(e) => Err((self, e)),
58        }
59    }
60}