Skip to content

Commit 18f08f9

Browse files
committed
Use generated structs in keys api
1 parent 7204ec3 commit 18f08f9

File tree

1 file changed

+22
-57
lines changed

1 file changed

+22
-57
lines changed

typesense/src/client/keys.rs

Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use hmac::{Hmac, Mac, NewMac};
66
use serde::{Deserialize, Serialize};
77
use sha2::Sha256;
8+
use typesense_codegen::models::{ApiKey, ApiKeySchema, ApiKeysResponse};
89

910
use super::Client;
1011
use crate::transport::HttpLowLevel;
@@ -26,9 +27,13 @@ where
2627
actions: Vec<Actions>,
2728
collections: Vec<String>,
2829
description: impl Into<Option<String>>,
29-
expires_at: impl Into<Option<usize>>,
30-
) -> crate::Result<ClientKeyCreate> {
31-
let create = Create {
30+
expires_at: impl Into<Option<i64>>,
31+
) -> crate::Result<ApiKey> {
32+
let actions = actions
33+
.into_iter()
34+
.map(|action| action.to_string())
35+
.collect();
36+
let create = ApiKeySchema {
3237
actions,
3338
collections,
3439
description: description.into(),
@@ -47,7 +52,7 @@ where
4752
/// Retrieve (metadata about) a key.
4853
///
4954
/// More info [here](https://typesense.org/docs/0.20.0/api/api-keys.html#retrieve-an-api-key).
50-
pub async fn retrieve(&self, n: usize) -> crate::Result<ClientKeyRetrieve> {
55+
pub async fn retrieve(&self, n: usize) -> crate::Result<ApiKey> {
5156
let response = self.client.get(format!("/keys/{}", n).as_str()).await?;
5257

5358
let body = response.into_body();
@@ -57,7 +62,7 @@ where
5762
/// Retrieve (metadata about) all keys.
5863
///
5964
/// More info [here](https://typesense.org/docs/0.20.0/api/api-keys.html#list-all-keys).
60-
pub async fn retrieve_all(&self) -> crate::Result<ClientKeyRetrieveAll> {
65+
pub async fn retrieve_all(&self) -> crate::Result<ApiKeysResponse> {
6166
let response = self.client.get("/keys").await?;
6267

6368
let body = response.into_body();
@@ -133,50 +138,18 @@ pub enum Actions {
133138
#[serde(rename = "*")]
134139
All,
135140
}
136-
137-
/// Structure returned by [`ClientKeys::create`] function.
138-
#[derive(Serialize, Deserialize)]
139-
pub struct ClientKeyCreate {
140-
/// Key ID
141-
pub id: usize,
142-
143-
/// Key Actions
144-
pub actions: Vec<Actions>,
145-
146-
/// Key Collections
147-
pub collections: Vec<String>,
148-
149-
/// Key Value
150-
pub value: String,
151-
152-
/// Key Description
153-
pub description: String,
154-
}
155-
156-
/// Structure returned by [`ClientKeys::retrieve`] function.
157-
#[derive(Serialize, Deserialize)]
158-
pub struct ClientKeyRetrieve {
159-
/// Key Actions
160-
pub actions: Vec<Actions>,
161-
162-
/// Key Collections
163-
pub collections: Vec<String>,
164-
165-
/// Key Description
166-
pub description: String,
167-
168-
/// Key ID
169-
pub id: usize,
170-
171-
/// Key Value Prefix
172-
pub value_prefix: String,
173-
}
174-
175-
/// Structure returned by [`ClientKeys::retrieve_all`] function.
176-
#[derive(Serialize, Deserialize)]
177-
pub struct ClientKeyRetrieveAll {
178-
/// Vector of all the Keys
179-
pub keys: Vec<ClientKeyRetrieve>,
141+
impl ToString for Actions {
142+
fn to_string(&self) -> String {
143+
match self {
144+
Self::DocumentsAll => "documents:*".to_string(),
145+
Self::DocumentsSearch => "documents:search".to_string(),
146+
Self::DocumentsGet => "documents:get".to_string(),
147+
Self::CollectionsAll => "collections:*".to_string(),
148+
Self::CollectionsDelete => "collections:delete".to_string(),
149+
Self::CollectionsCreate => "collections:create".to_string(),
150+
Self::All => "*".to_string(),
151+
}
152+
}
180153
}
181154

182155
/// Structure returned by [`ClientKeys::delete`] function.
@@ -186,14 +159,6 @@ pub struct ClientKeyDelete {
186159
pub id: usize,
187160
}
188161

189-
#[derive(Serialize)]
190-
struct Create {
191-
actions: Vec<Actions>,
192-
collections: Vec<String>,
193-
description: Option<String>,
194-
expires_at: Option<usize>,
195-
}
196-
197162
#[derive(Serialize)]
198163
struct GenerateScopedSearchKey {
199164
filter_by: String,

0 commit comments

Comments
 (0)