use std::time::Duration;
use reqwest::Client;
use crate::error::XboxError;
use crate::models::{PeopleSearchResponse, Xuid};
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(10);
pub(crate) async fn search_gamertag(
http: &Client,
peoplehub_base_url: &str,
authorization: &str,
gamertag: &str,
) -> Result<Xuid, XboxError> {
let sanitized = gamertag.replace('#', "%2523");
let url = format!(
"{peoplehub_base_url}/users/me/people/search/decoration/detail?q={sanitized}&maxItems=5"
);
let response = http
.get(&url)
.header("x-xbl-contract-version", "4")
.header("Accept-Language", "en-US")
.header("Accept", "application/json")
.header("Authorization", authorization)
.timeout(DEFAULT_TIMEOUT)
.send()
.await?;
if !response.status().is_success() {
return Err(XboxError::HttpStatus {
url,
status: response.status(),
});
}
let payload = response.json::<PeopleSearchResponse>().await?;
payload
.people
.into_iter()
.find(|person| person.gamertag.eq_ignore_ascii_case(gamertag))
.map(|person| Xuid(person.xuid))
.ok_or_else(|| XboxError::PersonNotFound(gamertag.to_string()))
}