#![allow(clippy::needless_return, clippy::into_iter_on_ref)]
use super::{configuration, ContentType, Error};
use crate::{apis::ResponseContent, models};
use reqwest;
use serde::{de::Error as _, Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AssignProjectUserRoleError {
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ListProjectUserRoleAssignmentsError {
UnknownValue(serde_json::Value),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum UnassignProjectUserRoleError {
UnknownValue(serde_json::Value),
}
#[bon::builder]
pub async fn assign_project_user_role(
configuration: &configuration::Configuration,
project_id: &str,
user_id: &str,
public_assign_organization_group_role_body: models::PublicAssignOrganizationGroupRoleBody,
) -> Result<models::UserRoleAssignment, Error<AssignProjectUserRoleError>> {
let p_path_project_id = project_id;
let p_path_user_id = user_id;
let p_body_public_assign_organization_group_role_body =
public_assign_organization_group_role_body;
let uri_str = format!(
"{}/projects/{project_id}/users/{user_id}/roles",
configuration.base_path,
project_id = crate::apis::urlencode(p_path_project_id),
user_id = crate::apis::urlencode(p_path_user_id)
);
let mut req_builder = configuration
.client
.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
req_builder = req_builder.json(&p_body_public_assign_organization_group_role_body);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::UserRoleAssignment`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::UserRoleAssignment`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<AssignProjectUserRoleError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent {
status,
content,
entity,
}))
}
}
#[bon::builder]
pub async fn list_project_user_role_assignments(
configuration: &configuration::Configuration,
project_id: &str,
user_id: &str,
limit: Option<i32>,
after: Option<&str>,
order: Option<&str>,
) -> Result<models::RoleListResource, Error<ListProjectUserRoleAssignmentsError>> {
let p_path_project_id = project_id;
let p_path_user_id = user_id;
let p_query_limit = limit;
let p_query_after = after;
let p_query_order = order;
let uri_str = format!(
"{}/projects/{project_id}/users/{user_id}/roles",
configuration.base_path,
project_id = crate::apis::urlencode(p_path_project_id),
user_id = crate::apis::urlencode(p_path_user_id)
);
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref param_value) = p_query_limit {
req_builder = req_builder.query(&[("limit", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_after {
req_builder = req_builder.query(&[("after", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_query_order {
req_builder = req_builder.query(&[("order", ¶m_value.to_string())]);
}
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::RoleListResource`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::RoleListResource`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<ListProjectUserRoleAssignmentsError> =
serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent {
status,
content,
entity,
}))
}
}
#[bon::builder]
pub async fn unassign_project_user_role(
configuration: &configuration::Configuration,
project_id: &str,
user_id: &str,
role_id: &str,
) -> Result<models::DeletedRoleAssignmentResource, Error<UnassignProjectUserRoleError>> {
let p_path_project_id = project_id;
let p_path_user_id = user_id;
let p_path_role_id = role_id;
let uri_str = format!(
"{}/projects/{project_id}/users/{user_id}/roles/{role_id}",
configuration.base_path,
project_id = crate::apis::urlencode(p_path_project_id),
user_id = crate::apis::urlencode(p_path_user_id),
role_id = crate::apis::urlencode(p_path_role_id)
);
let mut req_builder = configuration
.client
.request(reqwest::Method::DELETE, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref token) = configuration.bearer_access_token {
req_builder = req_builder.bearer_auth(token.to_owned());
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
let content_type = resp
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok())
.unwrap_or("application/octet-stream");
let content_type = super::ContentType::from(content_type);
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
match content_type {
ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::DeletedRoleAssignmentResource`"))),
ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::DeletedRoleAssignmentResource`")))),
}
} else {
let content = resp.text().await?;
let entity: Option<UnassignProjectUserRoleError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent {
status,
content,
entity,
}))
}
}