Refactor guilds.rs to use api:common
This commit is contained in:
parent
f6da3dcacf
commit
3f8724535e
|
@ -2,11 +2,14 @@ use reqwest::Client;
|
||||||
use serde_json::from_str;
|
use serde_json::from_str;
|
||||||
use serde_json::to_string;
|
use serde_json::to_string;
|
||||||
|
|
||||||
|
use crate::api::deserialize_response;
|
||||||
|
use crate::api::handle_request;
|
||||||
|
use crate::api::handle_request_as_option;
|
||||||
use crate::api::limits::Limits;
|
use crate::api::limits::Limits;
|
||||||
use crate::errors::ChorusLibError;
|
use crate::errors::ChorusLibError;
|
||||||
use crate::instance::UserMeta;
|
use crate::instance::UserMeta;
|
||||||
use crate::limit::LimitedRequester;
|
use crate::limit::LimitedRequester;
|
||||||
use crate::types::{Channel, ChannelCreateSchema, Guild, GuildCreateResponse, GuildCreateSchema};
|
use crate::types::{Channel, ChannelCreateSchema, Guild, GuildCreateSchema};
|
||||||
|
|
||||||
impl Guild {
|
impl Guild {
|
||||||
/// Creates a new guild with the given parameters.
|
/// Creates a new guild with the given parameters.
|
||||||
|
@ -28,37 +31,15 @@ impl Guild {
|
||||||
pub async fn create(
|
pub async fn create(
|
||||||
user: &mut UserMeta,
|
user: &mut UserMeta,
|
||||||
guild_create_schema: GuildCreateSchema,
|
guild_create_schema: GuildCreateSchema,
|
||||||
) -> Result<Guild, crate::errors::ChorusLibError> {
|
) -> Result<Guild, ChorusLibError> {
|
||||||
let mut belongs_to = user.belongs_to.borrow_mut();
|
let belongs_to = user.belongs_to.borrow();
|
||||||
let url = format!("{}/guilds/", belongs_to.urls.get_api());
|
let url = format!("{}/guilds/", belongs_to.urls.get_api());
|
||||||
|
drop(belongs_to);
|
||||||
let request = reqwest::Client::new()
|
let request = reqwest::Client::new()
|
||||||
.post(url.clone())
|
.post(url.clone())
|
||||||
.bearer_auth(user.token.clone())
|
.bearer_auth(user.token.clone())
|
||||||
.body(to_string(&guild_create_schema).unwrap());
|
.body(to_string(&guild_create_schema).unwrap());
|
||||||
let mut requester = crate::limit::LimitedRequester::new().await;
|
deserialize_response::<Guild>(request, user, crate::api::limits::LimitType::Guild).await
|
||||||
let result = match requester
|
|
||||||
.send_request(
|
|
||||||
request,
|
|
||||||
crate::api::limits::LimitType::Guild,
|
|
||||||
&mut belongs_to.limits,
|
|
||||||
&mut user.limits,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(result) => result,
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
};
|
|
||||||
let id: GuildCreateResponse = from_str(&result.text().await.unwrap()).unwrap();
|
|
||||||
let guild = Guild::_get(
|
|
||||||
belongs_to.clone().urls.get_api(),
|
|
||||||
&id.id,
|
|
||||||
&user.token,
|
|
||||||
&mut user.limits,
|
|
||||||
&mut belongs_to.limits,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
Ok(guild)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deletes a guild.
|
/// Deletes a guild.
|
||||||
|
@ -86,25 +67,13 @@ impl Guild {
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn delete(user: &mut UserMeta, guild_id: &str) -> Option<ChorusLibError> {
|
pub async fn delete(user: &mut UserMeta, guild_id: &str) -> Option<ChorusLibError> {
|
||||||
let mut belongs_to = user.belongs_to.borrow_mut();
|
let belongs_to = user.belongs_to.borrow();
|
||||||
let url = format!("{}/guilds/{}/delete/", belongs_to.urls.get_api(), guild_id);
|
let url = format!("{}/guilds/{}/delete/", belongs_to.urls.get_api(), guild_id);
|
||||||
|
drop(belongs_to);
|
||||||
let request = reqwest::Client::new()
|
let request = reqwest::Client::new()
|
||||||
.post(url.clone())
|
.post(url.clone())
|
||||||
.bearer_auth(user.token.clone());
|
.bearer_auth(user.token.clone());
|
||||||
let mut requester = crate::limit::LimitedRequester::new().await;
|
handle_request_as_option(request, user, crate::api::limits::LimitType::Guild).await
|
||||||
let result = requester
|
|
||||||
.send_request(
|
|
||||||
request,
|
|
||||||
crate::api::limits::LimitType::Guild,
|
|
||||||
&mut belongs_to.limits,
|
|
||||||
&mut user.limits,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
if result.is_err() {
|
|
||||||
Some(result.err().unwrap())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sends a request to create a new channel in the guild.
|
/// Sends a request to create a new channel in the guild.
|
||||||
|
@ -147,7 +116,7 @@ impl Guild {
|
||||||
/// * `limits_instance` - A mutable reference to a `Limits` struct containing the instance's rate limits.
|
/// * `limits_instance` - A mutable reference to a `Limits` struct containing the instance's rate limits.
|
||||||
///
|
///
|
||||||
pub async fn channels(&self, user: &mut UserMeta) -> Result<Vec<Channel>, ChorusLibError> {
|
pub async fn channels(&self, user: &mut UserMeta) -> Result<Vec<Channel>, ChorusLibError> {
|
||||||
let mut belongs_to = user.belongs_to.borrow_mut();
|
let belongs_to = user.belongs_to.borrow();
|
||||||
let request = Client::new()
|
let request = Client::new()
|
||||||
.get(format!(
|
.get(format!(
|
||||||
"{}/guilds/{}/channels/",
|
"{}/guilds/{}/channels/",
|
||||||
|
@ -155,19 +124,10 @@ impl Guild {
|
||||||
self.id.to_string()
|
self.id.to_string()
|
||||||
))
|
))
|
||||||
.bearer_auth(user.token());
|
.bearer_auth(user.token());
|
||||||
let result = match LimitedRequester::new()
|
drop(belongs_to);
|
||||||
|
let result = handle_request(request, user, crate::api::limits::LimitType::Channel)
|
||||||
.await
|
.await
|
||||||
.send_request(
|
.unwrap();
|
||||||
request,
|
|
||||||
crate::api::limits::LimitType::Guild,
|
|
||||||
&mut belongs_to.limits,
|
|
||||||
&mut user.limits,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(result) => result,
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
};
|
|
||||||
let stringed_response = match result.text().await {
|
let stringed_response = match result.text().await {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
Loading…
Reference in New Issue