Refactor roles.rs to use api::common
This commit is contained in:
parent
6023839a7a
commit
43e62359ae
|
@ -2,6 +2,7 @@ use reqwest::Client;
|
||||||
use serde_json::{from_str, to_string};
|
use serde_json::{from_str, to_string};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
api::deserialize_response,
|
||||||
errors::ChorusLibError,
|
errors::ChorusLibError,
|
||||||
instance::UserMeta,
|
instance::UserMeta,
|
||||||
limit::LimitedRequester,
|
limit::LimitedRequester,
|
||||||
|
@ -26,36 +27,21 @@ impl types::RoleObject {
|
||||||
pub async fn get_all(
|
pub async fn get_all(
|
||||||
user: &mut UserMeta,
|
user: &mut UserMeta,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
) -> Result<Option<Vec<RoleObject>>, crate::errors::ChorusLibError> {
|
) -> Result<Option<Vec<RoleObject>>, ChorusLibError> {
|
||||||
let mut belongs_to = user.belongs_to.borrow_mut();
|
let belongs_to = user.belongs_to.borrow();
|
||||||
let url = format!("{}/guilds/{}/roles/", belongs_to.urls.get_api(), guild_id);
|
let url = format!("{}/guilds/{}/roles/", belongs_to.urls.get_api(), guild_id);
|
||||||
|
drop(belongs_to);
|
||||||
let request = Client::new().get(url).bearer_auth(user.token());
|
let request = Client::new().get(url).bearer_auth(user.token());
|
||||||
let result = match LimitedRequester::new()
|
let roles = deserialize_response::<Vec<RoleObject>>(
|
||||||
.await
|
|
||||||
.send_request(
|
|
||||||
request,
|
request,
|
||||||
|
user,
|
||||||
crate::api::limits::LimitType::Guild,
|
crate::api::limits::LimitType::Guild,
|
||||||
&mut belongs_to.limits,
|
|
||||||
&mut user.limits,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
.unwrap();
|
||||||
Ok(request) => request,
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
};
|
|
||||||
let roles: Vec<RoleObject> = match from_str(&result.text().await.unwrap()) {
|
|
||||||
Ok(roles) => roles,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(ChorusLibError::InvalidResponseError {
|
|
||||||
error: e.to_string(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if roles.is_empty() {
|
if roles.is_empty() {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Some(roles))
|
Ok(Some(roles))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,38 +64,17 @@ impl types::RoleObject {
|
||||||
user: &mut UserMeta,
|
user: &mut UserMeta,
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
role_id: &str,
|
role_id: &str,
|
||||||
) -> Result<RoleObject, crate::errors::ChorusLibError> {
|
) -> Result<RoleObject, ChorusLibError> {
|
||||||
let mut belongs_to = user.belongs_to.borrow_mut();
|
let belongs_to = user.belongs_to.borrow();
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"{}/guilds/{}/roles/{}/",
|
"{}/guilds/{}/roles/{}/",
|
||||||
belongs_to.urls.get_api(),
|
belongs_to.urls.get_api(),
|
||||||
guild_id,
|
guild_id,
|
||||||
role_id
|
role_id
|
||||||
);
|
);
|
||||||
|
drop(belongs_to);
|
||||||
let request = Client::new().get(url).bearer_auth(user.token());
|
let request = Client::new().get(url).bearer_auth(user.token());
|
||||||
let result = match LimitedRequester::new()
|
deserialize_response(request, user, crate::api::limits::LimitType::Guild).await
|
||||||
.await
|
|
||||||
.send_request(
|
|
||||||
request,
|
|
||||||
crate::api::limits::LimitType::Guild,
|
|
||||||
&mut belongs_to.limits,
|
|
||||||
&mut user.limits,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(request) => request,
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
};
|
|
||||||
let role: RoleObject = match from_str(&result.text().await.unwrap()) {
|
|
||||||
Ok(role) => role,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(ChorusLibError::InvalidResponseError {
|
|
||||||
error: e.to_string(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(role)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new role for a given guild.
|
/// Creates a new role for a given guild.
|
||||||
|
@ -132,8 +97,9 @@ impl types::RoleObject {
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
role_create_schema: RoleCreateModifySchema,
|
role_create_schema: RoleCreateModifySchema,
|
||||||
) -> Result<RoleObject, ChorusLibError> {
|
) -> Result<RoleObject, ChorusLibError> {
|
||||||
let mut belongs_to = user.belongs_to.borrow_mut();
|
let belongs_to = user.belongs_to.borrow();
|
||||||
let url = format!("{}/guilds/{}/roles/", belongs_to.urls.get_api(), guild_id);
|
let url = format!("{}/guilds/{}/roles/", belongs_to.urls.get_api(), guild_id);
|
||||||
|
drop(belongs_to);
|
||||||
let body = match to_string::<RoleCreateModifySchema>(&role_create_schema) {
|
let body = match to_string::<RoleCreateModifySchema>(&role_create_schema) {
|
||||||
Ok(string) => string,
|
Ok(string) => string,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
@ -143,28 +109,7 @@ impl types::RoleObject {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let request = Client::new().post(url).bearer_auth(user.token()).body(body);
|
let request = Client::new().post(url).bearer_auth(user.token()).body(body);
|
||||||
let result = match LimitedRequester::new()
|
deserialize_response(request, user, crate::api::limits::LimitType::Guild).await
|
||||||
.await
|
|
||||||
.send_request(
|
|
||||||
request,
|
|
||||||
crate::api::limits::LimitType::Guild,
|
|
||||||
&mut belongs_to.limits,
|
|
||||||
&mut user.limits,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(request) => request,
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
};
|
|
||||||
let role: RoleObject = match from_str(&result.text().await.unwrap()) {
|
|
||||||
Ok(role) => role,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(ChorusLibError::InvalidResponseError {
|
|
||||||
error: e.to_string(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(role)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Updates the position of a role in the guild's hierarchy.
|
/// Updates the position of a role in the guild's hierarchy.
|
||||||
|
@ -187,7 +132,7 @@ impl types::RoleObject {
|
||||||
guild_id: &str,
|
guild_id: &str,
|
||||||
role_position_update_schema: types::RolePositionUpdateSchema,
|
role_position_update_schema: types::RolePositionUpdateSchema,
|
||||||
) -> Result<RoleObject, ChorusLibError> {
|
) -> Result<RoleObject, ChorusLibError> {
|
||||||
let mut belongs_to = user.belongs_to.borrow_mut();
|
let belongs_to = user.belongs_to.borrow();
|
||||||
let url = format!("{}/guilds/{}/roles/", belongs_to.urls.get_api(), guild_id);
|
let url = format!("{}/guilds/{}/roles/", belongs_to.urls.get_api(), guild_id);
|
||||||
let body = match to_string(&role_position_update_schema) {
|
let body = match to_string(&role_position_update_schema) {
|
||||||
Ok(body) => body,
|
Ok(body) => body,
|
||||||
|
@ -197,29 +142,13 @@ impl types::RoleObject {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
drop(belongs_to);
|
||||||
let request = Client::new()
|
let request = Client::new()
|
||||||
.patch(url)
|
.patch(url)
|
||||||
.bearer_auth(user.token())
|
.bearer_auth(user.token())
|
||||||
.body(body);
|
.body(body);
|
||||||
let response = LimitedRequester::new()
|
deserialize_response::<RoleObject>(request, user, crate::api::limits::LimitType::Guild)
|
||||||
.await
|
.await
|
||||||
.send_request(
|
|
||||||
request,
|
|
||||||
crate::api::limits::LimitType::Guild,
|
|
||||||
&mut belongs_to.limits,
|
|
||||||
&mut user.limits,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
let role: RoleObject = match from_str(&response.text().await.unwrap()) {
|
|
||||||
Ok(role) => role,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(ChorusLibError::InvalidResponseError {
|
|
||||||
error: e.to_string(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(role)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Updates a role in a guild.
|
/// Updates a role in a guild.
|
||||||
|
@ -244,13 +173,14 @@ impl types::RoleObject {
|
||||||
role_id: &str,
|
role_id: &str,
|
||||||
role_create_schema: RoleCreateModifySchema,
|
role_create_schema: RoleCreateModifySchema,
|
||||||
) -> Result<RoleObject, ChorusLibError> {
|
) -> Result<RoleObject, ChorusLibError> {
|
||||||
let mut belongs_to = user.belongs_to.borrow_mut();
|
let belongs_to = user.belongs_to.borrow();
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"{}/guilds/{}/roles/{}",
|
"{}/guilds/{}/roles/{}",
|
||||||
belongs_to.urls.get_api(),
|
belongs_to.urls.get_api(),
|
||||||
guild_id,
|
guild_id,
|
||||||
role_id
|
role_id
|
||||||
);
|
);
|
||||||
|
drop(belongs_to);
|
||||||
let body = match to_string::<RoleCreateModifySchema>(&role_create_schema) {
|
let body = match to_string::<RoleCreateModifySchema>(&role_create_schema) {
|
||||||
Ok(string) => string,
|
Ok(string) => string,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
@ -263,27 +193,7 @@ impl types::RoleObject {
|
||||||
.patch(url)
|
.patch(url)
|
||||||
.bearer_auth(user.token())
|
.bearer_auth(user.token())
|
||||||
.body(body);
|
.body(body);
|
||||||
let result = match LimitedRequester::new()
|
deserialize_response::<RoleObject>(request, user, crate::api::limits::LimitType::Guild)
|
||||||
.await
|
.await
|
||||||
.send_request(
|
|
||||||
request,
|
|
||||||
crate::api::limits::LimitType::Guild,
|
|
||||||
&mut belongs_to.limits,
|
|
||||||
&mut user.limits,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(request) => request,
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
};
|
|
||||||
let role: RoleObject = match from_str(&result.text().await.unwrap()) {
|
|
||||||
Ok(role) => role,
|
|
||||||
Err(e) => {
|
|
||||||
return Err(ChorusLibError::InvalidResponseError {
|
|
||||||
error: e.to_string(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Ok(role)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue