use crate::providers::web::base_api;
use rand::Rng;
use std::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RequestIdFactory {
Guid,
UnixMilliseconds,
}
impl RequestIdFactory {
pub fn create(self) -> String {
match self {
Self::Guid => random_hex_id(),
Self::UnixMilliseconds => base_api::unix_millis().to_string(),
}
}
}
fn random_hex_id() -> String {
use std::fmt::Write;
let mut rng = rand::rng();
let mut id = String::with_capacity(32);
for _ in 0..16 {
let _ = write!(id, "{:02x}", rng.random_range(0u8..=255));
}
id
}
#[derive(Debug, Clone)]
pub struct ApiOptions {
pub api_base_url: String,
pub app_id: String,
pub user_agent: Option<String>,
pub cookie: Option<String>,
pub timeout: Duration,
pub request_id_factory: RequestIdFactory,
}
impl Default for ApiOptions {
fn default() -> Self {
Self::new()
}
}
impl ApiOptions {
pub fn new() -> Self {
Self::android()
}
pub fn android() -> Self {
Self {
api_base_url: "https://apic.musixmatch.com/ws/1.1/".to_string(),
app_id: "android-player-v1.0".to_string(),
user_agent: Some("Dalvik/2.1.0 (Linux; U; Android 13)".to_string()),
cookie: Some("AWSELB=0; AWSELBCORS=0".to_string()),
timeout: Duration::from_secs(4),
request_id_factory: RequestIdFactory::Guid,
}
}
pub fn desktop() -> Self {
Self {
api_base_url: "https://apic-desktop.musixmatch.com/ws/1.1/".to_string(),
app_id: "web-desktop-app-v1.0".to_string(),
user_agent: Some(base_api::USER_AGENT.to_string()),
cookie: Some("AWSELB=0; AWSELBCORS=0".to_string()),
timeout: Duration::from_secs(4),
request_id_factory: RequestIdFactory::UnixMilliseconds,
}
}
pub fn use_android(&mut self) {
*self = Self::android();
}
pub fn use_desktop(&mut self) {
*self = Self::desktop();
}
pub fn use_mobile(&mut self) {
self.use_android();
}
}