From e1b656df02fc3b9d2043882073a4be007c4e5f0f Mon Sep 17 00:00:00 2001 From: qwxp Date: Sun, 2 Feb 2025 18:36:42 +0330 Subject: [PATCH 1/2] add Arc to Client --- src/client.rs | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/client.rs b/src/client.rs index 691f2f08..d018c922 100644 --- a/src/client.rs +++ b/src/client.rs @@ -19,11 +19,21 @@ use crate::{ /// The top-level struct of the SDK, representing a client containing [indexes](../indexes/struct.Index.html). #[derive(Debug, Clone)] pub struct Client { + pub(crate) inner:std::sync::Arc> +} + +#[derive(Debug)] +pub struct ClientInner { pub(crate) host: String, pub(crate) api_key: Option, pub(crate) http_client: Http, } - +impl std::ops::Deref for Client { + type Target = ClientInner; + fn deref(&self) -> &Self::Target { + &self.inner + } +} #[derive(Debug, Clone, Serialize, Deserialize)] pub struct SwapIndexes { pub indexes: (String, String), @@ -55,9 +65,11 @@ impl Client { let http_client = crate::reqwest::ReqwestClient::new(api_key.as_deref())?; Ok(Client { - host: host.into(), - api_key, - http_client, + inner: std::sync::Arc::new(ClientInner { + host: host.into(), + api_key, + http_client, + }) }) } } @@ -70,9 +82,11 @@ impl Client { http_client: Http, ) -> Client { Client { - host: host.into(), - api_key: api_key.map(|key| key.into()), - http_client, + inner: std::sync::Arc::new(ClientInner { + host: host.into(), + api_key: api_key.map(|key| key.into()), + http_client, + }) } } @@ -1353,7 +1367,7 @@ mod tests { let master_key = client.api_key.clone(); // create a new client with no right - let client = Client::new(client.host, Some(key.key.clone())).unwrap(); + let client = Client::new(client.host.clone(), Some(key.key.clone())).unwrap(); // with a wrong key let error = client.delete_key("invalid_key").await.unwrap_err(); insta::assert_snapshot!(error, @"Meilisearch auth: invalid_api_key: The provided API key is invalid.. https://docs.meilisearch.com/errors#invalid_api_key"); @@ -1378,7 +1392,7 @@ mod tests { )); // cleanup - let client = Client::new(client.host, master_key).unwrap(); + let client = Client::new(client.host.clone(), master_key).unwrap(); client.delete_key(key).await.unwrap(); } @@ -1446,7 +1460,7 @@ mod tests { // cleanup master_client - .delete_key(client.api_key.unwrap()) + .delete_key(client.api_key.clone().unwrap()) .await .unwrap(); } From 2af83517b827709bc4da664b680f2288754a5468 Mon Sep 17 00:00:00 2001 From: qwxp Date: Tue, 11 Feb 2025 13:09:25 +0330 Subject: [PATCH 2/2] fmt fix --- src/client.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/client.rs b/src/client.rs index d018c922..a84c4bd0 100644 --- a/src/client.rs +++ b/src/client.rs @@ -19,7 +19,7 @@ use crate::{ /// The top-level struct of the SDK, representing a client containing [indexes](../indexes/struct.Index.html). #[derive(Debug, Clone)] pub struct Client { - pub(crate) inner:std::sync::Arc> + pub(crate) inner: std::sync::Arc>, } #[derive(Debug)] @@ -69,7 +69,7 @@ impl Client { host: host.into(), api_key, http_client, - }) + }), }) } } @@ -86,7 +86,7 @@ impl Client { host: host.into(), api_key: api_key.map(|key| key.into()), http_client, - }) + }), } }