Skip to content

Commit 538680a

Browse files
committed
refactor: optimize the api of RustyVault
1 parent 88d0a55 commit 538680a

1 file changed

Lines changed: 49 additions & 21 deletions

File tree

src/lib.rs

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -160,48 +160,63 @@ impl RustyVault {
160160
self.token.store(Arc::new(token.into()));
161161
}
162162

163-
pub async fn mount(&self, path: &str, mount_type: &str) -> Result<Option<Response>, RvError> {
163+
pub async fn mount<S: Into<String>>(
164+
&self,
165+
token: Option<S>,
166+
path: S,
167+
mount_type: S,
168+
) -> Result<Option<Response>, RvError> {
164169
let data = serde_json::json!({
165-
"type": mount_type,
170+
"type": mount_type.into(),
166171
})
167172
.as_object()
168173
.cloned();
169174

170-
self.write(format!("sys/mounts/{path}").as_str(), data).await
175+
self.write::<String>(token.map(|t| t.into()), format!("sys/mounts/{}", path.into()), data).await
171176
}
172177

173-
pub async fn unmount(&self, path: &str) -> Result<Option<Response>, RvError> {
174-
self.delete(format!("sys/mounts/{path}").as_str(), None).await
178+
pub async fn unmount<S: Into<String>>(&self, token: Option<S>, path: S) -> Result<Option<Response>, RvError> {
179+
self.delete::<String>(token.map(|t| t.into()), format!("sys/mounts/{}", path.into()), None).await
175180
}
176181

177-
pub async fn remount(&self, from: &str, to: &str) -> Result<Option<Response>, RvError> {
182+
pub async fn remount<S: Into<String>>(
183+
&self,
184+
token: Option<S>,
185+
from: S,
186+
to: S,
187+
) -> Result<Option<Response>, RvError> {
178188
let data = serde_json::json!({
179-
"from": from,
180-
"to": to,
189+
"from": from.into(),
190+
"to": to.into(),
181191
})
182192
.as_object()
183193
.cloned();
184194

185-
self.write("sys/remount", data).await
195+
self.write::<String>(token.map(|t| t.into()), "sys/remount".to_string(), data).await
186196
}
187197

188-
pub async fn enable_auth(&self, path: &str, auth_type: &str) -> Result<Option<Response>, RvError> {
198+
pub async fn enable_auth<S: Into<String>>(
199+
&self,
200+
token: Option<S>,
201+
path: S,
202+
auth_type: S,
203+
) -> Result<Option<Response>, RvError> {
189204
let data = serde_json::json!({
190-
"type": auth_type,
205+
"type": auth_type.into(),
191206
})
192207
.as_object()
193208
.cloned();
194209

195-
self.write(format!("sys/auth/{path}").as_str(), data).await
210+
self.write::<String>(token.map(|t| t.into()), format!("sys/auth/{}", path.into()), data).await
196211
}
197212

198-
pub async fn disable_auth(&self, path: &str) -> Result<Option<Response>, RvError> {
199-
self.delete(format!("sys/auth/{path}").as_str(), None).await
213+
pub async fn disable_auth<S: Into<String>>(&self, token: Option<S>, path: S) -> Result<Option<Response>, RvError> {
214+
self.delete::<String>(token.map(|t| t.into()), format!("sys/auth/{}", path.into()), None).await
200215
}
201216

202-
pub async fn login(
217+
pub async fn login<S: Into<String>>(
203218
&self,
204-
path: &str,
219+
path: S,
205220
data: Option<Map<String, Value>>,
206221
) -> Result<(Option<Response>, bool), RvError> {
207222
let mut login_success = false;
@@ -218,27 +233,40 @@ impl RustyVault {
218233
}
219234

220235
pub async fn request(&self, req: &mut Request) -> Result<Option<Response>, RvError> {
221-
req.client_token = self.token.load().as_ref().clone();
222236
self.core.load().handle_request(req).await
223237
}
224238

225-
pub async fn read(&self, path: &str) -> Result<Option<Response>, RvError> {
239+
pub async fn read<S: Into<String>>(&self, token: Option<S>, path: &str) -> Result<Option<Response>, RvError> {
226240
let mut req = Request::new_read_request(path);
241+
req.client_token = token.map(Into::into).unwrap_or_else(|| self.token.load().as_ref().clone());
227242
self.request(&mut req).await
228243
}
229244

230-
pub async fn write(&self, path: &str, data: Option<Map<String, Value>>) -> Result<Option<Response>, RvError> {
245+
pub async fn write<S: Into<String>>(
246+
&self,
247+
token: Option<S>,
248+
path: S,
249+
data: Option<Map<String, Value>>,
250+
) -> Result<Option<Response>, RvError> {
231251
let mut req = Request::new_write_request(path, data);
252+
req.client_token = token.map(Into::into).unwrap_or_else(|| self.token.load().as_ref().clone());
232253
self.request(&mut req).await
233254
}
234255

235-
pub async fn delete(&self, path: &str, data: Option<Map<String, Value>>) -> Result<Option<Response>, RvError> {
256+
pub async fn delete<S: Into<String>>(
257+
&self,
258+
token: Option<S>,
259+
path: S,
260+
data: Option<Map<String, Value>>,
261+
) -> Result<Option<Response>, RvError> {
236262
let mut req = Request::new_delete_request(path, data);
263+
req.client_token = token.map(Into::into).unwrap_or_else(|| self.token.load().as_ref().clone());
237264
self.request(&mut req).await
238265
}
239266

240-
pub async fn list(&self, path: &str) -> Result<Option<Response>, RvError> {
267+
pub async fn list<S: Into<String>>(&self, token: Option<S>, path: S) -> Result<Option<Response>, RvError> {
241268
let mut req = Request::new_list_request(path);
269+
req.client_token = token.map(Into::into).unwrap_or_else(|| self.token.load().as_ref().clone());
242270
self.request(&mut req).await
243271
}
244272
}

0 commit comments

Comments
 (0)