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.
12#[derive(Clone)]
13pub struct CachedContentHandle {
14    /// The unique resource name of the cached content, e.g., `cachedContents/cache-xxxxxxxx`.
15    pub name: String,
16    client: Arc<GeminiClient>,
17}
18
19impl CachedContentHandle {
20    /// Creates a new CachedContentHandle instance.
21    pub(crate) fn new(name: String, client: Arc<GeminiClient>) -> Self {
22        Self { name, client }
23    }
24
25    /// Returns the unique resource name of the cached content.
26    pub fn name(&self) -> &str {
27        &self.name
28    }
29
30    /// Retrieves the cached content configuration by making an API call.
31    pub async fn get(&self) -> Result<CachedContent, Error> {
32        self.client
33            .get_cached_content(&self.name)
34            .await
35            .map_err(Box::new)
36            .context(ClientSnafu)
37    }
38
39    /// Updates the cached content configuration (typically the TTL).
40    pub async fn update(&self, expiration: CacheExpirationRequest) -> Result<CachedContent, Error> {
41        self.client
42            .update_cached_content(&self.name, expiration)
43            .await
44            .map_err(Box::new)
45            .context(ClientSnafu)
46    }
47
48    /// Deletes the cached content resource from the server.
49    pub async fn delete(self) -> Result<(), (Self, Error)> {
50        match self
51            .client
52            .delete_cached_content(&self.name)
53            .await
54            .map_err(Box::new)
55            .context(ClientSnafu)
56        {
57            Ok(response) => Ok(response),
58            Err(e) => Err((self, e)),
59        }
60    }
61}