Skip to content

Commit b21ef5f

Browse files
committed
add subbacounts (RELEASEMINOR)(API2-709)
1 parent cee8d42 commit b21ef5f

File tree

5 files changed

+236
-1
lines changed

5 files changed

+236
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "voiceit2"
3-
version = "0.2.4"
3+
version = "0.3.0"
44
authors = ["gilgameshskytrooper <[email protected]>"]
55
description = """
66
A Rust wrapper for VoiceIt's API 2.0 featuring Voice + Face Verification and Identification.

src/client/mod.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,125 @@ impl VoiceIt2 {
195195
Ok(body)
196196
}
197197

198+
// SUBACCOUNTS
199+
pub fn create_managed_sub_account(
200+
&self,
201+
first_name: &str,
202+
last_name: &str,
203+
email: &str,
204+
password: &str,
205+
content_language: &str,
206+
) -> Result<String, VoiceItError> {
207+
let url = format!(
208+
"{}/subaccount/managed{}",
209+
String::from(BASE_URL),
210+
self.notification_url_parameter
211+
);
212+
213+
let form = multipart::Form::new()
214+
.text("firstName", String::from(first_name))
215+
.text("lastName", String::from(last_name))
216+
.text("email", String::from(email))
217+
.text("password", String::from(password))
218+
.text("contentLanguage", String::from(content_language));
219+
220+
let mut response = Client::new()
221+
.post(&url)
222+
.header("platformId", PLATFORM_ID)
223+
.header("platformVersion", PLATFORM_VERSION)
224+
.basic_auth(self.api_key.clone(), Some(self.api_token.clone()))
225+
.multipart(form)
226+
.send()?;
227+
228+
let mut body = String::new();
229+
response.read_to_string(&mut body)?;
230+
231+
Ok(body)
232+
}
233+
234+
pub fn create_unmanaged_sub_account(
235+
&self,
236+
first_name: &str,
237+
last_name: &str,
238+
email: &str,
239+
password: &str,
240+
content_language: &str,
241+
) -> Result<String, VoiceItError> {
242+
let url = format!(
243+
"{}/subaccount/unmanaged{}",
244+
String::from(BASE_URL),
245+
self.notification_url_parameter
246+
);
247+
248+
let form = multipart::Form::new()
249+
.text("firstName", String::from(first_name))
250+
.text("lastName", String::from(last_name))
251+
.text("email", String::from(email))
252+
.text("password", String::from(password))
253+
.text("contentLanguage", String::from(content_language));
254+
255+
let mut response = Client::new()
256+
.post(&url)
257+
.header("platformId", PLATFORM_ID)
258+
.header("platformVersion", PLATFORM_VERSION)
259+
.basic_auth(self.api_key.clone(), Some(self.api_token.clone()))
260+
.multipart(form)
261+
.send()?;
262+
263+
let mut body = String::new();
264+
response.read_to_string(&mut body)?;
265+
266+
Ok(body)
267+
}
268+
269+
pub fn regenerate_sub_account_api_token(
270+
&self,
271+
sub_account_api_key: &str,
272+
) -> Result<String, VoiceItError> {
273+
let url = format!(
274+
"{}/subaccount/{}{}",
275+
String::from(BASE_URL),
276+
String::from(sub_account_api_key),
277+
self.notification_url_parameter
278+
);
279+
280+
let mut response = Client::new()
281+
.post(&url)
282+
.header("platformId", PLATFORM_ID)
283+
.header("platformVersion", PLATFORM_VERSION)
284+
.basic_auth(self.api_key.clone(), Some(self.api_token.clone()))
285+
.send()?;
286+
287+
let mut body = String::new();
288+
response.read_to_string(&mut body)?;
289+
290+
Ok(body)
291+
}
292+
293+
pub fn delete_subaccount(
294+
&self,
295+
sub_account_api_key: &str,
296+
) -> Result<String, VoiceItError> {
297+
let url = format!(
298+
"{}/subaccount/{}{}",
299+
String::from(BASE_URL),
300+
String::from(sub_account_api_key),
301+
self.notification_url_parameter
302+
);
303+
304+
let mut response = Client::new()
305+
.delete(&url)
306+
.header("platformId", PLATFORM_ID)
307+
.header("platformVersion", PLATFORM_VERSION)
308+
.basic_auth(self.api_key.clone(), Some(self.api_token.clone()))
309+
.send()?;
310+
311+
let mut body = String::new();
312+
response.read_to_string(&mut body)?;
313+
314+
Ok(body)
315+
}
316+
198317
// GROUPS
199318

200319
pub fn create_group(&self, description: &str) -> Result<String, VoiceItError> {

src/lib.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ use structs::verification::{
4040
VideoVerificationReturn, VoiceVerificationByUrlReturn, VoiceVerificationReturn,
4141
};
4242

43+
#[allow(unused_imports)]
44+
use structs::subaccounts::{
45+
CreateSubAccountReturn,RegenerateSubAccountAPITokenReturn,DeleteSubAccountReturn,
46+
};
47+
48+
4349
use regex::Regex;
4450
use std::fs::File;
4551
use std::io::copy;
@@ -59,6 +65,11 @@ fn is_user_token(text: &str) -> bool {
5965
re.is_match(text)
6066
}
6167

68+
fn is_api_key(text: &str) -> bool {
69+
let re: Regex = Regex::new(r"^key_[a-zA-Z0-9]{32}$").unwrap();
70+
re.is_match(text)
71+
}
72+
6273
fn download_file(link: &str) -> Result<(), Box<dyn std::error::Error>> {
6374
let mut response = reqwest::get(link)?;
6475

@@ -267,6 +278,78 @@ mod tests {
267278
assert_eq!(&x.get_base_url(), "https://api.voiceit.io");
268279
}
269280

281+
#[test]
282+
fn test_subaccounts(){
283+
let env = std::env::var("BOXFUSE_ENV").unwrap();
284+
if env == "voiceittest" {
285+
let home_dir = std::env::var("HOME").unwrap();
286+
std::fs::write(
287+
format!("{}/platformVersion", home_dir),
288+
crate::client::PLATFORM_VERSION,
289+
)
290+
.expect("Unable to write platformVersion file");
291+
}
292+
293+
let x = crate::client::VoiceIt2::new(
294+
std::env::var("VIAPIKEY").unwrap(),
295+
std::env::var("VIAPITOKEN").unwrap(),
296+
);
297+
298+
let result: crate::structs::subaccounts::CreateSubAccountReturn = match &x.create_managed_sub_account("test", "rust", "", "", "") {
299+
Ok(x) => serde_json::from_str(&x).expect(&x),
300+
Err(err) => {
301+
panic!("Panic error: {:?}", err);
302+
}
303+
};
304+
305+
assert_eq!(result.status, 201);
306+
let sub_account_managed_api_key = result.apiKey;
307+
assert!(crate::is_api_key(&sub_account_managed_api_key));
308+
assert_eq!(result.responseCode, "SUCC");
309+
310+
let result: crate::structs::subaccounts::CreateSubAccountReturn = match &x.create_unmanaged_sub_account("test", "rust", "", "", "") {
311+
Ok(x) => serde_json::from_str(&x).expect(&x),
312+
Err(err) => {
313+
panic!("Panic error: {:?}", err);
314+
}
315+
};
316+
317+
assert_eq!(result.status, 201);
318+
let sub_account_unmanaged_api_key = result.apiKey;
319+
assert!(crate::is_api_key(&sub_account_unmanaged_api_key));
320+
assert_eq!(result.responseCode, "SUCC");
321+
322+
let result: crate::structs::subaccounts::RegenerateSubAccountAPITokenReturn = match &x.regenerate_sub_account_api_token(&sub_account_managed_api_key) {
323+
Ok(x) => serde_json::from_str(&x).expect(&x),
324+
Err(err) => {
325+
panic!("Panic error: {:?}", err);
326+
}
327+
};
328+
329+
assert_eq!(result.status, 200);
330+
assert_eq!(result.responseCode, "SUCC");
331+
332+
let result: crate::structs::subaccounts::DeleteSubAccountReturn = match &x.delete_subaccount(&sub_account_managed_api_key) {
333+
Ok(x) => serde_json::from_str(&x).expect(&x),
334+
Err(err) => {
335+
panic!("Panic error: {:?}", err);
336+
}
337+
};
338+
339+
assert_eq!(result.status, 200);
340+
assert_eq!(result.responseCode, "SUCC");
341+
342+
let result: crate::structs::subaccounts::DeleteSubAccountReturn = match &x.delete_subaccount(&sub_account_unmanaged_api_key) {
343+
Ok(x) => serde_json::from_str(&x).expect(&x),
344+
Err(err) => {
345+
panic!("Panic error: {:?}", err);
346+
}
347+
};
348+
349+
assert_eq!(result.status, 200);
350+
assert_eq!(result.responseCode, "SUCC");
351+
}
352+
270353
#[test]
271354
fn test_io() {
272355
println!("To be filled out later");

src/structs/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pub mod identification;
44
pub mod phrases;
55
pub mod users;
66
pub mod verification;
7+
pub mod subaccounts;

src/structs/subaccounts.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#![allow(non_snake_case)]
2+
3+
use serde::{Deserialize, Serialize};
4+
5+
#[derive(Serialize, Deserialize, Debug)]
6+
pub struct CreateSubAccountReturn {
7+
pub message: String,
8+
pub status: u16,
9+
pub timeTaken: String,
10+
pub password: String,
11+
pub apiToken: String,
12+
pub apiKey: String,
13+
pub email: String,
14+
pub responseCode: String,
15+
}
16+
17+
#[derive(Serialize, Deserialize, Debug)]
18+
pub struct RegenerateSubAccountAPITokenReturn {
19+
pub message: String,
20+
pub status: u16,
21+
pub timeTaken: String,
22+
pub apiToken: String,
23+
pub responseCode: String,
24+
}
25+
26+
#[derive(Serialize, Deserialize, Debug)]
27+
pub struct DeleteSubAccountReturn {
28+
pub message: String,
29+
pub status: u16,
30+
pub timeTaken: String,
31+
pub responseCode: String,
32+
}

0 commit comments

Comments
 (0)