use crate::ApiInfo;
use crate::client::OrdinaryApiClient;
use anyhow::bail;
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD as b64};
use uuid::Uuid;
pub async fn info(
api_client: &OrdinaryApiClient<'_>,
correlation_id: Option<String>,
) -> anyhow::Result<ApiInfo> {
let correlation_id = correlation_id.unwrap_or(
api_client
.correlation_id
.unwrap_or(Uuid::new_v4())
.to_string(),
);
let access_token = api_client
.get_access(None, Some(correlation_id.clone()))
.await?;
let info_req = api_client
.client
.get(format!("{}/v1/info", api_client.addr))
.header(
"Authorization",
format!("Bearer {}", b64.encode(&access_token)),
)
.header("x-correlation-id", correlation_id.clone());
let info_res = info_req.send().await?;
tracing::info!("fetched");
Ok(info_res.json().await?)
}
pub async fn set_lock(
api_client: &OrdinaryApiClient<'_>,
correlation_id: Option<String>,
account: &str,
lock: bool,
) -> anyhow::Result<()> {
let correlation_id = correlation_id.unwrap_or(
api_client
.correlation_id
.unwrap_or(Uuid::new_v4())
.to_string(),
);
let access_token = api_client
.get_access(None, Some(correlation_id.clone()))
.await?;
vec![("a", account)];
let lock_req = api_client
.client
.post(format!(
"{}/v1/{}",
api_client.addr,
if lock { "lock" } else { "unlock" }
))
.header(
"Authorization",
format!("Bearer {}", b64.encode(&access_token)),
)
.header("x-correlation-id", correlation_id.clone());
let lock_res = lock_req.send().await?;
if lock_res.status().is_success() {
tracing::info!("{}", if lock { "locked" } else { "unlocked" });
Ok(())
} else {
tracing::error!(status = %lock_res.status());
bail!("{:?}", lock_res.status());
}
}