Skip to content

Commit 04ea0e0

Browse files
committed
Add support for new functions
1 parent c9501bf commit 04ea0e0

File tree

5 files changed

+134
-102
lines changed

5 files changed

+134
-102
lines changed

examples/cli-app-with-reqwest/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ impl HttpClient for ReqwestClient {
113113
body = "null".to_string();
114114
}
115115

116-
parse_response(status, expected_status_code, body)
116+
parse_response(status, expected_status_code, &body, url.to_string())
117+
// parse_response(status, expected_status_code, body)
117118
}
118119

119120
async fn stream_request<

src/documents.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ impl<'a, Http: HttpClient> DocumentsQuery<'a, Http> {
275275
self
276276
}
277277

278-
pub fn with_filter<'b>(&'b mut self, filter: &'a str) -> &'b mut DocumentsQuery<'a> {
278+
pub fn with_filter<'b>(&'b mut self, filter: &'a str) -> &'b mut DocumentsQuery<'a, Http> {
279279
self.filter = Some(filter);
280280
self
281281
}
@@ -318,25 +318,28 @@ impl<'a, Http: HttpClient> DocumentsQuery<'a, Http> {
318318
}
319319

320320
#[derive(Debug, Clone, Serialize)]
321-
pub struct DocumentDeletionQuery<'a> {
321+
pub struct DocumentDeletionQuery<'a, Http: HttpClient> {
322322
#[serde(skip_serializing)]
323-
pub index: &'a Index,
323+
pub index: &'a Index<Http>,
324324

325325
/// Filters to apply.
326326
///
327327
/// Read the [dedicated guide](https://docs.meilisearch.com/reference/features/filtering.html) to learn the syntax.
328328
pub filter: Option<&'a str>,
329329
}
330330

331-
impl<'a> DocumentDeletionQuery<'a> {
332-
pub fn new(index: &Index) -> DocumentDeletionQuery {
331+
impl<'a, Http: HttpClient> DocumentDeletionQuery<'a, Http> {
332+
pub fn new(index: &Index<Http>) -> DocumentDeletionQuery<Http> {
333333
DocumentDeletionQuery {
334334
index,
335335
filter: None,
336336
}
337337
}
338338

339-
pub fn with_filter<'b>(&'b mut self, filter: &'a str) -> &'b mut DocumentDeletionQuery<'a> {
339+
pub fn with_filter<'b>(
340+
&'b mut self,
341+
filter: &'a str,
342+
) -> &'b mut DocumentDeletionQuery<'a, Http> {
340343
self.filter = Some(filter);
341344
self
342345
}
@@ -349,9 +352,7 @@ impl<'a> DocumentDeletionQuery<'a> {
349352
#[cfg(test)]
350353
mod tests {
351354
use super::*;
352-
use crate::{
353-
client::Client, errors::Client, indexes::*, request::IsahcClient, request::IsahcClient,
354-
};
355+
use crate::{client::Client, errors::*, indexes::*, request::IsahcClient};
355356
use meilisearch_test_macro::meilisearch_test;
356357
use serde::{Deserialize, Serialize};
357358

@@ -445,7 +446,10 @@ mod tests {
445446
}
446447

447448
#[meilisearch_test]
448-
async fn test_delete_documents_with(client: Client, index: Index) -> Result<(), Error> {
449+
async fn test_delete_documents_with(
450+
client: Client<IsahcClient>,
451+
index: Index<IsahcClient>,
452+
) -> Result<(), Error> {
449453
setup_test_index(&client, &index).await?;
450454
index
451455
.set_filterable_attributes(["id"])
@@ -477,8 +481,8 @@ mod tests {
477481

478482
#[meilisearch_test]
479483
async fn test_delete_documents_with_filter_not_filterable(
480-
client: Client,
481-
index: Index,
484+
client: Client<IsahcClient>,
485+
index: Index<IsahcClient>,
482486
) -> Result<(), Error> {
483487
setup_test_index(&client, &index).await?;
484488

@@ -506,8 +510,8 @@ mod tests {
506510

507511
#[meilisearch_test]
508512
async fn test_get_documents_with_only_one_param(
509-
client: Client,
510-
index: Index,
513+
client: Client<IsahcClient>,
514+
index: Index<IsahcClient>,
511515
) -> Result<(), Error> {
512516
setup_test_index(&client, &index).await?;
513517
// let documents = index.get_documents(None, None, None).await.unwrap();
@@ -525,7 +529,10 @@ mod tests {
525529
}
526530

527531
#[meilisearch_test]
528-
async fn test_get_documents_with_filter(client: Client, index: Index) -> Result<(), Error> {
532+
async fn test_get_documents_with_filter(
533+
client: Client<IsahcClient>,
534+
index: Index<IsahcClient>,
535+
) -> Result<(), Error> {
529536
setup_test_index(&client, &index).await?;
530537

531538
index
@@ -579,8 +586,8 @@ mod tests {
579586

580587
#[meilisearch_test]
581588
async fn test_get_documents_with_error_hint_meilisearch_api_error(
582-
index: Index,
583-
client: Client,
589+
index: Index<IsahcClient>,
590+
client: Client<IsahcClient>,
584591
) -> Result<(), Error> {
585592
setup_test_index(&client, &index).await?;
586593

@@ -610,8 +617,8 @@ Hint: It might not be working because you're not up to date with the Meilisearch
610617

611618
#[meilisearch_test]
612619
async fn test_get_documents_with_invalid_filter(
613-
client: Client,
614-
index: Index,
620+
client: Client<IsahcClient>,
621+
index: Index<IsahcClient>,
615622
) -> Result<(), Error> {
616623
setup_test_index(&client, &index).await?;
617624

@@ -635,7 +642,10 @@ Hint: It might not be working because you're not up to date with the Meilisearch
635642
}
636643

637644
#[meilisearch_test]
638-
async fn test_settings_generated_by_macro(client: Client, index: Index) -> Result<(), Error> {
645+
async fn test_settings_generated_by_macro(
646+
client: Client<IsahcClient>,
647+
index: Index<IsahcClient>,
648+
) -> Result<(), Error> {
639649
setup_test_index(&client, &index).await?;
640650

641651
let movie_settings: Settings = MovieClips::generate_settings();

src/indexes.rs

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -483,35 +483,39 @@ impl<Http: HttpClient> Index<Http> {
483483
) -> Result<DocumentsResults<T>, Error> {
484484
if documents_query.filter.is_some() {
485485
let url = format!("{}/indexes/{}/documents/fetch", self.client.host, self.uid);
486-
return request::<(), &DocumentsQuery, DocumentsResults<T>>(
487-
&url,
488-
self.client.get_api_key(),
489-
Method::Post {
490-
body: documents_query,
491-
query: (),
492-
},
493-
200,
494-
)
495-
.await
496-
.map_err(|err| match err {
497-
Error::MeilisearchCommunication(error) => {
498-
Error::MeilisearchCommunication(MeilisearchCommunicationError {
499-
status_code: error.status_code,
500-
url: error.url,
501-
message: Some(format!("{}.", MEILISEARCH_VERSION_HINT)),
502-
})
503-
}
504-
Error::Meilisearch(error) => Error::Meilisearch(MeilisearchError {
505-
error_code: error.error_code,
506-
error_link: error.error_link,
507-
error_type: error.error_type,
508-
error_message: format!(
509-
"{}\n{}.",
510-
error.error_message, MEILISEARCH_VERSION_HINT
511-
),
512-
}),
513-
_ => err,
514-
});
486+
return self
487+
.client
488+
.http_client
489+
.clone()
490+
.request::<(), &DocumentsQuery<Http>, DocumentsResults<T>>(
491+
&url,
492+
self.client.get_api_key(),
493+
Method::Post {
494+
body: documents_query,
495+
query: (),
496+
},
497+
200,
498+
)
499+
.await
500+
.map_err(|err| match err {
501+
Error::MeilisearchCommunication(error) => {
502+
Error::MeilisearchCommunication(MeilisearchCommunicationError {
503+
status_code: error.status_code,
504+
url: error.url,
505+
message: Some(format!("{}.", MEILISEARCH_VERSION_HINT)),
506+
})
507+
}
508+
Error::Meilisearch(error) => Error::Meilisearch(MeilisearchError {
509+
error_code: error.error_code,
510+
error_link: error.error_link,
511+
error_type: error.error_type,
512+
error_message: format!(
513+
"{}\n{}.",
514+
error.error_message, MEILISEARCH_VERSION_HINT
515+
),
516+
}),
517+
_ => err,
518+
});
515519
}
516520

517521
let url = format!("{}/indexes/{}/documents", self.client.host, self.uid);
@@ -1036,18 +1040,21 @@ impl<Http: HttpClient> Index<Http> {
10361040
/// ```
10371041
pub async fn delete_documents_with(
10381042
&self,
1039-
query: &DocumentDeletionQuery<'_>,
1043+
query: &DocumentDeletionQuery<'_, Http>,
10401044
) -> Result<TaskInfo, Error> {
1041-
request::<(), &DocumentDeletionQuery, TaskInfo>(
1042-
&format!("{}/indexes/{}/documents/delete", self.client.host, self.uid),
1043-
self.client.get_api_key(),
1044-
Method::Post {
1045-
query: (),
1046-
body: query,
1047-
},
1048-
202,
1049-
)
1050-
.await
1045+
self.client
1046+
.http_client
1047+
.clone()
1048+
.request::<(), &DocumentDeletionQuery<Http>, TaskInfo>(
1049+
&format!("{}/indexes/{}/documents/delete", self.client.host, self.uid),
1050+
self.client.get_api_key(),
1051+
Method::Post {
1052+
query: (),
1053+
body: query,
1054+
},
1055+
202,
1056+
)
1057+
.await
10511058
}
10521059

10531060
/// Alias for the [`Index::update`] method.
@@ -1571,7 +1578,10 @@ impl<'a, Http: HttpClient> IndexUpdater<'a, Http> {
15711578
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
15721579
/// # });
15731580
/// ```
1574-
pub fn with_primary_key(&mut self, primary_key: impl AsRef<str>) -> &mut IndexUpdater<'a> {
1581+
pub fn with_primary_key(
1582+
&mut self,
1583+
primary_key: impl AsRef<str>,
1584+
) -> &mut IndexUpdater<'a, Http> {
15751585
self.primary_key = Some(primary_key.as_ref().to_string());
15761586
self
15771587
}

src/request.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ impl HttpClient for IsahcClient {
164164
body = "null".to_string();
165165
}
166166

167-
parse_response(status, expected_status_code, body)
167+
parse_response(status, expected_status_code, &body, url.to_string())
168+
// parse_response(status, expected_status_code, body)
168169
}
169170

170171
async fn stream_request<
@@ -262,7 +263,8 @@ impl HttpClient for IsahcClient {
262263
body = "null".to_string();
263264
}
264265

265-
parse_response(status, expected_status_code, body)
266+
parse_response(status, expected_status_code, &body, url.to_string())
267+
// parse_response(status, expected_status_code, body)
266268
}
267269
}
268270

@@ -363,9 +365,9 @@ impl HttpClient for WebSysClient {
363365

364366
if let Some(t) = text.as_string() {
365367
if t.is_empty() {
366-
parse_response(status, expected_status_code, String::from("null"))
368+
parse_response(status, expected_status_code, "null", url.to_string())
367369
} else {
368-
parse_response(status, expected_status_code, t)
370+
parse_response(status, expected_status_code, &t, url.to_string())
369371
}
370372
} else {
371373
error!("Invalid response");

0 commit comments

Comments
 (0)