use crate::auth::token_manager::TokenProvider;
use crate::dms::types::{
CompleteTaskRequest, FailTaskRequest, HeartbeatRequest, HeartbeatResponse, LeaseResponse,
};
use anyhow::{anyhow, Context, Result};
use reqwest::Client;
use reqwest::{
header::{HeaderMap, HeaderValue, AUTHORIZATION, CONTENT_TYPE},
StatusCode,
};
use std::sync::Arc;
use std::time::Duration;
use tracing::Level;
use url::Url;
use uuid::Uuid;
#[derive(Clone)]
pub struct DmsClient {
base: Url,
http: Client,
auth: Arc<dyn TokenProvider>,
}
impl DmsClient {
pub fn new(base: Url, timeout: Duration, auth: Arc<dyn TokenProvider>) -> Result<Self> {
let http = Client::builder()
.use_rustls_tls()
.timeout(timeout)
.build()
.context("build dms reqwest client")?;
Ok(Self { base, http, auth })
}
async fn auth_headers(&self) -> Result<HeaderMap> {
let mut h = HeaderMap::new();
let b = self
.auth
.bearer()
.await
.map_err(|e| anyhow!("token provider: {e}"))?;
let token = format!("Bearer {}", b);
let mut v = HeaderValue::from_str(&token)
.unwrap_or_else(|_| HeaderValue::from_static("Bearer INVALID"));
v.set_sensitive(true);
h.insert(AUTHORIZATION, v);
Ok(h)
}
fn join_segments(&self, segments: &[&str]) -> Result<Url> {
let mut url = self.base.clone();
url.path_segments_mut()
.map_err(|_| anyhow!("invalid DMS base URL; cannot be a base"))?
.extend(segments.iter().copied());
Ok(url)
}
pub async fn lease_by_capability(&self, _capability: &str) -> Result<Option<LeaseResponse>> {
let url = self.join_segments(&["tasks"]).context("join /tasks")?;
if tracing::enabled!(Level::DEBUG) {
tracing::debug!(
endpoint = %url,
"Sending DMS lease request"
);
}
let mut headers = self.auth_headers().await?;
let mut res = self
.http
.get(url.clone())
.headers(headers.clone())
.send()
.await
.context("send GET /tasks")?;
let mut status = res.status();
let mut bytes = res.bytes().await.context("read lease body")?;
if status == StatusCode::UNAUTHORIZED {
tracing::warn!(
status = %status,
"DMS lease unauthorized; refreshing token and retrying"
);
self.auth.on_unauthorized().await;
headers = self.auth_headers().await?;
res = self
.http
.get(url)
.headers(headers)
.send()
.await
.context("retry GET /tasks")?;
status = res.status();
bytes = res.bytes().await.context("read lease body (retry)")?;
}
if status == StatusCode::NO_CONTENT {
tracing::debug!("DMS lease returned 204 (no work available)");
return Ok(None);
}
if status == StatusCode::CONFLICT {
tracing::debug!(
status = %status,
"DMS lease returned conflict (busy); treating as no work"
);
return Ok(None);
}
if !status.is_success() {
tracing::warn!(
status = %status,
"DMS lease request returned non-success status"
);
return Err(anyhow!("/tasks status: {}", status));
}
let lease: LeaseResponse = serde_json::from_slice(&bytes)
.map_err(|err| {
tracing::error!(
status = %status,
error = %err,
"Failed to decode DMS lease response"
);
err
})
.context("decode lease")?;
if tracing::enabled!(Level::DEBUG) {
tracing::debug!(
status = %status,
task_id = %lease.task.id,
capability = %lease.task.capability,
access_token_updated = lease.access_token.is_some(),
"Decoded DMS lease response"
);
}
Ok(Some(lease))
}
pub async fn complete(&self, task_id: Uuid, body: &CompleteTaskRequest) -> Result<()> {
let url = self
.join_segments(&["tasks", &task_id.to_string(), "complete"])
.context("join /complete")?;
let mut headers = self.auth_headers().await?;
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
if tracing::enabled!(Level::DEBUG) {
tracing::debug!(
endpoint = %url,
task_id = %task_id,
"Sending DMS complete request"
);
}
let mut res = self
.http
.post(url.clone())
.headers(headers.clone())
.json(body)
.send()
.await
.context("send POST /complete")?;
let mut status = res.status();
let _ = res.bytes().await;
if status == StatusCode::UNAUTHORIZED {
tracing::warn!(
status = %status,
task_id = %task_id,
"DMS complete unauthorized; refreshing token and retrying"
);
self.auth.on_unauthorized().await;
headers = self.auth_headers().await?;
res = self
.http
.post(url)
.headers(headers)
.json(body)
.send()
.await
.context("retry POST /complete")?;
status = res.status();
let _ = res.bytes().await;
}
if tracing::enabled!(Level::DEBUG) {
tracing::debug!(
status = %status,
task_id = %task_id,
"DMS complete response"
);
}
if !status.is_success() {
tracing::error!(
status = %status,
task_id = %task_id,
"DMS complete endpoint returned non-success status"
);
return Err(anyhow!("POST /tasks/{task_id}/complete status {status}"));
}
Ok(())
}
pub async fn fail(&self, task_id: Uuid, body: &FailTaskRequest) -> Result<()> {
let url = self
.join_segments(&["tasks", &task_id.to_string(), "fail"])
.context("join /fail")?;
let mut headers = self.auth_headers().await?;
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
if tracing::enabled!(Level::DEBUG) {
tracing::debug!(
endpoint = %url,
task_id = %task_id,
"Sending DMS fail request"
);
}
let mut res = self
.http
.post(url.clone())
.headers(headers.clone())
.json(body)
.send()
.await
.context("send POST /fail")?;
let mut status = res.status();
let _ = res.bytes().await;
if status == StatusCode::UNAUTHORIZED {
tracing::warn!(
status = %status,
task_id = %task_id,
"DMS fail unauthorized; refreshing token and retrying"
);
self.auth.on_unauthorized().await;
headers = self.auth_headers().await?;
res = self
.http
.post(url)
.headers(headers)
.json(body)
.send()
.await
.context("retry POST /fail")?;
status = res.status();
let _ = res.bytes().await;
}
if tracing::enabled!(Level::DEBUG) {
tracing::debug!(
status = %status,
task_id = %task_id,
"DMS fail response"
);
}
if !status.is_success() {
tracing::error!(
status = %status,
task_id = %task_id,
"DMS fail endpoint returned non-success status"
);
return Err(anyhow!("POST /tasks/{task_id}/fail status {status}"));
}
Ok(())
}
pub async fn heartbeat(
&self,
task_id: Uuid,
body: &HeartbeatRequest,
) -> Result<HeartbeatResponse> {
let url = self
.join_segments(&["tasks", &task_id.to_string(), "heartbeat"])
.context("join /heartbeat")?;
let mut headers = self.auth_headers().await?;
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
if tracing::enabled!(Level::DEBUG) {
tracing::debug!(
endpoint = %url,
task_id = %task_id,
"Sending DMS heartbeat request"
);
}
let mut res = self
.http
.post(url.clone())
.headers(headers.clone())
.json(body)
.send()
.await
.context("send POST /heartbeat")?;
let mut status = res.status();
let mut bytes = res.bytes().await.context("read heartbeat response body")?;
if status == StatusCode::UNAUTHORIZED {
tracing::warn!(
status = %status,
task_id = %task_id,
"DMS heartbeat unauthorized; refreshing token and retrying"
);
self.auth.on_unauthorized().await;
headers = self.auth_headers().await?;
res = self
.http
.post(url)
.headers(headers)
.json(body)
.send()
.await
.context("retry POST /heartbeat")?;
status = res.status();
bytes = res
.bytes()
.await
.context("read heartbeat response body (retry)")?;
}
if !status.is_success() {
tracing::warn!(
status = %status,
task_id = %task_id,
"DMS heartbeat endpoint returned non-success status"
);
return Err(anyhow!("POST /tasks/{task_id}/heartbeat status {status}"));
}
let hb = serde_json::from_slice::<HeartbeatResponse>(&bytes)
.map_err(|err| {
tracing::error!(
status = %status,
task_id = %task_id,
error = %err,
"Failed to decode DMS heartbeat response"
);
err
})
.context("decode heartbeat response")?;
if tracing::enabled!(Level::DEBUG) {
tracing::debug!(
status = %status,
task_id = %task_id,
access_token_updated = hb.access_token.is_some(),
cancel = ?hb.cancel,
"Decoded DMS heartbeat response"
);
}
Ok(hb)
}
}