Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve examples #31

Merged
merged 7 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/cookies_load.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use reqwest::cookie::CookieStore;
use reqwest::header::HeaderValue;
use std::str::FromStr;
use std::sync::Arc;
pub use vrchatapi::apis;
use vrchatapi::models::EitherUserOrTwoFactor;

#[tokio::main]
async fn main() {
let mut config = apis::configuration::Configuration::default();
config.basic_auth = Some((String::from("username"), Some(String::from("password"))));
config.user_agent = Some(String::from("ProjectName/0.0.1 [email protected]"));

let mut jar = reqwest::cookie::Jar::default();
jar.set_cookies(
&mut [HeaderValue::from_str(
&"auth=[AUTH_COOKIE_HERE], twoFactorAuth=[TWO_FACTOR_AUTH_COOKIE_HERE]",
)
.expect("Cookie not okay")]
.iter(),
&url::Url::from_str("https://api.vrchat.cloud").expect("Url not okay"),
);
let jar = Arc::new(jar);

config.client = reqwest::Client::builder()
.cookie_store(true)
.cookie_provider(jar)
.build()
.unwrap();

let user = apis::authentication_api::get_current_user(&config)
.await
.unwrap();

match user {
EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name),
EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid"),
}
}
83 changes: 83 additions & 0 deletions examples/cookies_store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use reqwest::cookie::CookieStore;
use std::io::{self, Write};
use std::str::FromStr;
use url::Url;
pub use vrchatapi::apis;
use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode};

#[tokio::main]
async fn main() {
let mut config = apis::configuration::Configuration::default();
config.basic_auth = Some((String::from("username"), Some(String::from("password"))));
config.user_agent = Some(String::from("ProjectName/0.0.1 [email protected]"));

let cookie_store = std::sync::Arc::new(reqwest::cookie::Jar::default());
config.client = reqwest::Client::builder()
.cookie_store(true)
.cookie_provider(cookie_store.clone())
.build()
.unwrap();

match apis::authentication_api::get_current_user(&config)
.await
.unwrap()
{
vrchatapi::models::EitherUserOrTwoFactor::CurrentUser(me) => {
println!("Username: {}", me.username.unwrap())
}
vrchatapi::models::EitherUserOrTwoFactor::RequiresTwoFactorAuth(requires_auth) => {
if requires_auth
.requires_two_factor_auth
.contains(&String::from("emailOtp"))
{
let code = read_user_input("Please enter your Email 2fa code: ");
if let Err(err) = apis::authentication_api::verify2_fa_email_code(
&config,
TwoFactorEmailCode::new(code),
)
.await
{
eprintln!("Error verifying 2FA email code: {}", err);
}
} else {
let code = read_user_input("Please enter your Authenticator 2fa code: ");
if let Err(err) =
apis::authentication_api::verify2_fa(&config, TwoFactorAuthCode::new(code))
.await
{
eprintln!("Error verifying 2FA auth code: {}", err);
}
}
}
}

let user = apis::authentication_api::get_current_user(&config)
.await
.unwrap();

match user {
EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name),
EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid"),
}

println!(
"Cookie:{}",
cookie_store
.cookies(&Url::from_str("https://api.vrchat.cloud").expect("Url not okay"))
.expect("Cookies not found")
.to_str()
.expect("Cookies not valid string")
);
}

fn read_user_input(prompt: &str) -> String {
print!("{}", prompt);
io::stdout().flush().expect("Failed to flush stdout");

let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");

input.trim().to_string()
}
63 changes: 63 additions & 0 deletions examples/example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::io::{self, Write};
pub use vrchatapi::apis;
use vrchatapi::models::{EitherUserOrTwoFactor, TwoFactorAuthCode, TwoFactorEmailCode};

#[tokio::main]
async fn main() {
let mut config = apis::configuration::Configuration::default();
config.basic_auth = Some((String::from("username"), Some(String::from("password"))));
config.user_agent = Some(String::from("ProjectName/0.0.1 [email protected]"));
match apis::authentication_api::get_current_user(&config)
.await
.unwrap()
{
vrchatapi::models::EitherUserOrTwoFactor::CurrentUser(me) => {
println!("Username: {}", me.username.unwrap())
}
vrchatapi::models::EitherUserOrTwoFactor::RequiresTwoFactorAuth(requires_auth) => {
if requires_auth
.requires_two_factor_auth
.contains(&String::from("emailOtp"))
{
let code = read_user_input("Please enter your Email 2fa code: ");
if let Err(err) = apis::authentication_api::verify2_fa_email_code(
&config,
TwoFactorEmailCode::new(code),
)
.await
{
eprintln!("Error verifying 2FA email code: {}", err);
}
} else {
let code = read_user_input("Please enter your Authenticator 2fa code: ");
if let Err(err) =
apis::authentication_api::verify2_fa(&config, TwoFactorAuthCode::new(code))
.await
{
eprintln!("Error verifying 2FA auth code: {}", err);
}
}
}
}

let user = apis::authentication_api::get_current_user(&config)
.await
.unwrap();

match user {
EitherUserOrTwoFactor::CurrentUser(user) => println!("Current user: {}", user.display_name),
EitherUserOrTwoFactor::RequiresTwoFactorAuth(_) => println!("cookie invalid"),
}
}

fn read_user_input(prompt: &str) -> String {
print!("{}", prompt);
io::stdout().flush().expect("Failed to flush stdout");

let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");

input.trim().to_string()
}
25 changes: 0 additions & 25 deletions examples/online.rs

This file was deleted.

Loading