Skip to main content

atlassian_rust_api/jira/endpoints/application_roles/
update_role.rs

1use std::sync::Arc;
2
3use crate::{Jira, Result, rest_client::RestClient, web::{Endpoint, JsonFormParams}};
4
5#[derive(Debug, Clone)]
6pub struct UpdateApplicationRoleBuilder {
7	client: Arc<RestClient>,
8	request: UpdateApplicationRoleRequest,
9}
10
11#[derive(Debug, Clone, Default)]
12struct UpdateApplicationRoleRequest {
13	key: String,
14	groups: Vec<String>,
15	default_groups: Vec<String>,
16	//TODO: if_match: Option<String>,
17}
18
19impl Endpoint for UpdateApplicationRoleRequest {
20	fn endpoint(&self) -> std::borrow::Cow<'static, str> {
21		format!("applicationrole/{}", &self.key).into()
22	}
23
24	// TODO: This has a header parameter of If-Match, client doesn't currently support that
25
26	fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>> {
27		let mut body = JsonFormParams::default();
28		body
29			.push("key", &self.key)
30			.push("groups", &self.groups)
31			.push("defaultGroups", &self.default_groups);
32		body.into_body()
33	}
34}
35
36impl UpdateApplicationRoleBuilder {
37	fn new(client: Arc<RestClient>) -> UpdateApplicationRoleBuilder {
38		UpdateApplicationRoleBuilder { client, request: UpdateApplicationRoleRequest::default() }
39	}
40
41	fn key(mut self, key: impl Into<String>) -> UpdateApplicationRoleBuilder {
42		self.request.key = key.into();
43		self
44	}
45
46	/// Add a group to the update request.
47	pub fn group(mut self, group: impl Into<String>) -> UpdateApplicationRoleBuilder {
48		self.request.groups.push(group.into());
49		self
50	}
51
52	/// Add multiple groups to the update request.
53	pub fn groups<S>(mut self, groups: impl IntoIterator<Item = S>) -> UpdateApplicationRoleBuilder 
54	where
55		S: Into<String>,
56	{
57		let groups = groups.into_iter().map(|f| f.into()).collect::<Vec<String>>();
58		self.request.groups.extend(groups);
59		self
60	}
61
62	/// Add a default group to the update request.
63	pub fn default_group(mut self, default_group: impl Into<String>) -> UpdateApplicationRoleBuilder {
64		self.request.default_groups.push(default_group.into());
65		self
66	}
67
68	/// Add multiple default groups to the update request.
69	pub fn default_groups<S>(mut self, default_groups: impl IntoIterator<Item = S>) -> UpdateApplicationRoleBuilder 
70	where
71		S: Into<String>,
72	{
73		let default_groups = default_groups.into_iter().map(|f| f.into()).collect::<Vec<String>>();
74		self.request.default_groups.extend(default_groups);
75		self
76	}
77
78	pub async fn send(self) -> Result<serde_json::Value> {
79		self.client.put(self.request).await
80	}
81}
82
83impl Jira {
84	/// Updates the ApplicationRole with the passed data. Only the groups and default groups setting of the role may be updated.
85	/// 
86	/// **NOT IMPLEMENTED YET** *Optional*: if `versionHash` is passed through the `If-Match` header, the request will be rejected if not the same
87	/// as the server.
88	// TODO: If-Match header
89	pub fn update_role(&self, key: impl Into<String>) -> UpdateApplicationRoleBuilder {
90		UpdateApplicationRoleBuilder::new(Arc::clone(&self.client)).key(key)
91	}
92}