Skip to content

Commit

Permalink
Merge pull request #6 from imor/reduce_allocations
Browse files Browse the repository at this point in the history
Reduced allocations by using &str instead of String
  • Loading branch information
Proziam authored Oct 26, 2024
2 parents 8d14d03 + 7adc8a4 commit a218c35
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 101 deletions.
109 changes: 40 additions & 69 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,8 @@ impl AuthClient {
///
/// assert!(session.user.email == demo_email)
/// ```
pub async fn login_with_email<S: Into<String>>(
&self,
email: S,
password: S,
) -> Result<Session, Error> {
let payload = SignInWithEmailAndPasswordPayload {
email: email.into(),
password: password.into(),
};
pub async fn login_with_email(&self, email: &str, password: &str) -> Result<Session, Error> {
let payload = SignInWithEmailAndPasswordPayload { email, password };

let mut headers = header::HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
Expand Down Expand Up @@ -119,15 +112,8 @@ impl AuthClient {
///
/// assert!(session.user.phone == demo_phone)
/// ```
pub async fn login_with_phone<S: Into<String>>(
&self,
phone: S,
password: S,
) -> Result<Session, Error> {
let payload = SignInWithPhoneAndPasswordPayload {
phone: phone.into(),
password: password.into(),
};
pub async fn login_with_phone(&self, phone: &str, password: &str) -> Result<Session, Error> {
let payload = SignInWithPhoneAndPasswordPayload { phone, password };

let mut headers = header::HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?);
Expand Down Expand Up @@ -167,15 +153,15 @@ impl AuthClient {
///
/// assert!(session.user.email == demo_email)
///```
pub async fn sign_up_with_email_and_password<S: Into<String>>(
pub async fn sign_up_with_email_and_password(
&self,
email: S,
password: S,
email: &str,
password: &str,
options: Option<SignUpWithPasswordOptions>,
) -> Result<Session, Error> {
let payload = SignUpWithEmailAndPasswordPayload {
email: email.into(),
password: password.into(),
email,
password,
options,
};

Expand Down Expand Up @@ -214,15 +200,15 @@ impl AuthClient {
///
/// assert!(session.user.phone == demo_phone)
///```
pub async fn sign_up_with_phone_and_password<S: Into<String>>(
pub async fn sign_up_with_phone_and_password(
&self,
phone: S,
password: S,
phone: &str,
password: &str,
options: Option<SignUpWithPasswordOptions>,
) -> Result<Session, Error> {
let payload = SignUpWithPhoneAndPasswordPayload {
phone: phone.into(),
password: password.into(),
phone,
password,
options,
};

Expand Down Expand Up @@ -259,13 +245,8 @@ impl AuthClient {
/// .await
/// .unwrap();
///```
pub async fn send_login_email_with_magic_link<S: Into<String>>(
&self,
email: S,
) -> Result<(), Error> {
let payload = RequestMagicLinkPayload {
email: email.into(),
};
pub async fn send_login_email_with_magic_link(&self, email: &str) -> Result<(), Error> {
let payload = RequestMagicLinkPayload { email };

let mut headers = header::HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?);
Expand Down Expand Up @@ -302,10 +283,8 @@ impl AuthClient {
/// ```
/// let response = auth_client.send_sms_with_otp(demo_phone).await;
/// ```
pub async fn send_sms_with_otp<S: Into<String>>(&self, phone: S) -> Result<OTPResponse, Error> {
let payload = SendSMSOtpPayload {
phone: phone.into(),
};
pub async fn send_sms_with_otp(&self, phone: &str) -> Result<OTPResponse, Error> {
let payload = SendSMSOtpPayload { phone };

let mut headers = header::HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?);
Expand Down Expand Up @@ -342,15 +321,12 @@ impl AuthClient {
/// ```
/// let send = auth_client.send_sms_with_otp(demo_phone).await.unwrap();
/// ```
pub async fn send_email_with_otp<S: Into<String>>(
pub async fn send_email_with_otp(
&self,
email: S,
email: &str,
options: Option<SignInEmailOtpParams>,
) -> Result<OTPResponse, Error> {
let payload = SignInWithEmailOtpPayload {
email: email.into(),
options,
};
let payload = SignInWithEmailOtpPayload { email, options };

let mut headers = header::HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?);
Expand Down Expand Up @@ -445,12 +421,12 @@ impl AuthClient {
///
/// assert!(user.email == demo_email)
/// ```
pub async fn get_user<S: Into<String>>(&self, bearer_token: S) -> Result<User, Error> {
pub async fn get_user(&self, bearer_token: &str) -> Result<User, Error> {
let mut headers = header::HeaderMap::new();
headers.insert("apikey", HeaderValue::from_str(&self.api_key)?);
headers.insert(
AUTHORIZATION,
HeaderValue::from_str(&format!("Bearer {}", &bearer_token.into()))?,
HeaderValue::from_str(&format!("Bearer {}", bearer_token))?,
);

let response = self
Expand Down Expand Up @@ -485,17 +461,17 @@ impl AuthClient {
/// .await
/// .unwrap();
/// ```
pub async fn update_user<S: Into<String>>(
pub async fn update_user(
&self,
updated_user: UpdateUserPayload,
bearer_token: S,
bearer_token: &str,
) -> Result<User, Error> {
let mut headers = header::HeaderMap::new();
headers.insert("apikey", HeaderValue::from_str(&self.api_key)?);
headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?);
headers.insert(
AUTHORIZATION,
HeaderValue::from_str(&format!("Bearer {}", &bearer_token.into()))?,
HeaderValue::from_str(&format!("Bearer {}", bearer_token))?,
);

let body = serde_json::to_string::<UpdateUserPayload>(&updated_user)?;
Expand Down Expand Up @@ -577,18 +553,18 @@ impl AuthClient {
/// .await
/// .unwrap();
///```
pub async fn invite_user_by_email<S: Into<String>>(
pub async fn invite_user_by_email(
&self,
email: S,
email: &str,
data: Option<Value>,
bearer_token: S,
bearer_token: &str,
) -> Result<User, Error> {
let mut headers = HeaderMap::new();
headers.insert("apikey", HeaderValue::from_str(&self.api_key)?);
headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?);
headers.insert(
AUTHORIZATION,
HeaderValue::from_str(&format!("Bearer {}", &bearer_token.into()))?,
HeaderValue::from_str(&format!("Bearer {}", bearer_token))?,
);

let invite_payload = InviteParams {
Expand Down Expand Up @@ -733,17 +709,12 @@ impl AuthClient {
/// .await
/// .unwrap();
/// ```
pub async fn exchange_token_for_session<S: Into<String>>(
&self,
refresh_token: S,
) -> Result<Session, Error> {
pub async fn exchange_token_for_session(&self, refresh_token: &str) -> Result<Session, Error> {
let mut headers = HeaderMap::new();
headers.insert("apikey", HeaderValue::from_str(&self.api_key)?);
headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?);

let body = serde_json::to_string(&RefreshSessionPayload {
refresh_token: refresh_token.into(),
})?;
let body = serde_json::to_string(&RefreshSessionPayload { refresh_token })?;

let response = self
.client
Expand All @@ -767,7 +738,7 @@ impl AuthClient {
Ok(session)
}

pub async fn refresh_session(&self, refresh_token: String) -> Result<Session, Error> {
pub async fn refresh_session(&self, refresh_token: &str) -> Result<Session, Error> {
self.exchange_token_for_session(refresh_token).await
}

Expand All @@ -777,7 +748,7 @@ impl AuthClient {
/// ```
/// let response = auth_client.reset_password_for_email(demo_email).await.unwrap();
/// ```
pub async fn reset_password_for_email<S: Into<String>>(&self, email: S) -> Result<(), Error> {
pub async fn reset_password_for_email(&self, email: &str) -> Result<(), Error> {
let mut headers = HeaderMap::new();
headers.insert("apikey", HeaderValue::from_str(&self.api_key)?);
headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?);
Expand Down Expand Up @@ -852,17 +823,17 @@ impl AuthClient {
/// ```
/// auth_client.logout(Some(LogoutScope::Global), session.access_token).await.unwrap();
/// ```
pub async fn logout<S: Into<String>>(
pub async fn logout(
&self,
scope: Option<LogoutScope>,
bearer_token: S,
bearer_token: &str,
) -> Result<(), Error> {
let mut headers = HeaderMap::new();
headers.insert("apikey", HeaderValue::from_str(&self.api_key)?);
headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?);
headers.insert(
AUTHORIZATION,
HeaderValue::from_str(&format!("Bearer {}", &bearer_token.into()))?,
HeaderValue::from_str(&format!("Bearer {}", bearer_token))?,
);

let body = serde_json::to_string(&scope)?;
Expand Down Expand Up @@ -929,17 +900,17 @@ impl AuthClient {
}

/// Get the project URL from an AuthClient
pub fn project_url(&self) -> &String {
pub fn project_url(&self) -> &str {
&self.project_url
}

/// Get the API Key from an AuthClient
pub fn api_key(&self) -> &String {
pub fn api_key(&self) -> &str {
&self.api_key
}

/// Get the JWT Secret from an AuthClient
pub fn jwt_secret(&self) -> &String {
pub fn jwt_secret(&self) -> &str {
&self.jwt_secret
}
}
40 changes: 20 additions & 20 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,28 +150,28 @@ pub enum LoginOptions {
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SignInWithEmailAndPasswordPayload {
pub(crate) email: String,
pub(crate) password: String,
pub struct SignInWithEmailAndPasswordPayload<'a> {
pub(crate) email: &'a str,
pub(crate) password: &'a str,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SignInWithPhoneAndPasswordPayload {
pub(crate) phone: String,
pub(crate) password: String,
pub struct SignInWithPhoneAndPasswordPayload<'a> {
pub(crate) phone: &'a str,
pub(crate) password: &'a str,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SignUpWithEmailAndPasswordPayload {
pub(crate) email: String,
pub(crate) password: String,
pub struct SignUpWithEmailAndPasswordPayload<'a> {
pub(crate) email: &'a str,
pub(crate) password: &'a str,
pub(crate) options: Option<SignUpWithPasswordOptions>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SignUpWithPhoneAndPasswordPayload {
pub(crate) phone: String,
pub(crate) password: String,
pub struct SignUpWithPhoneAndPasswordPayload<'a> {
pub(crate) phone: &'a str,
pub(crate) password: &'a str,
pub(crate) options: Option<SignUpWithPasswordOptions>,
}

Expand All @@ -188,8 +188,8 @@ pub struct SignUpWithPasswordOptions {
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RequestMagicLinkPayload {
pub(crate) email: String,
pub struct RequestMagicLinkPayload<'a> {
pub(crate) email: &'a str,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand All @@ -200,8 +200,8 @@ pub struct UpdateUserPayload {
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SendSMSOtpPayload {
pub phone: String,
pub struct SendSMSOtpPayload<'a> {
pub phone: &'a str,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -278,8 +278,8 @@ pub enum SignInWithOtp {
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Default)]
pub struct SignInWithEmailOtpPayload {
pub email: String,
pub struct SignInWithEmailOtpPayload<'a> {
pub email: &'a str,
pub options: Option<SignInEmailOtpParams>,
}

Expand Down Expand Up @@ -316,8 +316,8 @@ pub struct SignInMobileOtpParams {
}

#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RefreshSessionPayload {
pub refresh_token: String,
pub struct RefreshSessionPayload<'a> {
pub refresh_token: &'a str,
}

#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
Expand Down
Loading

0 comments on commit a218c35

Please sign in to comment.