Refactor create() and get()

This commit is contained in:
bitfl0wer 2023-05-29 23:46:43 +02:00
parent f986b33878
commit b501aca5b2
2 changed files with 23 additions and 13 deletions

View File

@ -29,10 +29,8 @@ impl Guild {
user: &mut UserMeta, user: &mut UserMeta,
guild_create_schema: GuildCreateSchema, guild_create_schema: GuildCreateSchema,
) -> Result<Guild, crate::errors::InstanceServerError> { ) -> Result<Guild, crate::errors::InstanceServerError> {
let belongs_to = user.belongs_to.borrow_mut(); let mut belongs_to = user.belongs_to.borrow_mut();
let url = format!("{}/guilds/", belongs_to.urls.get_api()); let url = format!("{}/guilds/", belongs_to.urls.get_api());
let mut limits_user = user.limits.get_as_mut();
let mut limits_instance = &mut user.belongs_to.borrow_mut().limits;
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())
@ -42,8 +40,8 @@ impl Guild {
.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
{ {
@ -51,12 +49,12 @@ impl Guild {
Err(e) => return Err(e), Err(e) => return Err(e),
}; };
let id: GuildCreateResponse = from_str(&result.text().await.unwrap()).unwrap(); let id: GuildCreateResponse = from_str(&result.text().await.unwrap()).unwrap();
let guild = Guild::get( let guild = Guild::_get(
belongs_to.urls.get_api(), belongs_to.clone().urls.get_api(),
&id.id, &id.id,
&user.token, &user.token,
&mut limits_user, &mut user.limits,
&mut limits_instance, &mut belongs_to.limits,
) )
.await .await
.unwrap(); .unwrap();
@ -210,7 +208,21 @@ impl Guild {
/// * `limits_user` - A mutable reference to a `Limits` struct containing the user's rate limits. /// * `limits_user` - A mutable reference to a `Limits` struct containing the user's rate limits.
/// * `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 get( pub async fn get(user: &mut UserMeta, guild_id: &str) -> Result<Guild, InstanceServerError> {
let mut belongs_to = user.belongs_to.borrow_mut();
Guild::_get(
&format!("{}", belongs_to.urls.get_api()),
guild_id,
&user.token,
&mut user.limits,
&mut belongs_to.limits,
)
.await
}
/// For internal use. Does the same as the public get method, but does not require a second, mutable
/// borrow of `UserMeta::belongs_to`, when used in conjunction with other methods, which borrow `UserMeta::belongs_to`.
async fn _get(
url_api: &str, url_api: &str,
guild_id: &str, guild_id: &str,
token: &str, token: &str,

View File

@ -65,9 +65,7 @@ pub async fn setup() -> TestBundle {
video_quality_mode: None, video_quality_mode: None,
}; };
let mut user = instance.register_account(&reg).await.unwrap(); let mut user = instance.register_account(&reg).await.unwrap();
let guild = Guild::create(&mut user, urls.get_api(), guild_create_schema) let guild = Guild::create(&mut user, guild_create_schema).await.unwrap();
.await
.unwrap();
let channel = Channel::create( let channel = Channel::create(
&user.token, &user.token,
urls.get_api(), urls.get_api(),