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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use serde_json::{Value, json};
use crate::{
Endpoint,
http::{ApiResponse, errors::ApiError},
};
use super::WorkspaceResource;
impl WorkspaceResource {
/// Retrieves the current invitation code for this workspace.
///
/// The code can be shared with potential members, who join by passing it
/// to [`invite_member`](WorkspaceResource::invite_member).
///
/// # Errors
///
/// Returns [`ApiError::Transport`] on network failure or [`ApiError::Api`]
/// on an API-level error.
pub async fn get_invite_code(&self) -> Result<String, ApiError> {
let response: ApiResponse<Value> = self
.client
.request_endpoint(Endpoint::get_workspace_invite())
.await?;
let value = response.into_result_t()?;
let code = value.get("code").and_then(Value::as_str).unwrap();
Ok(code.to_string())
}
/// Accepts an invitation and joins this workspace under the given
/// permission group.
///
/// `code` is the invite code obtained from the workspace owner via
/// [`get_invite_code`](WorkspaceResource::get_invite_code). `group` is the
/// permission level assigned to the new member (e.g. `"member"`,
/// `"admin"`). Returns `Ok(true)` on success.
///
/// # Errors
///
/// Returns [`ApiError::Transport`] on network failure or [`ApiError::Api`]
/// if the code is invalid or has expired.
pub async fn invite_member(
&self,
code: &str,
group: &str,
) -> Result<bool, ApiError> {
let endpoint = Endpoint::workspace_invite_member();
let request = endpoint
.request_builder(&self.client.http_client, &self.client.base_url)
.json(&json!({
"workspaceId": self.id,
"code": code,
"group": group}))
.build()?;
self.client
.execute_request::<()>(request)
.await?
.into_bool_result()
}
/// Removes the member identified by `member_id` from this workspace.
///
/// Returns `Ok(true)` on success.
///
/// # Errors
///
/// Returns [`ApiError::Transport`] on network failure or [`ApiError::Api`]
/// if the member does not exist in this workspace.
pub async fn remove_member(
&self,
member_id: &str,
) -> Result<bool, ApiError> {
let endpoint = Endpoint::remove_workspace_member();
let request = endpoint
.request_builder(&self.client.http_client, &self.client.base_url)
.json(&json!({
"workspaceId": self.id,
"memberId": member_id}))
.build()?;
self.client
.execute_request::<()>(request)
.await?
.into_bool_result()
}
/// Changes the permission group of an existing workspace member.
///
/// `code` is the invite code that identifies the member and `group` is the
/// new permission level (e.g. `"member"`, `"admin"`). Returns `Ok(true)`
/// on success.
///
/// # Errors
///
/// Returns [`ApiError::Transport`] on network failure or [`ApiError::Api`]
/// if the member or group is invalid.
pub async fn change_member_permissions(
&self,
code: &str,
group: &str,
) -> Result<bool, ApiError> {
let endpoint = Endpoint::workspace_change_member_permissions();
let request = endpoint
.request_builder(&self.client.http_client, &self.client.base_url)
.json(&json!({
"workspaceId": self.id,
"code": code,
"group": group}))
.build()?;
self.client
.execute_request::<()>(request)
.await?
.into_bool_result()
}
}