meilisearch_sdk/
key.rs

1use serde::{Deserialize, Serialize};
2use time::OffsetDateTime;
3
4use crate::{client::Client, errors::Error, request::HttpClient};
5
6/// Represents a [meilisearch key](https://www.meilisearch.com/docs/reference/api/keys#returned-fields).
7///
8/// You can get a [Key] from the [`Client::get_key`] method, or you can create a [Key] with the [`KeyBuilder::new`] or [`Client::create_key`] methods.
9#[derive(Debug, Serialize, Deserialize, Clone)]
10#[serde(rename_all = "camelCase")]
11pub struct Key {
12    #[serde(skip_serializing_if = "Vec::is_empty")]
13    pub actions: Vec<Action>,
14    #[serde(skip_serializing, with = "time::serde::rfc3339")]
15    pub created_at: OffsetDateTime,
16    pub description: Option<String>,
17    pub name: Option<String>,
18    #[serde(with = "time::serde::rfc3339::option")]
19    pub expires_at: Option<OffsetDateTime>,
20    #[serde(skip_serializing_if = "Vec::is_empty")]
21    pub indexes: Vec<String>,
22    #[serde(skip_serializing)]
23    pub key: String,
24    #[serde(skip_serializing)]
25    pub uid: String,
26    #[serde(skip_serializing, with = "time::serde::rfc3339")]
27    pub updated_at: OffsetDateTime,
28}
29
30impl Key {
31    /// Update the description of the [Key].
32    ///
33    /// # Example
34    ///
35    /// ```
36    /// # use meilisearch_sdk::{key::*, client::Client};
37    /// #
38    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
39    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
40    /// #
41    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
42    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
43    /// let description = "My not so little lovely test key".to_string();
44    /// let mut key = KeyBuilder::new()
45    ///     .with_action(Action::DocumentsAdd)
46    ///     .with_index("*")
47    ///     .with_description(&description)
48    ///     .execute(&client).await.unwrap();
49    ///
50    /// assert_eq!(key.description, Some(description));
51    /// # client.delete_key(key).await.unwrap();
52    /// # });
53    /// ```
54    pub fn with_description(&mut self, desc: impl AsRef<str>) -> &mut Key {
55        self.description = Some(desc.as_ref().to_string());
56        self
57    }
58
59    /// Update the name of the [Key].
60    ///
61    /// # Example
62    ///
63    /// ```
64    /// # use meilisearch_sdk::{key::*, client::Client};
65    /// #
66    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
67    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
68    /// #
69    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
70    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
71    /// let name = "lovely key".to_string();
72    /// let mut key = KeyBuilder::new()
73    ///     .with_action(Action::DocumentsAdd)
74    ///     .with_index("*")
75    ///     .execute(&client)
76    ///     .await
77    ///     .unwrap();
78    ///
79    /// key.with_name(&name);
80    ///
81    /// assert_eq!(key.name, Some(name));
82    /// # client.delete_key(key).await.unwrap();
83    /// # });
84    /// ```
85    pub fn with_name(&mut self, desc: impl AsRef<str>) -> &mut Key {
86        self.name = Some(desc.as_ref().to_string());
87        self
88    }
89
90    /// Update the [Key].
91    ///
92    /// # Example
93    ///
94    /// ```
95    /// # use meilisearch_sdk::{key::KeyBuilder, client::Client};
96    /// #
97    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
98    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
99    /// #
100    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
101    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
102    /// let mut key = KeyBuilder::new()
103    ///     .execute(&client)
104    ///     .await
105    ///     .unwrap();
106    ///
107    /// let description = "My not so little lovely test key".to_string();
108    /// key.with_description(&description);
109    ///
110    /// let key = key.update(&client).await.unwrap();
111    ///
112    /// assert_eq!(key.description, Some(description));
113    /// # client.delete_key(key).await.unwrap();
114    /// # });
115    /// ```
116    pub async fn update<Http: HttpClient>(&self, client: &Client<Http>) -> Result<Key, Error> {
117        // only send description and name
118        let mut key_update = KeyUpdater::new(self);
119
120        if let Some(ref description) = self.description {
121            key_update.with_description(description);
122        }
123        if let Some(ref name) = self.name {
124            key_update.with_name(name);
125        }
126
127        key_update.execute(client).await
128    }
129
130    /// Delete the [Key].
131    ///
132    /// # Example
133    ///
134    /// ```
135    /// # use meilisearch_sdk::{key::KeyBuilder, client::Client};
136    /// #
137    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
138    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
139    /// #
140    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
141    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
142    /// let mut key = KeyBuilder::new()
143    ///     .execute(&client).await.unwrap();
144    ///
145    /// client.delete_key(key).await.unwrap();
146    /// # });
147    /// ```
148    pub async fn delete<Http: HttpClient>(&self, client: &Client<Http>) -> Result<(), Error> {
149        client.delete_key(self).await
150    }
151}
152
153impl AsRef<str> for Key {
154    fn as_ref(&self) -> &str {
155        &self.key
156    }
157}
158
159impl AsRef<Key> for Key {
160    fn as_ref(&self) -> &Key {
161        self
162    }
163}
164
165#[derive(Debug, Serialize, Deserialize, Clone)]
166#[serde(rename_all = "camelCase")]
167pub struct KeyUpdater {
168    pub description: Option<String>,
169    pub name: Option<String>,
170    #[serde(skip_serializing)]
171    pub key: String,
172}
173
174impl KeyUpdater {
175    pub fn new(key_or_uid: impl AsRef<str>) -> KeyUpdater {
176        KeyUpdater {
177            description: None,
178            name: None,
179            key: key_or_uid.as_ref().to_string(),
180        }
181    }
182
183    /// Update the description of the [Key].
184    ///
185    /// # Example
186    ///
187    /// ```
188    /// # use meilisearch_sdk::{key::*, client::Client};
189    /// #
190    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
191    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
192    /// #
193    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
194    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
195    /// let mut new_key = KeyBuilder::new()
196    ///     .execute(&client)
197    ///     .await
198    ///     .unwrap();
199    ///
200    /// let description = "My not so little lovely test key".to_string();
201    /// let mut key_update = KeyUpdater::new(new_key)
202    ///     .with_description(&description)
203    ///     .execute(&client)
204    ///     .await
205    ///     .unwrap();
206    ///
207    /// assert_eq!(key_update.description, Some(description));
208    /// # client.delete_key(key_update).await.unwrap();
209    /// # });
210    /// ```
211    pub fn with_description(&mut self, desc: impl AsRef<str>) -> &mut KeyUpdater {
212        self.description = Some(desc.as_ref().to_string());
213        self
214    }
215
216    /// Update the name of the [Key].
217    ///
218    /// # Example
219    ///
220    /// ```
221    /// # use meilisearch_sdk::{key::*, client::Client};
222    /// #
223    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
224    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
225    /// #
226    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
227    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
228    /// let mut new_key = KeyBuilder::new()
229    ///     .execute(&client)
230    ///     .await
231    ///     .unwrap();
232    ///
233    /// let name = "lovely key".to_string();
234    /// let mut key_update = KeyUpdater::new(new_key)
235    ///     .with_name(&name)
236    ///     .execute(&client)
237    ///     .await
238    ///     .unwrap();
239    ///
240    /// assert_eq!(key_update.name, Some(name));
241    /// # client.delete_key(key_update).await.unwrap();
242    /// # });
243    /// ```
244    pub fn with_name(&mut self, desc: impl AsRef<str>) -> &mut KeyUpdater {
245        self.name = Some(desc.as_ref().to_string());
246        self
247    }
248
249    /// Update a [Key] using the [`KeyUpdater`].
250    ///
251    /// # Example
252    ///
253    /// ```
254    /// # use meilisearch_sdk::{key::*, client::Client};
255    /// #
256    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
257    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
258    /// #
259    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
260    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
261    /// let description = "My little lovely test key".to_string();
262    /// let key = KeyBuilder::new()
263    ///     .execute(&client).await.unwrap();
264    ///
265    /// let mut key_update = KeyUpdater::new(&key.key);
266    /// key_update.with_description(&description).execute(&client).await;
267    ///
268    /// assert_eq!(key_update.description, Some(description));
269    /// # client.delete_key(key).await.unwrap();
270    /// # });
271    /// ```
272    pub async fn execute<Http: HttpClient>(&self, client: &Client<Http>) -> Result<Key, Error> {
273        client.update_key(self).await
274    }
275}
276
277impl AsRef<str> for KeyUpdater {
278    fn as_ref(&self) -> &str {
279        &self.key
280    }
281}
282
283impl AsRef<KeyUpdater> for KeyUpdater {
284    fn as_ref(&self) -> &KeyUpdater {
285        self
286    }
287}
288
289#[derive(Debug, Serialize, Clone, Default)]
290#[serde(rename_all = "camelCase")]
291pub struct KeysQuery {
292    /// The number of documents to skip.
293    ///
294    /// If the value of the parameter `offset` is `n`, the `n` first documents (ordered by relevance) will not be returned.
295    /// This is helpful for pagination.
296    ///
297    /// Example: If you want to skip the first document, set offset to `1`.
298    #[serde(skip_serializing_if = "Option::is_none")]
299    pub offset: Option<usize>,
300    /// The maximum number of documents returned.
301    ///
302    /// If the value of the parameter `limit` is `n`, there will never be more than `n` documents in the response.
303    /// This is helpful for pagination.
304    ///
305    /// Example: If you don't want to get more than two documents, set limit to `2`.
306    ///
307    /// **Default: `20`**
308    #[serde(skip_serializing_if = "Option::is_none")]
309    pub limit: Option<usize>,
310}
311
312impl KeysQuery {
313    /// Create a [`KeysQuery`] with only a description.
314    ///
315    /// # Example
316    ///
317    /// ```
318    /// # use meilisearch_sdk::{key::KeysQuery};
319    /// let builder = KeysQuery::new();
320    /// ```
321    #[must_use]
322    pub fn new() -> KeysQuery {
323        Self::default()
324    }
325
326    /// Specify the offset.
327    ///
328    /// # Example
329    ///
330    /// ```
331    /// # use meilisearch_sdk::{key::*, client::Client};
332    /// #
333    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
334    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
335    /// #
336    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
337    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
338    /// let mut keys = KeysQuery::new()
339    ///     .with_offset(1)
340    ///     .execute(&client).await.unwrap();
341    ///
342    /// assert_eq!(keys.offset, 1);
343    /// # });
344    /// ```
345    pub fn with_offset(&mut self, offset: usize) -> &mut KeysQuery {
346        self.offset = Some(offset);
347        self
348    }
349
350    /// Specify the maximum number of keys to return.
351    ///
352    /// # Example
353    ///
354    /// ```
355    /// # use meilisearch_sdk::{key::*, client::Client};
356    /// #
357    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
358    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
359    /// #
360    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
361    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
362    /// let mut keys = KeysQuery::new()
363    ///     .with_limit(1)
364    ///     .execute(&client).await.unwrap();
365    ///
366    /// assert_eq!(keys.results.len(), 1);
367    /// # });
368    /// ```
369    pub fn with_limit(&mut self, limit: usize) -> &mut KeysQuery {
370        self.limit = Some(limit);
371        self
372    }
373
374    /// Get [Key]'s.
375    ///
376    /// # Example
377    ///
378    /// ```
379    /// # use meilisearch_sdk::{key::*, client::Client};
380    /// #
381    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
382    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
383    /// #
384    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
385    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
386    /// let mut keys = KeysQuery::new()
387    ///     .with_limit(1)
388    ///     .execute(&client).await.unwrap();
389    ///
390    /// assert_eq!(keys.results.len(), 1);
391    /// # });
392    /// ```
393    pub async fn execute<Http: HttpClient>(
394        &self,
395        client: &Client<Http>,
396    ) -> Result<KeysResults, Error> {
397        client.get_keys_with(self).await
398    }
399}
400
401/// The [`KeyBuilder`] is an analog to the [Key] type but without all the fields managed by Meilisearch.
402///
403/// It's used to create [Key].
404///
405/// # Example
406///
407/// ```
408/// # use meilisearch_sdk::{key::*, client::Client};
409/// #
410/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
411/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
412/// #
413/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
414/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
415/// let description = "My little lovely test key".to_string();
416/// let key = KeyBuilder::new()
417///     .with_description(&description)
418///     .execute(&client).await.unwrap();
419///
420/// assert_eq!(key.description, Some(description));
421/// # client.delete_key(key).await.unwrap();
422/// # });
423/// ```
424#[derive(Debug, Clone, Serialize, Default)]
425#[serde(rename_all = "camelCase")]
426pub struct KeyBuilder {
427    pub actions: Vec<Action>,
428    #[serde(skip_serializing_if = "Option::is_none")]
429    pub description: Option<String>,
430    #[serde(skip_serializing_if = "Option::is_none")]
431    pub name: Option<String>,
432    #[serde(skip_serializing_if = "Option::is_none")]
433    pub uid: Option<String>,
434    #[serde(with = "time::serde::rfc3339::option")]
435    pub expires_at: Option<OffsetDateTime>,
436    pub indexes: Vec<String>,
437}
438
439impl KeyBuilder {
440    /// Create a [`KeyBuilder`].
441    ///
442    /// # Example
443    ///
444    /// ```
445    /// # use meilisearch_sdk::key::KeyBuilder;
446    /// let builder = KeyBuilder::new();
447    /// ```
448    #[must_use]
449    pub fn new() -> KeyBuilder {
450        Self::default()
451    }
452
453    /// Declare a set of actions the [Key] will be able to execute.
454    ///
455    /// # Example
456    ///
457    /// ```
458    /// # use meilisearch_sdk::key::*;
459    /// let mut builder = KeyBuilder::new();
460    /// builder.with_actions(vec![Action::Search, Action::DocumentsAdd]);
461    /// ```
462    pub fn with_actions(&mut self, actions: impl IntoIterator<Item = Action>) -> &mut KeyBuilder {
463        self.actions.extend(actions);
464        self
465    }
466
467    /// Add one action the [Key] will be able to execute.
468    ///
469    /// # Example
470    ///
471    /// ```
472    /// # use meilisearch_sdk::key::*;
473    /// let mut builder = KeyBuilder::new();
474    /// builder.with_action(Action::DocumentsAdd);
475    /// ```
476    pub fn with_action(&mut self, action: Action) -> &mut KeyBuilder {
477        self.actions.push(action);
478        self
479    }
480
481    /// Set the expiration date of the [Key].
482    ///
483    /// # Example
484    ///
485    /// ```
486    /// # use meilisearch_sdk::key::KeyBuilder;
487    /// # use time::{OffsetDateTime, Duration};
488    /// let mut builder = KeyBuilder::new();
489    /// // create a key that expires in two weeks from now
490    /// builder.with_expires_at(OffsetDateTime::now_utc() + Duration::WEEK * 2);
491    /// ```
492    pub fn with_expires_at(&mut self, expires_at: OffsetDateTime) -> &mut KeyBuilder {
493        self.expires_at = Some(expires_at);
494        self
495    }
496
497    /// Set the indexes the [Key] can manage.
498    ///
499    /// # Example
500    ///
501    /// ```
502    /// # use meilisearch_sdk::{key::KeyBuilder, client::Client};
503    /// #
504    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
505    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
506    /// #
507    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
508    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
509    /// let mut key = KeyBuilder::new()
510    ///     .with_indexes(vec!["test", "movies"])
511    ///     .execute(&client)
512    ///     .await
513    ///     .unwrap();
514    ///
515    /// assert_eq!(vec!["test", "movies"], key.indexes);
516    /// # client.delete_key(key).await.unwrap();
517    /// # });
518    /// ```
519    pub fn with_indexes(
520        &mut self,
521        indexes: impl IntoIterator<Item = impl AsRef<str>>,
522    ) -> &mut KeyBuilder {
523        self.indexes = indexes
524            .into_iter()
525            .map(|index| index.as_ref().to_string())
526            .collect();
527        self
528    }
529
530    /// Add one index the [Key] can manage.
531    ///
532    /// # Example
533    ///
534    /// ```
535    /// # use meilisearch_sdk::key::KeyBuilder;
536    /// let mut builder = KeyBuilder::new();
537    /// builder.with_index("test");
538    /// ```
539    pub fn with_index(&mut self, index: impl AsRef<str>) -> &mut KeyBuilder {
540        self.indexes.push(index.as_ref().to_string());
541        self
542    }
543
544    /// Add a description to the [Key].
545    ///
546    /// # Example
547    ///
548    /// ```
549    /// # use meilisearch_sdk::{key::*, client::Client};
550    /// #
551    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
552    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
553    /// #
554    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
555    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
556    /// let description = "My not so little lovely test key".to_string();
557    /// let mut key = KeyBuilder::new()
558    ///     .with_description(&description)
559    ///     .execute(&client).await.unwrap();
560    ///
561    /// assert_eq!(key.description, Some(description));
562    /// # client.delete_key(key).await.unwrap();
563    /// # });
564    /// ```
565    pub fn with_description(&mut self, desc: impl AsRef<str>) -> &mut KeyBuilder {
566        self.description = Some(desc.as_ref().to_string());
567        self
568    }
569
570    /// Add a name to the [Key].
571    ///
572    /// # Example
573    ///
574    /// ```
575    /// # use meilisearch_sdk::{key::*, client::Client};
576    /// #
577    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
578    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
579    /// #
580    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
581    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
582    /// let name = "lovely key".to_string();
583    /// let mut key = KeyBuilder::new()
584    ///     .with_name(&name)
585    ///     .execute(&client).await.unwrap();
586    ///
587    /// assert_eq!(key.name, Some(name));
588    /// # client.delete_key(key).await.unwrap();
589    /// # });
590    /// ```
591    pub fn with_name(&mut self, desc: impl AsRef<str>) -> &mut KeyBuilder {
592        self.name = Some(desc.as_ref().to_string());
593        self
594    }
595
596    /// Add a uid to the [Key].
597    ///
598    /// # Example
599    ///
600    /// ```
601    /// # use meilisearch_sdk::{key::*, client::Client};
602    /// #
603    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
604    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
605    /// #
606    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
607    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
608    /// let uid = "93bcd7fb-2196-4fd9-acb7-3fca8a96e78f".to_string();
609    /// let mut key = KeyBuilder::new()
610    ///     .with_uid(&uid)
611    ///     .execute(&client).await.unwrap();
612    ///
613    /// assert_eq!(key.uid, uid);
614    /// # client.delete_key(key).await.unwrap();
615    /// # });
616    /// ```
617    pub fn with_uid(&mut self, desc: impl AsRef<str>) -> &mut KeyBuilder {
618        self.uid = Some(desc.as_ref().to_string());
619        self
620    }
621
622    /// Create a [Key] from the builder.
623    ///
624    /// # Example
625    ///
626    /// ```
627    /// # use meilisearch_sdk::{key::KeyBuilder, client::Client};
628    /// #
629    /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
630    /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
631    /// #
632    /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
633    /// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
634    /// let description = "My little lovely test key".to_string();
635    /// let key = KeyBuilder::new()
636    ///     .with_description(&description)
637    ///     .execute(&client).await.unwrap();
638    ///
639    /// assert_eq!(key.description, Some(description));
640    /// # client.delete_key(key).await.unwrap();
641    /// # });
642    /// ```
643    pub async fn execute<Http: HttpClient>(&self, client: &Client<Http>) -> Result<Key, Error> {
644        client.create_key(self).await
645    }
646}
647
648impl AsRef<KeyBuilder> for KeyBuilder {
649    fn as_ref(&self) -> &KeyBuilder {
650        self
651    }
652}
653
654#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
655pub enum Action {
656    /// Provides access to everything.
657    #[serde(rename = "*")]
658    All,
659    /// Provides access to both [`POST`](https://www.meilisearch.com/docs/reference/api/search#search-in-an-index-with-post-route) and [`GET`](https://www.meilisearch.com/docs/reference/api/search#search-in-an-index-with-get-route) search endpoints on authorized indexes.
660    #[serde(rename = "search")]
661    Search,
662    /// Provides access to the [add documents](https://www.meilisearch.com/docs/reference/api/documents#add-or-replace-documents) and [update documents](https://www.meilisearch.com/docs/reference/api/documents#add-or-update-documents) endpoints on authorized indexes.
663    #[serde(rename = "documents.add")]
664    DocumentsAdd,
665    /// Provides access to the [get one document](https://www.meilisearch.com/docs/reference/api/documents#get-one-document) and [get documents](https://www.meilisearch.com/docs/reference/api/documents#get-documents) endpoints on authorized indexes.
666    #[serde(rename = "documents.get")]
667    DocumentsGet,
668    /// Provides access to the [delete one document](https://www.meilisearch.com/docs/reference/api/documents#delete-one-document), [delete all documents](https://www.meilisearch.com/docs/reference/api/documents#delete-all-documents), and [batch delete](https://www.meilisearch.com/docs/reference/api/documents#delete-documents-by-batch) endpoints on authorized indexes.
669    #[serde(rename = "documents.delete")]
670    DocumentsDelete,
671    /// Provides access to the [create index](https://www.meilisearch.com/docs/reference/api/indexes#create-an-index) endpoint.
672    #[serde(rename = "indexes.create")]
673    IndexesCreate,
674    /// Provides access to the [get one index](https://www.meilisearch.com/docs/reference/api/indexes#get-one-index) and [list all indexes](https://www.meilisearch.com/docs/reference/api/indexes#list-all-indexes) endpoints. **Non-authorized `indexes` will be omitted from the response**.
675    #[serde(rename = "indexes.get")]
676    IndexesGet,
677    /// Provides access to the [update index](https://www.meilisearch.com/docs/reference/api/indexes#update-an-index) endpoint.
678    #[serde(rename = "indexes.update")]
679    IndexesUpdate,
680    /// Provides access to the [delete index](https://www.meilisearch.com/docs/reference/api/indexes#delete-an-index) endpoint.
681    #[serde(rename = "indexes.delete")]
682    IndexesDelete,
683    /// Provides access to the [get one task](https://www.meilisearch.com/docs/reference/api/tasks#get-task) and [get all tasks](https://www.meilisearch.com/docs/reference/api/tasks#get-all-tasks) endpoints. **Tasks from non-authorized `indexes` will be omitted from the response**. Also provides access to the [get one task by index](https://www.meilisearch.com/docs/reference/api/tasks#get-task-by-index) and [get all tasks by index](https://www.meilisearch.com/docs/reference/api/tasks#get-all-tasks-by-index) endpoints on authorized indexes.
684    #[serde(rename = "tasks.get")]
685    TasksGet,
686    /// Provides access to the [get settings](https://www.meilisearch.com/docs/reference/api/settings#get-settings) endpoint and equivalents for all subroutes on authorized indexes.
687    #[serde(rename = "settings.get")]
688    SettingsGet,
689    /// Provides access to the [update settings](https://www.meilisearch.com/docs/reference/api/settings#update-settings) and [reset settings](https://www.meilisearch.com/docs/reference/api/settings#reset-settings) endpoints and equivalents for all subroutes on authorized indexes.
690    #[serde(rename = "settings.update")]
691    SettingsUpdate,
692    /// Provides access to the [get stats of an index](https://www.meilisearch.com/docs/reference/api/stats#get-stats-of-an-index) endpoint and the [get stats of all indexes](https://www.meilisearch.com/docs/reference/api/stats#get-stats-of-all-indexes) endpoint. For the latter, **non-authorized `indexes` are omitted from the response**.
693    #[serde(rename = "stats.get")]
694    StatsGet,
695    /// Provides access to the [create dump](https://www.meilisearch.com/docs/reference/api/dump#create-a-dump) endpoint. **Not restricted by `indexes`.**
696    #[serde(rename = "dumps.create")]
697    DumpsCreate,
698    /// Provides access to the [get dump status](https://www.meilisearch.com/docs/reference/api/dump#get-dump-status) endpoint. **Not restricted by `indexes`.**
699    #[serde(rename = "dumps.get")]
700    DumpsGet,
701    /// Provides access to the [get Meilisearch version](https://www.meilisearch.com/docs/reference/api/version#get-version-of-meilisearch) endpoint.
702    #[serde(rename = "version")]
703    Version,
704    /// Provides access to the [get Key](https://www.meilisearch.com/docs/reference/api/keys#get-one-key) and [get Keys](https://www.meilisearch.com/docs/reference/api/keys#get-all-keys) endpoints.
705    #[serde(rename = "keys.get")]
706    KeyGet,
707    /// Provides access to the [create key](https://www.meilisearch.com/docs/reference/api/keys#create-a-key) endpoint.
708    #[serde(rename = "keys.create")]
709    KeyCreate,
710    /// Provides access to the [update key](https://www.meilisearch.com/docs/reference/api/keys#update-a-key) endpoint.
711    #[serde(rename = "keys.update")]
712    KeyUpdate,
713    /// Provides access to the [delete key](https://www.meilisearch.com/docs/reference/api/keys#delete-a-key) endpoint.
714    #[serde(rename = "keys.delete")]
715    KeyDelete,
716    /// Provides access to chat completions endpoints.
717    #[serde(rename = "chatCompletions")]
718    ChatCompletions,
719    /// Any other value that might be added to Meilisearch in the future but that is not supported by this SDK.
720    /// If you see one, please open a PR
721    #[serde(untagged)]
722    Unknown(String),
723}
724
725#[derive(Debug, Clone, Deserialize)]
726pub struct KeysResults {
727    pub results: Vec<Key>,
728    pub limit: u32,
729    pub offset: u32,
730}