Skip to content

Commit 2b1bd59

Browse files
authored
Improve examples (#31)
* Update online example * Fix string name * Make linter happy * Update examples and add cookie examples * Run cargo fmt * Implement feedback * Run cargo fmt
1 parent 402dddb commit 2b1bd59

File tree

4 files changed

+185
-25
lines changed

4 files changed

+185
-25
lines changed

examples/cookies_load.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use reqwest::cookie::CookieStore;
2+
use reqwest::header::HeaderValue;
3+
use std::str::FromStr;
4+
use std::sync::Arc;
5+
pub use vrchatapi::apis;
6+
use vrchatapi::models::EitherUserOrTwoFactor;
7+
8+
#[tokio::main]
9+
async fn main() {
10+
let mut config = apis::configuration::Configuration::default();
11+
config.basic_auth = Some((String::from("username"), Some(String::from("password"))));
12+
config.user_agent = Some(String::from("ProjectName/0.0.1 email@example.com"));
13+
14+
let mut jar = reqwest::cookie::Jar::default();
15+
jar.set_cookies(
16+
&mut [HeaderValue::from_str(
17+
&"auth=[AUTH_COOKIE_HERE], twoFactorAuth=[TWO_FACTOR_AUTH_COOKIE_HERE]",
18+
)
19+
.expect("Cookie not okay")]
20+
.iter(),
21+
&url::Url::from_str("https://api.vrchat.cloud").expect("Url not okay"),
22+
);
23+
let jar = Arc::new(jar);
24+
25+
config.client = reqwest::Client::builder()
26+
.cookie_store(true)
27+
.cookie_provider(jar)
28+
.build()
29+
.unwrap();
30+
31+
let user = apis::authentication_api::get_current_user(&config)
32+
.await
33+
.unwrap();
34+
35+
match user {
36+
EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name),
37+
EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid"),
38+
}
39+
}

examples/cookies_store.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use reqwest::cookie::CookieStore;
2+
use std::io::{self, Write};
3+
use std::str::FromStr;
4+
use url::Url;
5+
pub use vrchatapi::apis;
6+
use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode};
7+
8+
#[tokio::main]
9+
async fn main() {
10+
let mut config = apis::configuration::Configuration::default();
11+
config.basic_auth = Some((String::from("username"), Some(String::from("password"))));
12+
config.user_agent = Some(String::from("ProjectName/0.0.1 email@example.com"));
13+
14+
let cookie_store = std::sync::Arc::new(reqwest::cookie::Jar::default());
15+
config.client = reqwest::Client::builder()
16+
.cookie_store(true)
17+
.cookie_provider(cookie_store.clone())
18+
.build()
19+
.unwrap();
20+
21+
match apis::authentication_api::get_current_user(&config)
22+
.await
23+
.unwrap()
24+
{
25+
vrchatapi::models::EitherUserOrTwoFactor::CurrentUser(me) => {
26+
println!("Username: {}", me.username.unwrap())
27+
}
28+
vrchatapi::models::EitherUserOrTwoFactor::RequiresTwoFactorAuth(requires_auth) => {
29+
if requires_auth
30+
.requires_two_factor_auth
31+
.contains(&String::from("emailOtp"))
32+
{
33+
let code = read_user_input("Please enter your Email 2fa code: ");
34+
if let Err(err) = apis::authentication_api::verify2_fa_email_code(
35+
&config,
36+
TwoFactorEmailCode::new(code),
37+
)
38+
.await
39+
{
40+
eprintln!("Error verifying 2FA email code: {}", err);
41+
}
42+
} else {
43+
let code = read_user_input("Please enter your Authenticator 2fa code: ");
44+
if let Err(err) =
45+
apis::authentication_api::verify2_fa(&config, TwoFactorAuthCode::new(code))
46+
.await
47+
{
48+
eprintln!("Error verifying 2FA auth code: {}", err);
49+
}
50+
}
51+
}
52+
}
53+
54+
let user = apis::authentication_api::get_current_user(&config)
55+
.await
56+
.unwrap();
57+
58+
match user {
59+
EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name),
60+
EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid"),
61+
}
62+
63+
println!(
64+
"Cookie:{}",
65+
cookie_store
66+
.cookies(&Url::from_str("https://api.vrchat.cloud").expect("Url not okay"))
67+
.expect("Cookies not found")
68+
.to_str()
69+
.expect("Cookies not valid string")
70+
);
71+
}
72+
73+
fn read_user_input(prompt: &str) -> String {
74+
print!("{}", prompt);
75+
io::stdout().flush().expect("Failed to flush stdout");
76+
77+
let mut input = String::new();
78+
io::stdin()
79+
.read_line(&mut input)
80+
.expect("Failed to read line");
81+
82+
input.trim().to_string()
83+
}

examples/example.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use std::io::{self, Write};
2+
pub use vrchatapi::apis;
3+
use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode};
4+
5+
#[tokio::main]
6+
async fn main() {
7+
let mut config = apis::configuration::Configuration::default();
8+
config.basic_auth = Some((String::from("username"), Some(String::from("password"))));
9+
config.user_agent = Some(String::from("ProjectName/0.0.1 email@example.com"));
10+
match apis::authentication_api::get_current_user(&config)
11+
.await
12+
.unwrap()
13+
{
14+
vrchatapi::models::EitherUserOrTwoFactor::CurrentUser(me) => {
15+
println!("Username: {}", me.username.unwrap())
16+
}
17+
vrchatapi::models::EitherUserOrTwoFactor::RequiresTwoFactorAuth(requires_auth) => {
18+
if requires_auth
19+
.requires_two_factor_auth
20+
.contains(&String::from("emailOtp"))
21+
{
22+
let code = read_user_input("Please enter your Email 2fa code: ");
23+
if let Err(err) = apis::authentication_api::verify2_fa_email_code(
24+
&config,
25+
TwoFactorEmailCode::new(code),
26+
)
27+
.await
28+
{
29+
eprintln!("Error verifying 2FA email code: {}", err);
30+
}
31+
} else {
32+
let code = read_user_input("Please enter your Authenticator 2fa code: ");
33+
if let Err(err) =
34+
apis::authentication_api::verify2_fa(&config, TwoFactorAuthCode::new(code))
35+
.await
36+
{
37+
eprintln!("Error verifying 2FA auth code: {}", err);
38+
}
39+
}
40+
}
41+
}
42+
43+
let user = apis::authentication_api::get_current_user(&config)
44+
.await
45+
.unwrap();
46+
47+
match user {
48+
EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name),
49+
EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid"),
50+
}
51+
}
52+
53+
fn read_user_input(prompt: &str) -> String {
54+
print!("{}", prompt);
55+
io::stdout().flush().expect("Failed to flush stdout");
56+
57+
let mut input = String::new();
58+
io::stdin()
59+
.read_line(&mut input)
60+
.expect("Failed to read line");
61+
62+
input.trim().to_string()
63+
}

examples/online.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)