Skip to content

Commit

Permalink
add Arc<Inner> to Client
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAliSalehi authored and curquiza committed Feb 10, 2025
1 parent 548ec19 commit e1b656d
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Http: HttpClient = DefaultHttpClient> {
pub(crate) inner:std::sync::Arc<ClientInner<Http>>
}

#[derive(Debug)]
pub struct ClientInner<Http: HttpClient = DefaultHttpClient> {
pub(crate) host: String,
pub(crate) api_key: Option<String>,
pub(crate) http_client: Http,
}

impl<Http: HttpClient> std::ops::Deref for Client<Http> {
type Target = ClientInner<Http>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SwapIndexes {
pub indexes: (String, String),
Expand Down Expand Up @@ -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,
})
})
}
}
Expand All @@ -70,9 +82,11 @@ impl<Http: HttpClient> Client<Http> {
http_client: Http,
) -> Client<Http> {
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,
})
}
}

Expand Down Expand Up @@ -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");
Expand All @@ -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();
}

Expand Down Expand Up @@ -1446,7 +1460,7 @@ mod tests {

// cleanup
master_client
.delete_key(client.api_key.unwrap())
.delete_key(client.api_key.clone().unwrap())
.await
.unwrap();
}
Expand Down

0 comments on commit e1b656d

Please sign in to comment.