use std::time::Duration;
use serde_json::Value;
use crate::{
metrics::ClientObservation, percent_encode_path_segment, protocol_failure, Client, Error,
ProtocolFailure, RequestProtocol, Result, CONTROL_PLANE_VERSION,
};
const PROTOCOL_VERSION_HEADER: &str = "X-Durable-Workflow-Protocol-Version";
const CONTROL_PLANE_VERSION_HEADER: &str = "X-Durable-Workflow-Control-Plane-Version";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RawMethod {
Get,
Post,
Put,
Patch,
Delete,
}
impl RawMethod {
fn as_reqwest(self) -> reqwest::Method {
match self {
Self::Get => reqwest::Method::GET,
Self::Post => reqwest::Method::POST,
Self::Put => reqwest::Method::PUT,
Self::Patch => reqwest::Method::PATCH,
Self::Delete => reqwest::Method::DELETE,
}
}
}
#[derive(Clone, Debug)]
pub struct RawRequest {
method: RawMethod,
segments: Vec<String>,
query: Vec<(String, String)>,
body: Option<Value>,
timeout: Option<Duration>,
protocol_version: Option<String>,
control_plane_version: Option<String>,
}
impl RawRequest {
pub fn new<I, S>(method: RawMethod, segments: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self {
method,
segments: segments.into_iter().map(Into::into).collect(),
query: Vec::new(),
body: None,
timeout: None,
protocol_version: None,
control_plane_version: None,
}
}
pub fn get<I: IntoIterator<Item = S>, S: Into<String>>(segments: I) -> Self {
Self::new(RawMethod::Get, segments)
}
pub fn post<I: IntoIterator<Item = S>, S: Into<String>>(segments: I) -> Self {
Self::new(RawMethod::Post, segments)
}
pub fn put<I: IntoIterator<Item = S>, S: Into<String>>(segments: I) -> Self {
Self::new(RawMethod::Put, segments)
}
pub fn patch<I: IntoIterator<Item = S>, S: Into<String>>(segments: I) -> Self {
Self::new(RawMethod::Patch, segments)
}
pub fn delete<I: IntoIterator<Item = S>, S: Into<String>>(segments: I) -> Self {
Self::new(RawMethod::Delete, segments)
}
pub fn query(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.query.push((key.into(), value.into()));
self
}
pub fn json(mut self, body: Value) -> Self {
self.body = Some(body);
self
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
self
}
pub fn protocol_version(mut self, version: impl Into<String>) -> Self {
self.protocol_version = Some(version.into());
self
}
pub fn control_plane_version(mut self, version: impl Into<String>) -> Self {
self.control_plane_version = Some(version.into());
self
}
pub fn method(&self) -> RawMethod {
self.method
}
pub fn path(&self) -> Result<String> {
if self.segments.is_empty() {
return Err(invalid_request("at least one path segment is required"));
}
let mut path = String::new();
for segment in &self.segments {
if segment.is_empty() || segment == "." || segment == ".." {
return Err(invalid_request(
"path segments must not be empty, \".\" or \"..\"",
));
}
path.push('/');
path.push_str(&percent_encode_path_segment(segment));
}
Ok(path)
}
}
fn invalid_request(reason: &str) -> Error {
Error::InvalidRawRequest(reason.to_string())
}
#[derive(Clone, Debug)]
pub struct RawResponse {
status: u16,
headers: reqwest::header::HeaderMap,
body: Vec<u8>,
}
impl RawResponse {
pub fn status(&self) -> u16 {
self.status
}
pub fn is_success(&self) -> bool {
(200..300).contains(&self.status)
}
pub fn header(&self, name: &str) -> Option<&str> {
self.headers.get(name)?.to_str().ok()
}
pub fn headers(&self) -> impl Iterator<Item = (&str, &[u8])> {
self.headers
.iter()
.map(|(name, value)| (name.as_str(), value.as_bytes()))
}
pub fn protocol_version(&self) -> Option<&str> {
self.header(PROTOCOL_VERSION_HEADER)
}
pub fn control_plane_version(&self) -> Option<&str> {
self.header(CONTROL_PLANE_VERSION_HEADER)
}
pub fn body_bytes(&self) -> &[u8] {
&self.body
}
pub fn json(&self) -> Result<Value> {
if self.body.is_empty() {
return Ok(Value::Null);
}
Ok(serde_json::from_slice(&self.body)?)
}
pub fn refusal(&self) -> Option<HttpRefusal> {
if self.is_success() {
return None;
}
HttpRefusal::decode(self.status, serde_json::from_slice(&self.body).ok()?)
}
pub fn error_for_status(self) -> Result<Self> {
if self.is_success() {
return Ok(self);
}
let status = reqwest::StatusCode::from_u16(self.status)
.map_err(|_| invalid_request("response status is not a valid HTTP status"))?;
let body = String::from_utf8_lossy(&self.body).into_owned();
if let Some(failure) = protocol_failure(status, &body) {
return Err(Error::Protocol(failure));
}
Err(Error::Http { status, body })
}
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct HttpRefusal {
pub status: u16,
pub reason: Option<String>,
pub rejection_reason: Option<String>,
pub message: Option<String>,
pub error: Option<String>,
pub validation_errors: Option<Value>,
pub body: Value,
}
impl HttpRefusal {
fn decode(status: u16, body: Value) -> Option<Self> {
let object = body.as_object()?;
let text = |key: &str| object.get(key).and_then(Value::as_str).map(str::to_owned);
Some(Self {
status,
reason: text("reason"),
rejection_reason: text("rejection_reason"),
message: text("message"),
error: text("error"),
validation_errors: object
.get("validation_errors")
.or_else(|| object.get("errors"))
.cloned(),
body,
})
}
}
impl Error {
pub fn refusal(&self) -> Option<HttpRefusal> {
match self {
Self::Http { status, body } => {
HttpRefusal::decode(status.as_u16(), serde_json::from_str(body).ok()?)
}
Self::Protocol(failure) => HttpRefusal::decode(failure.status, failure.body.clone()),
_ => None,
}
}
}
pub fn worker_protocol_compatible(client: &str, server: &str) -> bool {
fn split(version: &str) -> Option<(u64, u64)> {
let (major, minor) = version.split_once('.')?;
let digits = |part: &str| !part.is_empty() && part.bytes().all(|b| b.is_ascii_digit());
if !digits(major) || !digits(minor) {
return None;
}
Some((major.parse().ok()?, minor.parse().ok()?))
}
match (split(client), split(server)) {
(Some(client), Some(server)) => client.0 == server.0 && client.1 <= server.1,
_ => client == server,
}
}
fn invalid_worker_response(status: u16, message: String, requested: &str, body: Value) -> Error {
Error::Protocol(ProtocolFailure {
status,
reason: "invalid_worker_protocol_response".to_string(),
message,
supported_version: None,
requested_version: Some(requested.to_string()),
body,
})
}
impl Client {
pub async fn control_plane_request(&self, request: RawRequest) -> Result<RawResponse> {
self.raw_request(request, RequestProtocol::ControlPlane)
.await
}
pub async fn worker_plane_request(&self, request: RawRequest) -> Result<RawResponse> {
self.raw_request(request, RequestProtocol::DefaultWorker)
.await
}
async fn raw_request(
&self,
request: RawRequest,
protocol: RequestProtocol,
) -> Result<RawResponse> {
let path = request.path()?;
let method = request.method.as_reqwest();
let mut observation = ClientObservation::new(self, &method, &path, protocol);
let result = self
.send_raw_request(request, method, &path, protocol, &observation)
.await;
observation.finish(&result);
result
}
async fn send_raw_request(
&self,
request: RawRequest,
method: reqwest::Method,
path: &str,
protocol: RequestProtocol,
observation: &ClientObservation,
) -> Result<RawResponse> {
let worker_version = request
.protocol_version
.clone()
.unwrap_or_else(|| self.worker_protocol_version.as_str().to_string());
let control_version = request
.control_plane_version
.as_deref()
.unwrap_or(CONTROL_PLANE_VERSION);
let auth_token = self.auth_token(protocol)?;
let mut builder = self
.http
.request(method, format!("{}/api{}", self.base_url, path))
.header(reqwest::header::ACCEPT, "application/json")
.header("X-Namespace", &self.namespace)
.header(PROTOCOL_VERSION_HEADER, &worker_version)
.header(CONTROL_PLANE_VERSION_HEADER, control_version);
if let Some(timeout) = request.timeout {
builder = builder.timeout(timeout);
}
if let Some(token) = auth_token {
builder = builder.bearer_auth(token);
}
if !request.query.is_empty() {
builder = builder.query(&request.query);
}
if let Some(body) = &request.body {
builder = builder.json(body);
}
let response = builder.send().await?;
let status = response.status();
observation.status(status);
let response = RawResponse {
status: status.as_u16(),
headers: response.headers().clone(),
body: response.bytes().await?.to_vec(),
};
if matches!(protocol, RequestProtocol::ControlPlane) || !response.is_success() {
return Ok(response);
}
let body = response.json().unwrap_or(Value::Null);
let echoed = response.protocol_version().map(str::trim);
if !echoed.is_some_and(|echoed| worker_protocol_compatible(&worker_version, echoed)) {
return Err(invalid_worker_response(
response.status,
format!(
"worker response version {} is not same-major compatible with {worker_version}",
echoed.unwrap_or("missing")
),
&worker_version,
body,
));
}
let body_version = body
.get("protocol_version")
.and_then(|version| match version {
Value::String(text) => Some(text.trim().to_string()),
Value::Number(number) => Some(number.to_string()),
_ => None,
});
if body_version.as_deref() != echoed {
return Err(invalid_worker_response(
response.status,
"worker response body protocol_version must equal the protocol header".into(),
&worker_version,
body,
));
}
Ok(response)
}
}