Refactor get() to take less boilerplate args

This commit is contained in:
bitfl0wer 2023-05-29 23:01:17 +02:00
parent 3a6d1c5620
commit 3e4840d7a6
2 changed files with 16 additions and 19 deletions

View File

@ -4,28 +4,31 @@ use serde_json::{from_str, to_string};
use crate::{ use crate::{
api::limits::Limits, api::limits::Limits,
errors::InstanceServerError, errors::InstanceServerError,
instance::UserMeta,
limit::LimitedRequester, limit::LimitedRequester,
types::{Channel, ChannelModifySchema}, types::{Channel, ChannelModifySchema},
}; };
impl Channel { impl Channel {
pub async fn get( pub async fn get(
token: &str, user: &mut UserMeta,
url_api: &str,
channel_id: &str, channel_id: &str,
limits_user: &mut Limits,
limits_instance: &mut Limits,
) -> Result<Channel, InstanceServerError> { ) -> Result<Channel, InstanceServerError> {
let mut belongs_to = user.belongs_to.borrow_mut();
let request = Client::new() let request = Client::new()
.get(format!("{}/channels/{}/", url_api, channel_id)) .get(format!(
.bearer_auth(token); "{}/channels/{}/",
belongs_to.urls.get_api(),
channel_id
))
.bearer_auth(user.token());
let mut requester = LimitedRequester::new().await; let mut requester = LimitedRequester::new().await;
let result = match requester let result = match requester
.send_request( .send_request(
request, request,
crate::api::limits::LimitType::Guild, crate::api::limits::LimitType::Guild,
limits_instance, &mut belongs_to.limits,
limits_user, &mut user.limits,
) )
.await .await
{ {
@ -36,7 +39,7 @@ impl Channel {
match from_str::<Channel>(&result_text) { match from_str::<Channel>(&result_text) {
Ok(object) => Ok(object), Ok(object) => Ok(object),
Err(e) => Err(InstanceServerError::RequestErrorError { Err(e) => Err(InstanceServerError::RequestErrorError {
url: format!("{}/channels/{}/", url_api, channel_id), url: format!("{}/channels/{}/", belongs_to.urls.get_api(), channel_id),
error: e.to_string(), error: e.to_string(),
}), }),
} }

View File

@ -5,19 +5,13 @@ use chorus::types::{self, Channel};
async fn get_channel() { async fn get_channel() {
let mut bundle = common::setup().await; let mut bundle = common::setup().await;
let bundle_channel = bundle.channel.clone(); let bundle_channel = bundle.channel.clone();
let bundle_user = &mut bundle.user; let mut bundle_user = &mut bundle.user;
assert_eq!( assert_eq!(
bundle_channel, bundle_channel,
Channel::get( Channel::get(&mut bundle_user, &bundle_channel.id.to_string(),)
bundle_user.token.as_str(), .await
bundle.instance.urls.get_api(), .unwrap()
&bundle_channel.id.to_string(),
&mut bundle_user.limits,
&mut bundle.instance.limits
)
.await
.unwrap()
); );
common::teardown(bundle).await common::teardown(bundle).await
} }