Move Channels::get() to channels.rs

This commit is contained in:
bitfl0wer 2023-05-23 16:28:25 +02:00
parent 377e619f77
commit d762a25953
No known key found for this signature in database
GPG Key ID: 84BBB60DF895ABF2
2 changed files with 43 additions and 33 deletions

View File

@ -0,0 +1,43 @@
use reqwest::Client;
use serde_json::from_str;
use crate::{
api::{limits::Limits, types},
errors::InstanceServerError,
limit::LimitedRequester,
};
impl types::Channel {
pub async fn get(
token: &str,
url_api: &str,
channel_id: &str,
limits_user: &mut Limits,
limits_instance: &mut Limits,
) -> Result<types::Channel, InstanceServerError> {
let request = Client::new()
.get(format!("{}/channels/{}/", url_api, channel_id))
.bearer_auth(token);
let mut requester = LimitedRequester::new().await;
let result = match requester
.send_request(
request,
crate::api::limits::LimitType::Guild,
limits_instance,
limits_user,
)
.await
{
Ok(result) => result,
Err(e) => return Err(e),
};
let result_text = result.text().await.unwrap();
match from_str::<types::Channel>(&result_text) {
Ok(object) => Ok(object),
Err(e) => Err(InstanceServerError::RequestErrorError {
url: format!("{}/channels/{}/", url_api, channel_id),
error: e.to_string(),
}),
}
}
}

View File

@ -198,37 +198,4 @@ impl types::Channel {
}), }),
} }
} }
pub async fn get(
token: &str,
url_api: &str,
channel_id: &str,
limits_user: &mut Limits,
limits_instance: &mut Limits,
) -> Result<types::Channel, InstanceServerError> {
let request = Client::new()
.get(format!("{}/channels/{}/", url_api, channel_id))
.bearer_auth(token);
let mut requester = LimitedRequester::new().await;
let result = match requester
.send_request(
request,
crate::api::limits::LimitType::Guild,
limits_instance,
limits_user,
)
.await
{
Ok(result) => result,
Err(e) => return Err(e),
};
let result_text = result.text().await.unwrap();
match from_str::<types::Channel>(&result_text) {
Ok(object) => Ok(object),
Err(e) => Err(InstanceServerError::RequestErrorError {
url: format!("{}/channels/{}/", url_api, channel_id),
error: e.to_string(),
}),
}
}
} }