Skip to main content

claude_api/blocking/
models.rs

1//! Synchronous Models namespace.
2
3use crate::error::Result;
4use crate::models::{ListModelsParams, ModelInfo};
5use crate::pagination::Paginated;
6
7use super::Client;
8
9/// Namespace handle for the Models API (sync).
10///
11/// Obtained via [`Client::models`].
12pub struct Models<'a> {
13    client: &'a Client,
14}
15
16impl<'a> Models<'a> {
17    pub(crate) fn new(client: &'a Client) -> Self {
18        Self { client }
19    }
20
21    /// Fetch one page of models.
22    #[allow(clippy::needless_pass_by_value)]
23    pub fn list(&self, params: ListModelsParams) -> Result<Paginated<ModelInfo>> {
24        let params_ref = &params;
25        self.client.execute_with_retry(
26            || {
27                self.client
28                    .request_builder(reqwest::Method::GET, "/v1/models")
29                    .query(params_ref)
30            },
31            &[],
32        )
33    }
34
35    /// Fetch all models, transparently paging until exhausted.
36    pub fn list_all(&self) -> Result<Vec<ModelInfo>> {
37        let mut all = Vec::new();
38        let mut params = ListModelsParams::default();
39        loop {
40            let page = self.list(params.clone())?;
41            let next_cursor = page.next_after().map(str::to_owned);
42            all.extend(page.data);
43            match next_cursor {
44                Some(cursor) => params.after_id = Some(cursor),
45                None => break,
46            }
47        }
48        Ok(all)
49    }
50
51    /// Fetch metadata for a single model by ID.
52    pub fn get(&self, id: impl AsRef<str>) -> Result<ModelInfo> {
53        let path = format!("/v1/models/{}", id.as_ref());
54        self.client.execute_with_retry(
55            || self.client.request_builder(reqwest::Method::GET, &path),
56            &[],
57        )
58    }
59}