gsuite_api/
groups.rs

1use crate::Client;
2use crate::ClientResult;
3
4pub struct Groups {
5    pub client: Client,
6}
7
8impl Groups {
9    #[doc(hidden)]
10    pub fn new(client: Client) -> Self {
11        Groups { client }
12    }
13
14    /**
15     * This function performs a `GET` to the `/admin/directory/v1/groups` endpoint.
16     *
17     * Retrieves all groups of a domain or of a user given a userKey (paginated).
18     *
19     * **Parameters:**
20     *
21     * * `customer: &str` -- The unique ID for the customer's Google Workspace account. In case of a multi-domain account, to fetch all groups for a customer, fill this field instead of domain. As an account administrator, you can also use the `my_customer` alias to represent your account's `customerId`. The `customerId` is also returned as part of the [Users](/admin-sdk/directory/v1/reference/users).
22     * * `domain: &str` -- The domain name. Use this field to get fields from only one domain. To return all domains for a customer account, use the `customer` query parameter instead.
23     * * `max_results: i64` -- Maximum number of results to return. Max allowed value is 200.
24     * * `order_by: crate::types::DirectoryGroupsListOrderBy` -- Column to use for sorting results.
25     * * `page_token: &str` -- Token to specify next page in the list.
26     * * `query: &str` -- Query string search. Should be of the form "". Complete documentation is at https: //developers.google.com/admin-sdk/directory/v1/guides/search-groups.
27     * * `sort_order: crate::types::SortOrder` -- Whether to return results in ascending or descending order. Must be used with the `orderBy` parameter.
28     * * `user_key: &str` -- Email or immutable ID of the user if only those groups are to be listed, the given user is a member of. If it's an ID, it should match with the ID of the user object.
29     */
30    pub async fn list(
31        &self,
32        customer: &str,
33        domain: &str,
34        max_results: i64,
35        order_by: crate::types::DirectoryGroupsListOrderBy,
36        page_token: &str,
37        query: &str,
38        sort_order: crate::types::SortOrder,
39        user_key: &str,
40    ) -> ClientResult<crate::Response<Vec<crate::types::Group>>> {
41        let mut query_args: Vec<(String, String)> = Default::default();
42        if !customer.is_empty() {
43            query_args.push(("customer".to_string(), customer.to_string()));
44        }
45        if !domain.is_empty() {
46            query_args.push(("domain".to_string(), domain.to_string()));
47        }
48        if max_results > 0 {
49            query_args.push(("maxResults".to_string(), max_results.to_string()));
50        }
51        if !order_by.to_string().is_empty() {
52            query_args.push(("orderBy".to_string(), order_by.to_string()));
53        }
54        if !page_token.is_empty() {
55            query_args.push(("pageToken".to_string(), page_token.to_string()));
56        }
57        if !query.is_empty() {
58            query_args.push(("query".to_string(), query.to_string()));
59        }
60        if !sort_order.to_string().is_empty() {
61            query_args.push(("sortOrder".to_string(), sort_order.to_string()));
62        }
63        if !user_key.is_empty() {
64            query_args.push(("userKey".to_string(), user_key.to_string()));
65        }
66        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
67        let url = self
68            .client
69            .url(&format!("/admin/directory/v1/groups?{}", query_), None);
70        let resp: crate::Response<crate::types::Groups> = self
71            .client
72            .get(
73                &url,
74                crate::Message {
75                    body: None,
76                    content_type: None,
77                },
78            )
79            .await?;
80
81        // Return our response data.
82        Ok(crate::Response::new(
83            resp.status,
84            resp.headers,
85            resp.body.groups.to_vec(),
86        ))
87    }
88    /**
89     * This function performs a `GET` to the `/admin/directory/v1/groups` endpoint.
90     *
91     * As opposed to `list`, this function returns all the pages of the request at once.
92     *
93     * Retrieves all groups of a domain or of a user given a userKey (paginated).
94     */
95    pub async fn list_all(
96        &self,
97        customer: &str,
98        domain: &str,
99        order_by: crate::types::DirectoryGroupsListOrderBy,
100        query: &str,
101        sort_order: crate::types::SortOrder,
102        user_key: &str,
103    ) -> ClientResult<crate::Response<Vec<crate::types::Group>>> {
104        let mut query_args: Vec<(String, String)> = Default::default();
105        if !customer.is_empty() {
106            query_args.push(("customer".to_string(), customer.to_string()));
107        }
108        if !domain.is_empty() {
109            query_args.push(("domain".to_string(), domain.to_string()));
110        }
111        if !order_by.to_string().is_empty() {
112            query_args.push(("orderBy".to_string(), order_by.to_string()));
113        }
114        if !query.is_empty() {
115            query_args.push(("query".to_string(), query.to_string()));
116        }
117        if !sort_order.to_string().is_empty() {
118            query_args.push(("sortOrder".to_string(), sort_order.to_string()));
119        }
120        if !user_key.is_empty() {
121            query_args.push(("userKey".to_string(), user_key.to_string()));
122        }
123        let query_ = serde_urlencoded::to_string(&query_args).unwrap();
124        let url = self
125            .client
126            .url(&format!("/admin/directory/v1/groups?{}", query_), None);
127        let crate::Response::<crate::types::Groups> {
128            mut status,
129            mut headers,
130            mut body,
131        } = self
132            .client
133            .get(
134                &url,
135                crate::Message {
136                    body: None,
137                    content_type: None,
138                },
139            )
140            .await?;
141
142        let mut groups = body.groups;
143        let mut page = body.next_page_token;
144
145        // Paginate if we should.
146        while !page.is_empty() {
147            if !url.contains('?') {
148                crate::Response::<crate::types::Groups> {
149                    status,
150                    headers,
151                    body,
152                } = self
153                    .client
154                    .get(
155                        &format!("{}?pageToken={}", url, page),
156                        crate::Message {
157                            body: None,
158                            content_type: None,
159                        },
160                    )
161                    .await?;
162            } else {
163                crate::Response::<crate::types::Groups> {
164                    status,
165                    headers,
166                    body,
167                } = self
168                    .client
169                    .get(
170                        &format!("{}&pageToken={}", url, page),
171                        crate::Message {
172                            body: None,
173                            content_type: None,
174                        },
175                    )
176                    .await?;
177            }
178
179            groups.append(&mut body.groups);
180
181            if !body.next_page_token.is_empty() && body.next_page_token != page {
182                page = body.next_page_token.to_string();
183            } else {
184                page = "".to_string();
185            }
186        }
187
188        // Return our response data.
189        Ok(crate::Response::new(status, headers, groups))
190    }
191    /**
192     * This function performs a `POST` to the `/admin/directory/v1/groups` endpoint.
193     *
194     * Creates a group.
195     */
196    pub async fn insert(
197        &self,
198        body: &crate::types::Group,
199    ) -> ClientResult<crate::Response<crate::types::Group>> {
200        let url = self.client.url("/admin/directory/v1/groups", None);
201        self.client
202            .post(
203                &url,
204                crate::Message {
205                    body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
206                    content_type: Some("application/json".to_string()),
207                },
208            )
209            .await
210    }
211    /**
212     * This function performs a `GET` to the `/admin/directory/v1/groups/{groupKey}` endpoint.
213     *
214     * Retrieves a group's properties.
215     *
216     * **Parameters:**
217     *
218     * * `group_key: &str` -- Identifies the group in the API request. The value can be the group's email address, group alias, or the unique group ID.
219     */
220    pub async fn get(&self, group_key: &str) -> ClientResult<crate::Response<crate::types::Group>> {
221        let url = self.client.url(
222            &format!(
223                "/admin/directory/v1/groups/{}",
224                crate::progenitor_support::encode_path(group_key),
225            ),
226            None,
227        );
228        self.client
229            .get(
230                &url,
231                crate::Message {
232                    body: None,
233                    content_type: None,
234                },
235            )
236            .await
237    }
238    /**
239     * This function performs a `PUT` to the `/admin/directory/v1/groups/{groupKey}` endpoint.
240     *
241     * Updates a group's properties.
242     *
243     * **Parameters:**
244     *
245     * * `group_key: &str` -- Identifies the group in the API request. The value can be the group's email address, group alias, or the unique group ID.
246     */
247    pub async fn update(
248        &self,
249        group_key: &str,
250        body: &crate::types::Group,
251    ) -> ClientResult<crate::Response<crate::types::Group>> {
252        let url = self.client.url(
253            &format!(
254                "/admin/directory/v1/groups/{}",
255                crate::progenitor_support::encode_path(group_key),
256            ),
257            None,
258        );
259        self.client
260            .put(
261                &url,
262                crate::Message {
263                    body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
264                    content_type: Some("application/json".to_string()),
265                },
266            )
267            .await
268    }
269    /**
270     * This function performs a `DELETE` to the `/admin/directory/v1/groups/{groupKey}` endpoint.
271     *
272     * Deletes a group.
273     *
274     * **Parameters:**
275     *
276     * * `group_key: &str` -- Identifies the group in the API request. The value can be the group's email address, group alias, or the unique group ID.
277     */
278    pub async fn delete(&self, group_key: &str) -> ClientResult<crate::Response<()>> {
279        let url = self.client.url(
280            &format!(
281                "/admin/directory/v1/groups/{}",
282                crate::progenitor_support::encode_path(group_key),
283            ),
284            None,
285        );
286        self.client
287            .delete(
288                &url,
289                crate::Message {
290                    body: None,
291                    content_type: None,
292                },
293            )
294            .await
295    }
296    /**
297     * This function performs a `PATCH` to the `/admin/directory/v1/groups/{groupKey}` endpoint.
298     *
299     * Updates a group's properties. This method supports [patch semantics](/admin-sdk/directory/v1/guides/performance#patch).
300     *
301     * **Parameters:**
302     *
303     * * `group_key: &str` -- Identifies the group in the API request. The value can be the group's email address, group alias, or the unique group ID.
304     */
305    pub async fn patch(
306        &self,
307        group_key: &str,
308        body: &crate::types::Group,
309    ) -> ClientResult<crate::Response<crate::types::Group>> {
310        let url = self.client.url(
311            &format!(
312                "/admin/directory/v1/groups/{}",
313                crate::progenitor_support::encode_path(group_key),
314            ),
315            None,
316        );
317        self.client
318            .patch(
319                &url,
320                crate::Message {
321                    body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
322                    content_type: Some("application/json".to_string()),
323                },
324            )
325            .await
326    }
327    /**
328     * This function performs a `GET` to the `/admin/directory/v1/groups/{groupKey}/aliases` endpoint.
329     *
330     * Lists all aliases for a group.
331     *
332     * **Parameters:**
333     *
334     * * `group_key: &str` -- Identifies the group in the API request. The value can be the group's email address, group alias, or the unique group ID.
335     */
336    pub async fn aliases_list(
337        &self,
338        group_key: &str,
339    ) -> ClientResult<crate::Response<crate::types::Aliases>> {
340        let url = self.client.url(
341            &format!(
342                "/admin/directory/v1/groups/{}/aliases",
343                crate::progenitor_support::encode_path(group_key),
344            ),
345            None,
346        );
347        self.client
348            .get(
349                &url,
350                crate::Message {
351                    body: None,
352                    content_type: None,
353                },
354            )
355            .await
356    }
357    /**
358     * This function performs a `POST` to the `/admin/directory/v1/groups/{groupKey}/aliases` endpoint.
359     *
360     * Adds an alias for the group.
361     *
362     * **Parameters:**
363     *
364     * * `group_key: &str` -- Identifies the group in the API request. The value can be the group's email address, group alias, or the unique group ID.
365     */
366    pub async fn aliases_insert(
367        &self,
368        group_key: &str,
369        body: &crate::types::Alias,
370    ) -> ClientResult<crate::Response<crate::types::Alias>> {
371        let url = self.client.url(
372            &format!(
373                "/admin/directory/v1/groups/{}/aliases",
374                crate::progenitor_support::encode_path(group_key),
375            ),
376            None,
377        );
378        self.client
379            .post(
380                &url,
381                crate::Message {
382                    body: Some(reqwest::Body::from(serde_json::to_vec(body)?)),
383                    content_type: Some("application/json".to_string()),
384                },
385            )
386            .await
387    }
388    /**
389     * This function performs a `DELETE` to the `/admin/directory/v1/groups/{groupKey}/aliases/{alias}` endpoint.
390     *
391     * Removes an alias.
392     *
393     * **Parameters:**
394     *
395     * * `group_key: &str` -- Identifies the group in the API request. The value can be the group's email address, group alias, or the unique group ID.
396     * * `alias: &str` -- The alias to be removed.
397     */
398    pub async fn aliases_delete(
399        &self,
400        group_key: &str,
401        alias: &str,
402    ) -> ClientResult<crate::Response<()>> {
403        let url = self.client.url(
404            &format!(
405                "/admin/directory/v1/groups/{}/aliases/{}",
406                crate::progenitor_support::encode_path(group_key),
407                crate::progenitor_support::encode_path(alias),
408            ),
409            None,
410        );
411        self.client
412            .delete(
413                &url,
414                crate::Message {
415                    body: None,
416                    content_type: None,
417                },
418            )
419            .await
420    }
421}