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
//! The explicit context-cache verbs of a Gemini [`CachedContents`] model.
//! Each is one call through the driver; a request that targets an existing
//! handle maps HTTP 403 and 404 to [`ProviderError::CacheExpired`].
use crate::driver::{Model, Transport};
use crate::error::ProviderError;
use crate::providers::gemini::cached_content::{
CacheExpiry, CachedContent, CachedContentRequest, CachedContents, NewCachedContent, on_handle,
};
/// Explicit context-cache operations. Requests targeting an existing handle
/// map HTTP 403 and 404 to [`ProviderError::CacheExpired`].
impl<T> Model<CachedContents, T>
where
T: Transport<CachedContents>,
{
/// Creates cached content and returns its handle and storage usage metadata.
pub async fn create(&self, request: NewCachedContent) -> Result<CachedContent, ProviderError> {
self.call(CachedContentRequest::Create(request))
.await?
.resource()
}
/// Fetch one cached content by handle.
pub async fn get(&self, name: &str) -> Result<CachedContent, ProviderError> {
self.call(CachedContentRequest::Get(name.to_owned()))
.await
.map_err(|error| on_handle(error, name))?
.resource()
}
/// Every cached content this API key can see, following pagination at
/// the wire's page size.
pub async fn list(&self) -> Result<Vec<CachedContent>, ProviderError> {
let pages =
crate::driver::follow_cursors(self.name(), "cached_content", |page_token| async move {
let reply = self.call(CachedContentRequest::List { page_token }).await?;
let next = reply.next_page_token();
Ok((reply.entries()?, next))
})
.await?;
Ok(pages.into_iter().flatten().collect())
}
/// Changes cache expiry without modifying its immutable content.
/// Returns the updated resource or an error, including `Expired` for HTTP 403/404.
pub async fn update_expiry(
&self,
name: &str,
expiry: CacheExpiry,
) -> Result<CachedContent, ProviderError> {
let request = CachedContentRequest::UpdateExpiry {
name: name.to_owned(),
expiry,
};
self.call(request)
.await
.map_err(|error| on_handle(error, name))?
.resource()
}
/// Deletes cached content before its expiry. Callers should also clean up
/// task-scoped caches on failure to avoid continued storage charges.
/// Handles other than `cachedContents/<id>` or bare `<id>` return
/// [`ProviderError::Request`] before dispatch.
pub async fn delete(&self, name: &str) -> Result<(), ProviderError> {
self.call(CachedContentRequest::Delete(name.to_owned()))
.await
.map_err(|error| on_handle(error, name))?;
Ok(())
}
}