Skip to main content

atlassian_rust_api/jira/endpoints/application_roles/
update_roles.rs

1use std::sync::Arc;
2
3use crate::{Jira, Result, rest_client::RestClient, web::{Endpoint, JsonFormParams}};
4
5#[derive(Debug, Clone)]
6pub struct UpdateApplicationRolesBuilder {
7	client: Arc<RestClient>,
8	request: UpdateApplicationRolesRequest,
9}
10
11#[derive(Debug, Clone, Default)]
12struct UpdateApplicationRolesRequest {
13	key: String,
14	groups: Vec<String>,
15	default_groups: Vec<String>,
16	//TODO: if_match: Option<String>,
17}
18
19impl Endpoint for UpdateApplicationRolesRequest {
20	fn endpoint(&self) -> std::borrow::Cow<'static, str> {
21		"applicationrole".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 UpdateApplicationRolesBuilder {
37	fn new(client: Arc<RestClient>) -> UpdateApplicationRolesBuilder {
38		UpdateApplicationRolesBuilder { client, request: UpdateApplicationRolesRequest::default() }
39	}
40
41	fn key(mut self, key: impl Into<String>) -> UpdateApplicationRolesBuilder {
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>) -> UpdateApplicationRolesBuilder {
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>) -> UpdateApplicationRolesBuilder 
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>) -> UpdateApplicationRolesBuilder {
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>) -> UpdateApplicationRolesBuilder 
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 ApplicationRoles with the passed data if the version hash is the same as the server. Only
85	/// the groups and default groups setting of the role may be updated. It is acceptable to pass only the roles 
86	/// that are updated as roles that are present in the server but not in data to update with, will not be deleted.
87	/// 
88	/// *Note*: This currently does the same as [`Jira::update_role`] since this accepts 1 role
89	// TODO: Look at how I handle this in tetanus::Safe::update_issue_properties
90	pub fn update_roles(&self, key: impl Into<String>) -> UpdateApplicationRolesBuilder {
91		UpdateApplicationRolesBuilder::new(Arc::clone(&self.client)).key(key)
92	}
93}