Skip to main content

adk_gemini/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.get_cached_content(&self.name).await.map_err(Box::new).context(ClientSnafu)
32    }
33
34    /// Updates the cached content configuration (typically the TTL).
35    pub async fn update(&self, expiration: CacheExpirationRequest) -> Result<CachedContent, Error> {
36        self.client
37            .update_cached_content(&self.name, expiration)
38            .await
39            .map_err(Box::new)
40            .context(ClientSnafu)
41    }
42
43    /// Deletes the cached content resource from the server.
44    pub async fn delete(self) -> Result<(), (Self, Error)> {
45        match self
46            .client
47            .delete_cached_content(&self.name)
48            .await
49            .map_err(Box::new)
50            .context(ClientSnafu)
51        {
52            Ok(response) => Ok(response),
53            Err(e) => Err((self, e)),
54        }
55    }
56}