Implement RoleObject::create()

This commit is contained in:
bitfl0wer 2023-06-08 22:26:24 +02:00
parent 951e41db4b
commit f41312d430
1 changed files with 30 additions and 3 deletions

View File

@ -2,6 +2,7 @@ use reqwest::Client;
use serde_json::{from_str, to_string};
use crate::{
errors::ChorusLibError,
instance::UserMeta,
limit::LimitedRequester,
types::{self, RoleCreateModifySchema, RoleObject},
@ -41,13 +42,39 @@ impl types::RoleObject {
user: &mut UserMeta,
guild_id: &str,
role_create_schema: RoleCreateModifySchema,
) {
) -> Result<RoleObject, ChorusLibError> {
let mut belongs_to = user.belongs_to.borrow_mut();
let url = format!("{}/guilds/{}/roles/", belongs_to.urls.get_api(), guild_id);
let body = match to_string::<RoleCreateModifySchema>(&role_create_schema) {
Ok(string) => string,
Err(e) =>
Err(e) => {
return Err(ChorusLibError::FormCreationError {
error: e.to_string(),
})
}
};
let request = Client::new().post(url).bearer_auth(user.token()).body()
let request = Client::new().post(url).bearer_auth(user.token()).body(body);
let result = match LimitedRequester::new()
.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)
}
}