From 915c9be4addf73f4c92328b1d42751eb4372554d Mon Sep 17 00:00:00 2001 From: Flori Weber Date: Tue, 20 Jun 2023 22:20:06 +0200 Subject: [PATCH 1/6] Change docstring from multi- to singleline --- src/limit.rs | 58 +++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/src/limit.rs b/src/limit.rs index b9820f8..855658d 100644 --- a/src/limit.rs +++ b/src/limit.rs @@ -19,36 +19,34 @@ pub struct TypedRequest { pub struct LimitedRequester; impl LimitedRequester { - /** - # send_request - Checks, if a request can be sent without hitting API rate limits and sends it, if true. - Will automatically update the rate limits of the LimitedRequester the request has been - sent with. - - ## Arguments - - `request`: A [`RequestBuilder`](reqwest::RequestBuilder) that contains a request ready to be - sent. Unfinished or invalid requests will result in the method panicing. - - `limit_type`: Because this library does not yet implement a way to check for which rate limit - will be used when the request gets send, you will have to specify this manually using a - [`LimitType`](crate::api::limits::LimitType) enum. - - ## Returns - - `Response`: The [`Response`](`reqwest::Response`) gotten from sending the request to the - server. This will be returned if the Request was built and send successfully. Is wrapped in - an [`Option`](`core::option::Option`) - - `None`: [`None`](`core::option::Option`) will be returned if the rate limit has been hit, and - the request could therefore not have been sent. - - ## Errors - - This method will error, if: - - The request does not return a success status code (200-299) - - The supplied [`RequestBuilder`](reqwest::RequestBuilder) contains invalid or incomplete - information - - There has been an error with processing (unwrapping) the [`Response`](`reqwest::Response`) - - The call to [`update_limits`](`crate::limits::update_limits`) yielded errors. Read the - methods' Errors section for more information. - */ + /// Checks if a request can be sent without hitting API rate limits and sends it, if true. + /// Will automatically update the rate limits of the LimitedRequester the request has been + /// sent with. + /// + /// # Arguments + /// + /// * `request`: A `RequestBuilder` that contains a request ready to be sent. Unfinished or + /// invalid requests will result in the method panicing. + /// * `limit_type`: Because this library does not yet implement a way to check for which rate + /// limit will be used when the request gets send, you will have to specify this manually using + /// a `LimitType` enum. + /// + /// # Returns + /// + /// * `Response`: The `Response` gotten from sending the request to the server. This will be + /// returned if the Request was built and send successfully. Is wrapped in an `Option`. + /// * `None`: `None` will be returned if the rate limit has been hit, and the request could + /// therefore not have been sent. + /// + /// # Errors + /// + /// This method will error if: + /// + /// * The request does not return a success status code (200-299) + /// * The supplied `RequestBuilder` contains invalid or incomplete information + /// * There has been an error with processing (unwrapping) the `Response` + /// * The call to `update_limits` yielded errors. Read the methods' Errors section for more + /// information. pub async fn send_request( request: RequestBuilder, limit_type: LimitType, From 0a523d54c1f85c8d91ea2b61317e761d7306a44c Mon Sep 17 00:00:00 2001 From: Flori Weber Date: Tue, 20 Jun 2023 22:30:42 +0200 Subject: [PATCH 2/6] Multiline docstring to single line docstring --- src/api/users/users.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/api/users/users.rs b/src/api/users/users.rs index 5bd509b..bf7b94f 100644 --- a/src/api/users/users.rs +++ b/src/api/users/users.rs @@ -10,16 +10,18 @@ use crate::{ }; impl UserMeta { - /** - Get a user object by id, or get the current user. - # Arguments - * `token` - A valid access token for the API. - * `url_api` - The URL to the API. - * `id` - The id of the user that will be retrieved. If this is None, the current user will be retrieved. - * `instance_limits` - The [`Limits`] of the instance. - # Errors - * [`ChorusLibError`] - If the request fails. - */ + /// Get a user object by id, or get the current user. + /// + /// # Arguments + /// + /// * `token` - A valid access token for the API. + /// * `url_api` - The URL to the API. + /// * `id` - The id of the user that will be retrieved. If this is None, the current user will be retrieved. + /// * `instance_limits` - The [`Limits`] of the instance. + /// + /// # Errors + /// + /// * [`ChorusLibError`] - If the request fails. pub async fn get(user: &mut UserMeta, id: Option<&String>) -> Result { User::get(user, id).await } From 3958f049ea70505c4dd155b09fcfbf68ecd1d5bf Mon Sep 17 00:00:00 2001 From: Flori Weber Date: Tue, 20 Jun 2023 22:36:39 +0200 Subject: [PATCH 3/6] Change to single-line docstring --- src/api/auth/register.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/api/auth/register.rs b/src/api/auth/register.rs index 85196cb..a862ae3 100644 --- a/src/api/auth/register.rs +++ b/src/api/auth/register.rs @@ -12,13 +12,15 @@ use crate::{ }; impl Instance { - /** - Registers a new user on the Spacebar server. - # Arguments - * `register_schema` - The [`RegisterSchema`] that contains all the information that is needed to register a new user. - # Errors - * [`ChorusLibError`] - If the server does not respond. - */ + /// Registers a new user on the Spacebar server. + /// + /// # Arguments + /// + /// * `register_schema` - The [`RegisterSchema`] that contains all the information that is needed to register a new user. + /// + /// # Errors + /// + /// * [`ChorusLibError`] - If the server does not respond. pub async fn register_account( &mut self, register_schema: &RegisterSchema, From 27047ab909d4b80fd5d48ae1cf898a1c12a49ad6 Mon Sep 17 00:00:00 2001 From: Flori Weber Date: Tue, 20 Jun 2023 22:41:31 +0200 Subject: [PATCH 4/6] Add reqwest::Client to chorus::Instance. --- src/instance.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/instance.rs b/src/instance.rs index d33fb95..2bdbbb7 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -2,6 +2,7 @@ use std::cell::RefCell; use std::fmt; use std::rc::Rc; +use reqwest::Client; use serde::{Deserialize, Serialize}; use crate::api::limits::Limits; @@ -17,6 +18,7 @@ pub struct Instance { pub urls: UrlBundle, pub instance_info: GeneralConfiguration, pub limits: Limits, + pub client: Client, } impl Instance { @@ -32,6 +34,7 @@ impl Instance { // Will be overwritten in the next step instance_info: GeneralConfiguration::default(), limits: Limits::check_limits(urls.api).await, + client: Client::new(), }; instance.instance_info = match instance.general_configuration_schema().await { Ok(schema) => schema, From 2233063d5f105304528acfdb7c68206029f42095 Mon Sep 17 00:00:00 2001 From: Flori Weber Date: Tue, 20 Jun 2023 22:42:08 +0200 Subject: [PATCH 5/6] Use Instance.client instead of creating a new one. --- src/limit.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/limit.rs b/src/limit.rs index fba8474..9f453a7 100644 --- a/src/limit.rs +++ b/src/limit.rs @@ -1,8 +1,9 @@ -use reqwest::{Client, RequestBuilder, Response}; +use reqwest::{RequestBuilder, Response}; use crate::{ api::limits::{Limit, LimitType, Limits, LimitsMutRef}, errors::ChorusLibError, + instance::Instance, }; #[derive(Debug)] @@ -40,10 +41,10 @@ impl LimitedRequester { pub async fn send_request( request: RequestBuilder, limit_type: LimitType, - instance_rate_limits: &mut Limits, + instance: &mut Instance, user_rate_limits: &mut Limits, ) -> Result { - if LimitedRequester::can_send_request(limit_type, instance_rate_limits, user_rate_limits) { + if LimitedRequester::can_send_request(limit_type, &instance.limits, user_rate_limits) { let built_request = match request.build() { Ok(request) => request, Err(e) => { @@ -53,7 +54,7 @@ impl LimitedRequester { }); } }; - let result = Client::new().execute(built_request).await; + let result = instance.client.execute(built_request).await; let response = match result { Ok(is_response) => is_response, Err(e) => { @@ -65,7 +66,7 @@ impl LimitedRequester { LimitedRequester::update_limits( &response, limit_type, - instance_rate_limits, + &mut instance.limits, user_rate_limits, ); if !response.status().is_success() { @@ -256,17 +257,17 @@ mod rate_limit { String::from("http://localhost:3001/cdn"), ); let mut request: Option> = None; - let mut instance_rate_limits = Limits::check_limits(urls.api.clone()).await; + let mut instance = Instance::new(urls.clone()).await.unwrap(); let mut user_rate_limits = Limits::check_limits(urls.api.clone()).await; for _ in 0..=50 { let request_path = urls.api.clone() + "/some/random/nonexisting/path"; - let request_builder = Client::new().get(request_path); + let request_builder = instance.client.get(request_path); request = Some( LimitedRequester::send_request( request_builder, LimitType::Channel, - &mut instance_rate_limits, + &mut instance, &mut user_rate_limits, ) .await, @@ -282,15 +283,15 @@ mod rate_limit { String::from("wss://localhost:3001/"), String::from("http://localhost:3001/cdn"), ); - let mut instance_rate_limits = Limits::check_limits(urls.api.clone()).await; + let mut instance = Instance::new(urls.clone()).await.unwrap(); let mut user_rate_limits = Limits::check_limits(urls.api.clone()).await; let _requester = LimitedRequester; let request_path = urls.api.clone() + "/policies/instance/limits"; - let request_builder = Client::new().get(request_path); + let request_builder = instance.client.get(request_path); let request = LimitedRequester::send_request( request_builder, LimitType::Channel, - &mut instance_rate_limits, + &mut instance, &mut user_rate_limits, ) .await; From e7eee4808e29477128566adc693b7b5f8b147469 Mon Sep 17 00:00:00 2001 From: Flori Weber Date: Tue, 20 Jun 2023 22:42:42 +0200 Subject: [PATCH 6/6] Change send_request() calls to pass &mut Instance --- src/api/auth/login.rs | 2 +- src/api/auth/register.rs | 4 ++-- src/api/common.rs | 2 +- src/api/guilds/guilds.rs | 15 ++++++++------- src/api/policies/instance/instance.rs | 8 +++----- src/api/users/users.rs | 20 ++++++++++---------- 6 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/api/auth/login.rs b/src/api/auth/login.rs index 28b50fe..85ba0e7 100644 --- a/src/api/auth/login.rs +++ b/src/api/auth/login.rs @@ -26,7 +26,7 @@ impl Instance { let response = LimitedRequester::send_request( request_builder, LimitType::AuthRegister, - &mut self.limits, + self, &mut cloned_limits, ) .await; diff --git a/src/api/auth/register.rs b/src/api/auth/register.rs index a862ae3..0a8ec5c 100644 --- a/src/api/auth/register.rs +++ b/src/api/auth/register.rs @@ -36,7 +36,7 @@ impl Instance { let response = LimitedRequester::send_request( request_builder, LimitType::AuthRegister, - &mut self.limits, + self, &mut cloned_limits, ) .await; @@ -61,7 +61,7 @@ impl Instance { return Err(ChorusLibError::InvalidFormBodyError { error_type, error }); } let user_object = self.get_user(token.clone(), None).await.unwrap(); - let settings = UserMeta::get_settings(&token, &self.urls.api, &mut self.limits) + let settings = UserMeta::get_settings(&token, &self.urls.api.clone(), self) .await .unwrap(); let user = UserMeta::new( diff --git a/src/api/common.rs b/src/api/common.rs index f61ab19..e627c9f 100644 --- a/src/api/common.rs +++ b/src/api/common.rs @@ -15,7 +15,7 @@ pub async fn handle_request( LimitedRequester::send_request( request, limit_type, - &mut user.belongs_to.borrow_mut().limits, + &mut user.belongs_to.borrow_mut(), &mut user.limits, ) .await diff --git a/src/api/guilds/guilds.rs b/src/api/guilds/guilds.rs index 05bf8c2..14bb8b8 100644 --- a/src/api/guilds/guilds.rs +++ b/src/api/guilds/guilds.rs @@ -7,6 +7,7 @@ use crate::api::handle_request; use crate::api::handle_request_as_result; use crate::api::limits::Limits; use crate::errors::ChorusLibError; +use crate::instance::Instance; use crate::instance::UserMeta; use crate::limit::LimitedRequester; use crate::types::{Channel, ChannelCreateSchema, Guild, GuildCreateSchema}; @@ -101,7 +102,7 @@ impl Guild { &self.id.to_string(), schema, &mut user.limits, - &mut belongs_to.limits, + &mut belongs_to, ) .await } @@ -161,7 +162,7 @@ impl Guild { guild_id, &user.token, &mut user.limits, - &mut belongs_to.limits, + &mut belongs_to, ) .await } @@ -173,7 +174,7 @@ impl Guild { guild_id: &str, token: &str, limits_user: &mut Limits, - limits_instance: &mut Limits, + instance: &mut Instance, ) -> Result { let request = Client::new() .get(format!("{}/guilds/{}/", url_api, guild_id)) @@ -181,7 +182,7 @@ impl Guild { let response = match LimitedRequester::send_request( request, crate::api::limits::LimitType::Guild, - limits_instance, + instance, limits_user, ) .await @@ -221,7 +222,7 @@ impl Channel { guild_id, schema, &mut user.limits, - &mut belongs_to.limits, + &mut belongs_to, ) .await } @@ -232,7 +233,7 @@ impl Channel { guild_id: &str, schema: ChannelCreateSchema, limits_user: &mut Limits, - limits_instance: &mut Limits, + instance: &mut Instance, ) -> Result { let request = Client::new() .post(format!("{}/guilds/{}/channels/", url_api, guild_id)) @@ -241,7 +242,7 @@ impl Channel { let result = match LimitedRequester::send_request( request, crate::api::limits::LimitType::Guild, - limits_instance, + instance, limits_user, ) .await diff --git a/src/api/policies/instance/instance.rs b/src/api/policies/instance/instance.rs index 816ab11..4a47f24 100644 --- a/src/api/policies/instance/instance.rs +++ b/src/api/policies/instance/instance.rs @@ -6,11 +6,9 @@ use crate::instance::Instance; use crate::types::GeneralConfiguration; impl Instance { - /** - Gets the instance policies schema. - # Errors - [`ChorusLibError`] - If the request fails. - */ + /// Gets the instance policies schema. + /// # Errors + /// [`ChorusLibError`] - If the request fails. pub async fn general_configuration_schema( &self, ) -> Result { diff --git a/src/api/users/users.rs b/src/api/users/users.rs index bf7b94f..4144b03 100644 --- a/src/api/users/users.rs +++ b/src/api/users/users.rs @@ -29,9 +29,9 @@ impl UserMeta { pub async fn get_settings( token: &String, url_api: &String, - instance_limits: &mut Limits, + instance: &mut Instance, ) -> Result { - User::get_settings(token, url_api, instance_limits).await + User::get_settings(token, url_api, instance).await } /// Modify the current user's `UserObject`. @@ -91,7 +91,7 @@ impl User { User::_get( &user.token(), &format!("{}", belongs_to.urls.api), - &mut belongs_to.limits, + &mut belongs_to, id, ) .await @@ -100,7 +100,7 @@ impl User { async fn _get( token: &str, url_api: &str, - limits_instance: &mut Limits, + instance: &mut Instance, id: Option<&String>, ) -> Result { let url = if id.is_none() { @@ -109,11 +109,11 @@ impl User { format!("{}/users/{}", url_api, id.unwrap()) }; let request = reqwest::Client::new().get(url).bearer_auth(token); - let mut cloned_limits = limits_instance.clone(); + let mut cloned_limits = instance.limits.clone(); match LimitedRequester::send_request( request, crate::api::limits::LimitType::Ip, - limits_instance, + instance, &mut cloned_limits, ) .await @@ -129,16 +129,16 @@ impl User { pub async fn get_settings( token: &String, url_api: &String, - instance_limits: &mut Limits, + instance: &mut Instance, ) -> Result { let request: reqwest::RequestBuilder = Client::new() .get(format!("{}/users/@me/settings/", url_api)) .bearer_auth(token); - let mut cloned_limits = instance_limits.clone(); + let mut cloned_limits = instance.limits.clone(); match LimitedRequester::send_request( request, crate::api::limits::LimitType::Ip, - instance_limits, + instance, &mut cloned_limits, ) .await @@ -165,6 +165,6 @@ impl Instance { token: String, id: Option<&String>, ) -> Result { - User::_get(&token, &self.urls.api, &mut self.limits, id).await + User::_get(&token, &self.urls.api.clone(), self, id).await } }