oxide_api/
system.rs

1use anyhow::Result;
2
3use crate::Client;
4
5pub struct System {
6    pub client: Client,
7}
8
9impl System {
10    #[doc(hidden)]
11    pub fn new(client: Client) -> Self {
12        System { client }
13    }
14
15    /**
16     * List built-in users.
17     *
18     * This function performs a `GET` to the `/system/user` endpoint.
19     *
20     * **Parameters:**
21     *
22     * * `limit: u32` -- Maximum number of items returned by a single call.
23     * * `page_token: &str` -- Token returned by previous call to retrieve the subsequent page.
24     * * `sort_by: crate::types::NameSortMode` -- Supported set of sort modes for scanning by name only
25     *  
26     *  Currently, we only support scanning in ascending order.
27     */
28    pub async fn user_list(
29        &self,
30        limit: u32,
31        page_token: &str,
32        sort_by: crate::types::NameSortMode,
33    ) -> Result<Vec<crate::types::UserBuiltin>> {
34        let mut query_args: Vec<(String, String)> = Default::default();
35        if !limit.to_string().is_empty() {
36            query_args.push(("limit".to_string(), limit.to_string()));
37        }
38        if !page_token.is_empty() {
39            query_args.push(("page_token".to_string(), page_token.to_string()));
40        }
41        if !sort_by.to_string().is_empty() {
42            query_args.push(("sort_by".to_string(), sort_by.to_string()));
43        }
44        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
45        let url = format!("/system/user?{}", query_);
46
47        let resp: crate::types::UserBuiltinResultsPage = self.client.get(&url, None).await?;
48
49        // Return our response data.
50        Ok(resp.items)
51    }
52
53    /**
54     * List built-in users.
55     *
56     * This function performs a `GET` to the `/system/user` endpoint.
57     *
58     * As opposed to `user_list`, this function returns all the pages of the request at once.
59     */
60    pub async fn user_list_all(
61        &self,
62        sort_by: crate::types::NameSortMode,
63    ) -> Result<Vec<crate::types::UserBuiltin>> {
64        let mut query_args: Vec<(String, String)> = Default::default();
65        if !sort_by.to_string().is_empty() {
66            query_args.push(("sort_by".to_string(), sort_by.to_string()));
67        }
68        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
69        let url = format!("/system/user?{}", query_);
70
71        let mut resp: crate::types::UserBuiltinResultsPage = self.client.get(&url, None).await?;
72
73        let mut items = resp.items;
74        let mut page = resp.next_page;
75
76        // Paginate if we should.
77        while !page.is_empty() {
78            if !url.contains('?') {
79                resp = self
80                    .client
81                    .get(&format!("{}?page={}", url, page), None)
82                    .await?;
83            } else {
84                resp = self
85                    .client
86                    .get(&format!("{}&page={}", url, page), None)
87                    .await?;
88            }
89
90            items.append(&mut resp.items);
91
92            if !resp.next_page.is_empty() && resp.next_page != page {
93                page = resp.next_page.to_string();
94            } else {
95                page = "".to_string();
96            }
97        }
98
99        // Return our response data.
100        Ok(items)
101    }
102
103    /**
104     * Fetch a built-in user.
105     *
106     * This function performs a `GET` to the `/system/user/{user_name}` endpoint.
107     *
108     * **Parameters:**
109     *
110     * * `user_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
111     */
112    pub async fn user_view(&self, user_name: &str) -> Result<crate::types::UserBuiltin> {
113        let url = format!(
114            "/system/user/{}",
115            crate::progenitor_support::encode_path(user_name),
116        );
117
118        self.client.get(&url, None).await
119    }
120}