use std::sync::Arc;
use std::sync::Mutex;
use super::RequestError;
use super::Value;
use crate::connection::Connection;
use crate::request::Request;
#[derive(Debug)]
pub struct LoginInfo {
pub customer_id: i32,
pub account_id: i32,
pub tfa: String,
pub builddate: String,
pub version: String,
}
pub struct Account {
pub(crate) conn: Arc<Mutex<Connection>>,
}
impl Account {
pub fn login(&self, user: &str, pass: &str) -> Result<LoginInfo, RequestError> {
const E: RequestError = RequestError::InvalidResponse;
let mut req = Request::new("account.login");
req.param("user", Value::from(user));
req.param("pass", Value::from(pass));
let res = self.conn.lock().unwrap().send(&req)?.ok_or(E)?;
if self.conn.lock().unwrap().cookies.get("domrobot").is_none() {
return Err(RequestError::LoginFailed);
}
Ok(LoginInfo {
customer_id: res.get("customerId").ok_or(E)?.as_i32().ok_or(E)?,
account_id: res.get("accountId").ok_or(E)?.as_i32().ok_or(E)?,
tfa: res.get("tfa").ok_or(E)?.as_str().ok_or(E)?.to_owned(),
builddate: res.get("builddate").ok_or(E)?.as_str().ok_or(E)?.to_owned(),
version: res.get("version").ok_or(E)?.as_str().ok_or(E)?.to_owned(),
})
}
pub fn logout(&self) -> Result<(), RequestError> {
self.conn
.lock()
.unwrap()
.send(&Request::new("account.logout"))?;
Ok(())
}
}