paperless_api_client/
search.rs

1use crate::Client;
2use anyhow::Result;
3#[derive(Clone, Debug)]
4pub struct Search {
5    pub client: Client,
6}
7
8impl Search {
9    #[doc(hidden)]
10    pub fn new(client: Client) -> Self {
11        Self { client }
12    }
13
14    #[doc = "Perform a `GET` request to `/api/search/`.\n\nGlobal search\n\n**Parameters:**\n\n- `db_only: Option<bool>`: Search only the database\n- `query: &'astr`: Query to search for (required)\n\n```rust,no_run\nasync fn example_search_retrieve() -> anyhow::Result<()> {\n    let client = paperless_api_client::Client::new_from_env();\n    let result: paperless_api_client::types::SearchResult =\n        client.search().retrieve(Some(true), \"some-string\").await?;\n    println!(\"{:?}\", result);\n    Ok(())\n}\n```"]
15    #[tracing::instrument]
16    #[allow(non_snake_case)]
17    pub async fn retrieve<'a>(
18        &'a self,
19        db_only: Option<bool>,
20        query: &'a str,
21    ) -> Result<crate::types::SearchResult, crate::types::error::Error> {
22        let mut req = self.client.client.request(
23            http::Method::GET,
24            format!("{}/{}", self.client.base_url, "api/search/"),
25        );
26        req = req.header("Authorization", format!("Token {}", &self.client.token));
27        let mut query_params = vec![("query", query.to_string())];
28        if let Some(p) = db_only {
29            query_params.push(("db_only", format!("{p}")));
30        }
31
32        req = req.query(&query_params);
33        let resp = req.send().await?;
34        let status = resp.status();
35        if status.is_success() {
36            let text = resp.text().await.unwrap_or_default();
37            serde_json::from_str(&text).map_err(|err| {
38                crate::types::error::Error::from_serde_error(
39                    format_serde_error::SerdeError::new(text.to_string(), err),
40                    status,
41                )
42            })
43        } else {
44            let text = resp.text().await.unwrap_or_default();
45            Err(crate::types::error::Error::Server {
46                body: text.to_string(),
47                status,
48            })
49        }
50    }
51
52    #[doc = "Perform a `GET` request to `/api/search/autocomplete/`.\n\nGet a list of all available tags\n\n**Parameters:**\n\n- `limit: Option<i64>`: Number of completions to return\n- `term: Option<String>`: Term to search for\n\n```rust,no_run\nasync fn example_search_autocomplete_list() -> anyhow::Result<()> {\n    let client = paperless_api_client::Client::new_from_env();\n    let result: Vec<String> = client\n        .search()\n        .autocomplete_list(Some(4 as i64), Some(\"some-string\".to_string()))\n        .await?;\n    println!(\"{:?}\", result);\n    Ok(())\n}\n```"]
53    #[tracing::instrument]
54    #[allow(non_snake_case)]
55    pub async fn autocomplete_list<'a>(
56        &'a self,
57        limit: Option<i64>,
58        term: Option<String>,
59    ) -> Result<Vec<String>, crate::types::error::Error> {
60        let mut req = self.client.client.request(
61            http::Method::GET,
62            format!("{}/{}", self.client.base_url, "api/search/autocomplete/"),
63        );
64        req = req.header("Authorization", format!("Token {}", &self.client.token));
65        let mut query_params = vec![];
66        if let Some(p) = limit {
67            query_params.push(("limit", format!("{p}")));
68        }
69
70        if let Some(p) = term {
71            query_params.push(("term", p));
72        }
73
74        req = req.query(&query_params);
75        let resp = req.send().await?;
76        let status = resp.status();
77        if status.is_success() {
78            let text = resp.text().await.unwrap_or_default();
79            serde_json::from_str(&text).map_err(|err| {
80                crate::types::error::Error::from_serde_error(
81                    format_serde_error::SerdeError::new(text.to_string(), err),
82                    status,
83                )
84            })
85        } else {
86            let text = resp.text().await.unwrap_or_default();
87            Err(crate::types::error::Error::Server {
88                body: text.to_string(),
89                status,
90            })
91        }
92    }
93}