Refactor channels.rs to use api::common
This commit is contained in:
parent
5d039c3e9b
commit
bf005c44a4
|
@ -1,44 +1,35 @@
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use serde_json::{from_str, to_string};
|
use serde_json::to_string;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
api::common,
|
||||||
errors::ChorusLibError,
|
errors::ChorusLibError,
|
||||||
instance::UserMeta,
|
instance::UserMeta,
|
||||||
limit::LimitedRequester,
|
|
||||||
types::{Channel, ChannelModifySchema},
|
types::{Channel, ChannelModifySchema},
|
||||||
};
|
};
|
||||||
|
|
||||||
impl Channel {
|
impl Channel {
|
||||||
pub async fn get(user: &mut UserMeta, channel_id: &str) -> Result<Channel, ChorusLibError> {
|
pub async fn get(user: &mut UserMeta, channel_id: &str) -> Result<Channel, ChorusLibError> {
|
||||||
let mut belongs_to = user.belongs_to.borrow_mut();
|
let belongs_to = user.belongs_to.borrow_mut();
|
||||||
|
let url = belongs_to.urls.get_api().to_string();
|
||||||
|
drop(belongs_to);
|
||||||
let request = Client::new()
|
let request = Client::new()
|
||||||
.get(format!(
|
.get(format!("{}/channels/{}/", url, channel_id))
|
||||||
"{}/channels/{}/",
|
|
||||||
belongs_to.urls.get_api(),
|
|
||||||
channel_id
|
|
||||||
))
|
|
||||||
.bearer_auth(user.token());
|
.bearer_auth(user.token());
|
||||||
let mut requester = LimitedRequester::new().await;
|
|
||||||
let result = match requester
|
let result = common::deserialize_response::<Channel>(
|
||||||
.send_request(
|
|
||||||
request,
|
request,
|
||||||
crate::api::limits::LimitType::Guild,
|
user,
|
||||||
&mut belongs_to.limits,
|
crate::api::limits::LimitType::Channel,
|
||||||
&mut user.limits,
|
|
||||||
)
|
)
|
||||||
.await
|
.await;
|
||||||
{
|
if result.is_err() {
|
||||||
Ok(result) => result,
|
return Err(ChorusLibError::RequestErrorError {
|
||||||
Err(e) => return Err(e),
|
url: format!("{}/channels/{}/", url, channel_id),
|
||||||
};
|
error: result.err().unwrap().to_string(),
|
||||||
let result_text = result.text().await.unwrap();
|
});
|
||||||
match from_str::<Channel>(&result_text) {
|
|
||||||
Ok(object) => Ok(object),
|
|
||||||
Err(e) => Err(ChorusLibError::RequestErrorError {
|
|
||||||
url: format!("{}/channels/{}/", belongs_to.urls.get_api(), channel_id),
|
|
||||||
error: e.to_string(),
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
|
Ok(result.unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deletes a channel.
|
/// Deletes a channel.
|
||||||
|
@ -55,7 +46,7 @@ impl Channel {
|
||||||
///
|
///
|
||||||
/// An `Option` that contains an `ChorusLibError` if an error occurred during the request, or `None` if the request was successful.
|
/// An `Option` that contains an `ChorusLibError` if an error occurred during the request, or `None` if the request was successful.
|
||||||
pub async fn delete(self, user: &mut UserMeta) -> Option<ChorusLibError> {
|
pub async fn delete(self, user: &mut UserMeta) -> Option<ChorusLibError> {
|
||||||
let mut belongs_to = user.belongs_to.borrow_mut();
|
let belongs_to = user.belongs_to.borrow_mut();
|
||||||
let request = Client::new()
|
let request = Client::new()
|
||||||
.delete(format!(
|
.delete(format!(
|
||||||
"{}/channels/{}/",
|
"{}/channels/{}/",
|
||||||
|
@ -63,18 +54,13 @@ impl Channel {
|
||||||
self.id.to_string()
|
self.id.to_string()
|
||||||
))
|
))
|
||||||
.bearer_auth(user.token());
|
.bearer_auth(user.token());
|
||||||
match LimitedRequester::new()
|
drop(belongs_to);
|
||||||
.await
|
let response =
|
||||||
.send_request(
|
common::handle_request(request, user, crate::api::limits::LimitType::Channel).await;
|
||||||
request,
|
if response.is_err() {
|
||||||
crate::api::limits::LimitType::Channel,
|
return Some(response.err().unwrap());
|
||||||
&mut belongs_to.limits,
|
} else {
|
||||||
&mut user.limits,
|
return None;
|
||||||
)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(_) => None,
|
|
||||||
Err(e) => return Some(e),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,7 +83,7 @@ impl Channel {
|
||||||
channel_id: &str,
|
channel_id: &str,
|
||||||
user: &mut UserMeta,
|
user: &mut UserMeta,
|
||||||
) -> Result<Channel, ChorusLibError> {
|
) -> Result<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()
|
||||||
.patch(format!(
|
.patch(format!(
|
||||||
"{}/channels/{}/",
|
"{}/channels/{}/",
|
||||||
|
@ -106,19 +92,14 @@ impl Channel {
|
||||||
))
|
))
|
||||||
.bearer_auth(user.token())
|
.bearer_auth(user.token())
|
||||||
.body(to_string(&modify_data).unwrap());
|
.body(to_string(&modify_data).unwrap());
|
||||||
let channel = match LimitedRequester::new()
|
drop(belongs_to);
|
||||||
.await
|
let channel = common::deserialize_response::<Channel>(
|
||||||
.send_request(
|
|
||||||
request,
|
request,
|
||||||
|
user,
|
||||||
crate::api::limits::LimitType::Channel,
|
crate::api::limits::LimitType::Channel,
|
||||||
&mut belongs_to.limits,
|
|
||||||
&mut user.limits,
|
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
.unwrap();
|
||||||
Ok(channel) => from_str::<Channel>(&channel.text().await.unwrap()).unwrap(),
|
|
||||||
Err(e) => return Err(e),
|
|
||||||
};
|
|
||||||
Ok(channel)
|
Ok(channel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue