use crate::{
DeviceProperties,
api::{
endpoint::Requester,
login::{AuthToken, checkin, toc, upload_device},
},
client::{Client, ClientRequester, async_doc},
error::{LoginError, PropsError},
};
pub struct ShortLivedClient {
props: DeviceProperties,
auth_token: AuthToken,
checkin_token: String,
config_token: String,
dfe_cookie: String,
gsf: i64,
}
impl ShortLivedClient {
#[doc = async_doc!(true, r#"let mut props_map = gpipipi::PropsMap::parse_aurora_config(PROPS)?;
let props = props_map.remove("arm").ok_or("No 'arm' device props!")?;
let client = gpipipi::ShortLivedClient::login(auth_token, props).await?;"#)]
pub async fn login<A: Into<String>>(
auth_token: A,
props: DeviceProperties,
) -> Result<Self, LoginError> {
let checkin_response = checkin(&props).await?;
let gsf = checkin_response
.android_id
.ok_or(LoginError::NoAndroidId())?
.cast_signed();
let checkin_token = checkin_response
.device_checkin_consistency_token
.ok_or(LoginError::NoDeviceToken())?;
let upload_response = upload_device(&props, gsf, &checkin_token).await?;
let config_token = upload_response
.upload_device_config_token
.ok_or(LoginError::NoConfigToken())?;
let auth_token = AuthToken::without_expire(auth_token.into());
let dfe_cookie = toc(&auth_token, gsf, &checkin_token, &props).await?;
Ok(Self {
props,
auth_token,
checkin_token,
config_token,
dfe_cookie,
gsf,
})
}
#[doc = async_doc!(true, r#"let client = gpipipi::ShortLivedClient::login(&auth_token, props).await?;
client.set_auth_token(auth_token);"#)]
pub fn set_auth_token<A: Into<String>>(&self, auth_token: A) {
self.auth_token.set(auth_token.into(), None);
}
}
impl ClientRequester for ShortLivedClient {
async fn requester(&self) -> Result<Requester<'_>, PropsError> {
Requester::new()
.checkin_token(&self.checkin_token)
.config_token(&self.config_token)
.auth_token(&self.auth_token)
.dfe_cookie(&self.dfe_cookie)
.props(&self.props)
.gsf(self.gsf)
.default_headers()
.await
}
}
impl Client for ShortLivedClient {}