use std::time::Duration;
use http::StatusCode;
use serde::{de::DeserializeOwned, Serialize};
use serde_json::Value;
use tracing_error::SpanTrace;
use crate::error::ApiError;
#[derive(Debug, Serialize)]
#[serde(untagged)]
pub enum Response {
Raw(Value),
Auth(AuthResponse),
Lease(LeaseResponse),
}
#[derive(Debug, Serialize)]
pub struct LeaseResponse {
pub revoke: LeaseRenewRevokeEndpoint,
pub renew: LeaseRenewRevokeEndpoint,
pub data: Value,
#[serde(with = "humantime_serde")]
pub ttl: Option<Duration>,
}
#[derive(Debug, Serialize)]
pub struct LeaseRenewRevokeEndpoint {
pub path: String,
pub data: Value,
}
#[derive(Debug, Serialize)]
pub struct AuthResponse {
pub alias: String,
#[serde(with = "humantime_serde")]
pub ttl: Option<Duration>,
}
impl Response {
#[must_use]
pub fn ok() -> Self {
Self::Raw(Value::default())
}
pub fn raw<T: Serialize>(data: T) -> Result<Self, serde_json::Error> {
serde_json::to_value(data).map(Self::Raw)
}
pub fn data<T: DeserializeOwned>(self) -> Result<T, ApiError> {
match self {
Response::Raw(data) => serde_json::from_value(data).map_err(|err| ApiError {
error: err.into(),
status_code: StatusCode::BAD_REQUEST,
span_trace: Some(SpanTrace::capture()),
}),
Response::Auth(_data) => Err(ApiError {
error: anyhow::Error::msg("expected raw data, found auth data"),
status_code: StatusCode::BAD_REQUEST,
span_trace: Some(SpanTrace::capture()),
}),
Response::Lease(_data) => Err(ApiError {
error: anyhow::Error::msg("expected raw data, found lease data"),
status_code: StatusCode::BAD_REQUEST,
span_trace: Some(SpanTrace::capture()),
}),
}
}
}