From f7f1aa4bde6be9f80183cbf13e023c994b7f04fb Mon Sep 17 00:00:00 2001 From: Matthew Esposito Date: Thu, 8 Jun 2023 16:27:36 -0400 Subject: [PATCH] Abstract out random choosing --- src/oauth.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/oauth.rs b/src/oauth.rs index 9885a83..951ab75 100644 --- a/src/oauth.rs +++ b/src/oauth.rs @@ -157,7 +157,7 @@ impl Device { let uuid = uuid::Uuid::new_v4().to_string(); // Select random user agent from ANDROID_USER_AGENT - let android_user_agent = ANDROID_USER_AGENT[fastrand::usize(..ANDROID_USER_AGENT.len())].to_string(); + let android_user_agent = choose(&ANDROID_USER_AGENT).to_string(); // Android device headers let headers = HashMap::from([ @@ -178,10 +178,10 @@ impl Device { let uuid = uuid::Uuid::new_v4().to_string(); // Select random user agent from IOS_USER_AGENT - let ios_user_agent = IOS_USER_AGENT[fastrand::usize(..IOS_USER_AGENT.len())].to_string(); + let ios_user_agent = choose(&IOS_USER_AGENT).to_string(); // Select random iOS device from IOS_DEVICES - let ios_device = IOS_DEVICES[fastrand::usize(..IOS_DEVICES.len())].to_string(); + let ios_device = choose(&IOS_DEVICES).to_string(); // iOS device headers let headers = HashMap::from([ @@ -209,6 +209,12 @@ impl Device { } } +// Waiting on fastrand 2.0.0 for the `choose` function +// https://github.com/smol-rs/fastrand/pull/59/ +fn choose(list: &[T]) -> T { + list[fastrand::usize(..list.len())] +} + #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn test_oauth_client() { assert!(!OAUTH_CLIENT.read().await.token.is_empty());