Skip to content

Commit 034ff6d

Browse files
MrAliSalehicurquiza
authored andcommitted
add Arc<Inner> to Client
1 parent 8575e24 commit 034ff6d

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/client.rs

+24-10
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,21 @@ use crate::{
1919
/// The top-level struct of the SDK, representing a client containing [indexes](../indexes/struct.Index.html).
2020
#[derive(Debug, Clone)]
2121
pub struct Client<Http: HttpClient = DefaultHttpClient> {
22+
pub(crate) inner:std::sync::Arc<ClientInner<Http>>
23+
}
24+
25+
#[derive(Debug)]
26+
pub struct ClientInner<Http: HttpClient = DefaultHttpClient> {
2227
pub(crate) host: String,
2328
pub(crate) api_key: Option<String>,
2429
pub(crate) http_client: Http,
2530
}
26-
31+
impl<Http: HttpClient> std::ops::Deref for Client<Http> {
32+
type Target = ClientInner<Http>;
33+
fn deref(&self) -> &Self::Target {
34+
&self.inner
35+
}
36+
}
2737
#[derive(Debug, Clone, Serialize, Deserialize)]
2838
pub struct SwapIndexes {
2939
pub indexes: (String, String),
@@ -55,9 +65,11 @@ impl Client {
5565
let http_client = crate::reqwest::ReqwestClient::new(api_key.as_deref())?;
5666

5767
Ok(Client {
58-
host: host.into(),
59-
api_key,
60-
http_client,
68+
inner: std::sync::Arc::new(ClientInner {
69+
host: host.into(),
70+
api_key,
71+
http_client,
72+
})
6173
})
6274
}
6375
}
@@ -70,9 +82,11 @@ impl<Http: HttpClient> Client<Http> {
7082
http_client: Http,
7183
) -> Client<Http> {
7284
Client {
73-
host: host.into(),
74-
api_key: api_key.map(|key| key.into()),
75-
http_client,
85+
inner: std::sync::Arc::new(ClientInner {
86+
host: host.into(),
87+
api_key: api_key.map(|key| key.into()),
88+
http_client,
89+
})
7690
}
7791
}
7892

@@ -1353,7 +1367,7 @@ mod tests {
13531367
let master_key = client.api_key.clone();
13541368

13551369
// create a new client with no right
1356-
let client = Client::new(client.host, Some(key.key.clone())).unwrap();
1370+
let client = Client::new(client.host.clone(), Some(key.key.clone())).unwrap();
13571371
// with a wrong key
13581372
let error = client.delete_key("invalid_key").await.unwrap_err();
13591373
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 {
13781392
));
13791393

13801394
// cleanup
1381-
let client = Client::new(client.host, master_key).unwrap();
1395+
let client = Client::new(client.host.clone(), master_key).unwrap();
13821396
client.delete_key(key).await.unwrap();
13831397
}
13841398

@@ -1446,7 +1460,7 @@ mod tests {
14461460

14471461
// cleanup
14481462
master_client
1449-
.delete_key(client.api_key.unwrap())
1463+
.delete_key(client.api_key.clone().unwrap())
14501464
.await
14511465
.unwrap();
14521466
}

0 commit comments

Comments
 (0)