Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tenant token api for v0.28.1 #318

Merged
merged 78 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from 76 commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
a37ffd3
Update task api v0.28.0
bidoubiwa Jul 7, 2022
b9ffd60
Update tests to be relevant with the structures where they are ran
bidoubiwa Jul 11, 2022
c527312
Update types in code-samples
bidoubiwa Jul 11, 2022
17d6c19
Remove clearAll task type
bidoubiwa Jul 11, 2022
ff0d350
Add index_uid as an optional in the Tasks
bidoubiwa Jul 11, 2022
9241a7b
Add comments on best builder implementation
bidoubiwa Jul 19, 2022
eb38d18
Add execute function on get_tasks
bidoubiwa Aug 2, 2022
d37be56
Update code samples
bidoubiwa Aug 3, 2022
61be5a5
Remove out of context comment
bidoubiwa Aug 3, 2022
7aa409b
Merge
bidoubiwa Aug 3, 2022
c968df3
Fix tests
bidoubiwa Aug 3, 2022
c21b18f
Fix pagination tests
bidoubiwa Aug 3, 2022
012f939
Update HTTP methods for v0.28.0
bidoubiwa Aug 3, 2022
0cb9e93
Fix clippy
bidoubiwa Aug 3, 2022
a485ba5
Merge branch 'update_task_api_for_v0.28.0' of github.com:meilisearch/…
bidoubiwa Aug 3, 2022
f86bff4
Remove comment of task tests since the tests are now sucesful
bidoubiwa Aug 3, 2022
734fd13
Fix doc tests
bidoubiwa Aug 3, 2022
e5821c4
Merge branch 'update_task_api_for_v0.28.0' of github.com:meilisearch/…
bidoubiwa Aug 3, 2022
11bfb7e
Update keys for v0.28.0
bidoubiwa Aug 4, 2022
6b70f5a
Fix get tasks inside index structure
bidoubiwa Aug 4, 2022
604e08d
Make description and name optional in keys
bidoubiwa Aug 8, 2022
67637c2
Fix none doc tests with new get_tasks api
bidoubiwa Aug 8, 2022
c2dcd92
Add from and limit in tasks params
bidoubiwa Aug 8, 2022
efff813
Add warning on failing test
bidoubiwa Aug 8, 2022
f0020b6
Merge branch 'update_task_api_for_v0.28.0' of https://github.com/meil…
bidoubiwa Aug 8, 2022
e0762b1
Merge
bidoubiwa Aug 8, 2022
c1796a3
Update keys design
bidoubiwa Aug 9, 2022
abc0f7b
Update task API
bidoubiwa Aug 9, 2022
eaef1ea
Remove useless comment
bidoubiwa Aug 9, 2022
bdf4f13
Merge branch 'update_task_api_for_v0.28.0' of https://github.com/meil…
bidoubiwa Aug 9, 2022
2441da1
Merge branch 'update_http_methods_for_v0.28.0' of https://github.com/…
bidoubiwa Aug 9, 2022
d506eae
Remove client as mandatory parameter for the keyUpdater
bidoubiwa Aug 10, 2022
c803fd5
Add doc and tests on doc
bidoubiwa Aug 10, 2022
cdc2a41
Fix docs tests on keys in client
bidoubiwa Aug 10, 2022
b2ba741
Fix docs tests
bidoubiwa Aug 10, 2022
24ff749
Remove dbg
bidoubiwa Aug 10, 2022
1b41330
Add with_uid filter
bidoubiwa Aug 10, 2022
f6f821d
Add new actions on key creation
bidoubiwa Aug 10, 2022
710b2d6
Remove new line at the start of docs
bidoubiwa Aug 10, 2022
54d29eb
Fix clippy errors
bidoubiwa Aug 10, 2022
2439778
Merge branch 'update_keys_for_v0.28.0' of https://github.com/meilisea…
bidoubiwa Aug 10, 2022
3989758
Rename type in Task structure
bidoubiwa Aug 10, 2022
668ba13
Removed useless newlines
bidoubiwa Aug 10, 2022
ac95329
Fix typo in comment
bidoubiwa Aug 10, 2022
66bd1a5
Add missing semi-column
bidoubiwa Aug 10, 2022
3adc25e
t checkout uMerge branch 'update_task_api_for_v0.28.0' of https://git…
bidoubiwa Aug 10, 2022
17f1ecf
Merge
bidoubiwa Aug 10, 2022
31b3941
Update indexes api for v0.28.0
bidoubiwa Aug 10, 2022
7865c1f
Merge
bidoubiwa Aug 22, 2022
027181b
Improve doc comments
bidoubiwa Aug 22, 2022
761a9df
Merge branch 'update_keys_for_v0.28.0' of github.com:meilisearch/meil…
bidoubiwa Aug 22, 2022
9b36f77
Change indexes methods
bidoubiwa Aug 23, 2022
335fbf1
Add index query and index updater
bidoubiwa Aug 23, 2022
7b5b5e2
Add documentation and doc tests on indexes routes
bidoubiwa Aug 24, 2022
3012f2a
Add tests on get indexes with params
bidoubiwa Aug 24, 2022
62e5f2c
Add test on index update
bidoubiwa Aug 24, 2022
555f7dd
Fix clippy
bidoubiwa Aug 24, 2022
e253823
update nb hits to estimated_nb_hits
bidoubiwa Aug 24, 2022
7c8d8a0
remove exhaustive facets count
bidoubiwa Aug 24, 2022
3d1682d
Change matches to show_matches_position
bidoubiwa Aug 24, 2022
de69f44
Change matches_info to matches_position
bidoubiwa Aug 24, 2022
f7d9862
Update search api for v0.28.1
bidoubiwa Aug 24, 2022
c6c2833
Improve comment syntax
bidoubiwa Aug 24, 2022
49ec3c5
Hide client creation in search query builder doc test
bidoubiwa Aug 24, 2022
c3f3939
Add DocumentsQuery and DocumentsResults
bidoubiwa Aug 24, 2022
33629b9
Resolve merge conflicts
bidoubiwa Aug 25, 2022
2062dce
Rollback wrong merging
bidoubiwa Aug 25, 2022
25fb8b5
Merge branch 'update_search_for_v0.28.0' of github.com:meilisearch/me…
bidoubiwa Aug 25, 2022
8c1bcba
Fix failing key tests
bidoubiwa Aug 25, 2022
405e0f4
Merge branch 'update_search_for_v0.28.0' of github.com:meilisearch/me…
bidoubiwa Aug 25, 2022
1499f7e
Merge branch 'bump-meilisearch-v0.28.0' of github.com:meilisearch/mei…
bidoubiwa Aug 25, 2022
4754825
Add documentation
bidoubiwa Aug 25, 2022
a262309
Fix execute doc test
bidoubiwa Aug 25, 2022
be22268
Fix clippy tests
bidoubiwa Aug 25, 2022
d60ac80
Update tenant token api for v0.28.1
bidoubiwa Aug 25, 2022
c1affa9
Fix clippy warning
bidoubiwa Aug 25, 2022
95f86a2
Remove public on TenantTokenClaim
bidoubiwa Aug 29, 2022
2d0979f
Fix merge conflict
bidoubiwa Aug 29, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ serde_json = "1.0"
time = { version = "0.3.7", features = ["serde-well-known", "formatting", "parsing"] }
jsonwebtoken = { version = "8", default-features = false }
yaup = "0.2.0"
uuid = { version = "1.1.2", features = ["v4"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
futures = "0.3"
Expand Down
6 changes: 4 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,19 +806,21 @@ impl Client {
/// #
/// # futures::executor::block_on(async move {
/// # let client = client::Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
/// let token = client.generate_tenant_token(serde_json::json!(["*"]), None, None).unwrap();
/// let api_key_uid = "76cf8b87-fd12-4688-ad34-260d930ca4f4".to_string();
/// let token = client.generate_tenant_token(api_key_uid, serde_json::json!(["*"]), None, None).unwrap();
/// let client = client::Client::new(MEILISEARCH_HOST, token);
/// # });
/// ```
pub fn generate_tenant_token(
&self,
api_key_uid: String,
search_rules: serde_json::Value,
api_key: Option<&str>,
expires_at: Option<OffsetDateTime>,
) -> Result<String, Error> {
let api_key = api_key.unwrap_or(&self.api_key);

crate::tenant_tokens::generate_tenant_token(search_rules, api_key, expires_at)
crate::tenant_tokens::generate_tenant_token(api_key_uid, search_rules, api_key, expires_at)
}
}

Expand Down
232 changes: 232 additions & 0 deletions src/documents.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
use crate::{errors::Error, indexes::Index};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize)]
pub struct DocumentsResults<T> {
pub results: Vec<T>,
pub limit: u32,
pub offset: u32,
pub total: u32,
}

#[derive(Debug, Clone, Serialize)]
pub struct DocumentsQuery<'a> {
#[serde(skip_serializing)]
pub index: &'a Index,

/// The number of documents to skip.
/// If the value of the parameter `offset` is `n`, the `n` first documents will not be returned.
/// This is helpful for pagination.
///
/// Example: If you want to skip the first document, set offset to `1`.
#[serde(skip_serializing_if = "Option::is_none")]
pub offset: Option<usize>,

/// The maximum number of documents returned.
/// If the value of the parameter `limit` is `n`, there will never be more than `n` documents in the response.
/// This is helpful for pagination.
///
/// Example: If you don't want to get more than two documents, set limit to `2`.
/// Default: `20`
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<usize>,

pub fields: Option<Vec<&'a str>>,
}

impl<'a> DocumentsQuery<'a> {
pub fn new(index: &Index) -> DocumentsQuery {
DocumentsQuery {
index,
offset: None,
limit: None,
fields: None,
}
}

/// Specify the offset.
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, documents::*};
/// #
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// # let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
///
/// let index = client.index("documents_query_offset");
///
/// let mut documents_query = DocumentsQuery::new(&index);
///
/// documents_query.with_offset(1);
/// ```
pub fn with_offset(&mut self, offset: usize) -> &mut DocumentsQuery<'a> {
self.offset = Some(offset);
self
}

/// Specify the limit.
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, documents::*};
/// #
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// # let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
///
/// let index = client.index("documents_query_offset");
///
/// let mut documents_query = DocumentsQuery::new(&index);
///
/// documents_query.with_offset(1);
/// ```
pub fn with_limit(&mut self, limit: usize) -> &mut DocumentsQuery<'a> {
self.limit = Some(limit);
self
}

/// Specify the fields to return in the documents.
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, documents::*};
/// #
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// # let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
/// let index = client.index("documents_query_offset");
///
/// let mut documents_query = DocumentsQuery::new(&index);
///
/// documents_query.with_fields(["title"]);
/// ```
pub fn with_fields(
&mut self,
fields: impl IntoIterator<Item = &'a str>,
) -> &mut DocumentsQuery<'a> {
self.fields = Some(fields.into_iter().collect());
self
}

/// Specify the limit.
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, documents::*};
/// # use serde::{Deserialize, Serialize};
/// #
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// # let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
///
/// # futures::executor::block_on(async move {
/// # let index = client.create_index("documents_query_execute", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap().try_make_index(&client).unwrap();
/// #[derive(Debug, Serialize, Deserialize, PartialEq)]
/// struct MyObject {
/// id: Option<usize>,
/// kind: String,
/// }
/// let index = client.index("documents_query_execute");
///
/// let mut documents_query = DocumentsQuery::new(&index);
///
/// documents_query.with_offset(1).execute::<MyObject>().await.unwrap();
/// # });
/// ```
pub async fn execute<T: DeserializeOwned + 'static>(
&self,
) -> Result<DocumentsResults<T>, Error> {
self.index.get_documents_with::<T>(self).await
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{client::*, indexes::*};
use meilisearch_test_macro::meilisearch_test;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct MyObject {
id: Option<usize>,
kind: String,
}

async fn setup_test_index(client: &Client, index: &Index) -> Result<(), Error> {
let t0 = index
.add_documents(
&[
MyObject {
id: Some(0),
kind: "text".into(),
},
MyObject {
id: Some(1),
kind: "text".into(),
},
MyObject {
id: Some(2),
kind: "title".into(),
},
MyObject {
id: Some(3),
kind: "title".into(),
},
],
None,
)
.await?;

t0.wait_for_completion(client, None, None).await?;

Ok(())
}

#[meilisearch_test]
async fn test_get_documents_with_execute(client: Client, index: Index) -> Result<(), Error> {
setup_test_index(&client, &index).await?;
// let documents = index.get_documents(None, None, None).await.unwrap();
let documents = DocumentsQuery::new(&index)
.with_limit(1)
.with_offset(1)
.with_fields(["kind"])
.execute::<MyObject>()
.await
.unwrap();

assert_eq!(documents.limit, 1);
assert_eq!(documents.offset, 1);
assert_eq!(documents.results.len(), 1);

Ok(())
}
#[meilisearch_test]
async fn test_get_documents_with_only_one_param(
client: Client,
index: Index,
) -> Result<(), Error> {
setup_test_index(&client, &index).await?;
// let documents = index.get_documents(None, None, None).await.unwrap();
let documents = DocumentsQuery::new(&index)
.with_limit(1)
.execute::<MyObject>()
.await
.unwrap();

assert_eq!(documents.limit, 1);
assert_eq!(documents.offset, 0);
assert_eq!(documents.results.len(), 1);

Ok(())
}
}
14 changes: 13 additions & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub enum Error {
HttpError(String),
// The library formating the query parameters encountered an error.
Yaup(yaup::Error),
// The library validating the format of an uuid.
Uuid(uuid::Error),
// Error thrown in case the version of the Uuid is not v4.
InvalidUuid4Version,
}

#[derive(Debug, Clone, Deserialize)]
Expand Down Expand Up @@ -75,6 +79,12 @@ impl From<yaup::Error> for Error {
}
}

impl From<uuid::Error> for Error {
fn from(error: uuid::Error) -> Error {
Error::Uuid(error)
}
}

/// The type of error that was encountered.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
Expand Down Expand Up @@ -194,7 +204,9 @@ impl std::fmt::Display for Error {
Error::TenantTokensInvalidApiKey => write!(fmt, "The provided api_key is invalid."),
Error::TenantTokensExpiredSignature => write!(fmt, "The provided expires_at is already expired."),
Error::InvalidTenantToken(e) => write!(fmt, "Impossible to generate the token, jsonwebtoken encountered an error: {}", e),
Error::Yaup(e) => write!(fmt, "Internal Error: could not parse the query parameters: {}", e)
Error::Yaup(e) => write!(fmt, "Internal Error: could not parse the query parameters: {}", e),
Error::Uuid(e) => write!(fmt, "The uid of the token has bit an uuid4 format: {}", e),
Error::InvalidUuid4Version => write!(fmt, "The uid provided to the token is not of version uuidv4")
}
}
}
Expand Down
Loading