use std::time::Duration;
#[derive(Debug, Clone)]
pub struct TransportRequest {
pub url: String,
pub method: String,
pub body: Vec<u8>,
pub headers: Vec<(String, String)>,
pub timeout: Duration,
pub accept_invalid_certs: bool,
}
impl TransportRequest {
pub fn post_json(url: impl Into<String>, body: Vec<u8>, accept_invalid_certs: bool) -> Self {
Self {
url: url.into(),
method: "POST".to_string(),
body,
headers: vec![("Content-Type".to_string(), "application/json".to_string())],
timeout: Duration::from_secs(15),
accept_invalid_certs,
}
}
pub fn post_bytes(url: impl Into<String>, body: Vec<u8>, accept_invalid_certs: bool) -> Self {
Self {
url: url.into(),
method: "POST".to_string(),
body,
headers: Vec::new(),
timeout: Duration::from_secs(30),
accept_invalid_certs,
}
}
}
#[derive(Debug, Clone)]
pub struct TransportResponse {
pub status_code: u16,
pub body: Vec<u8>,
pub headers: Vec<(String, String)>,
}
#[allow(async_fn_in_trait)]
pub trait Transport: Send + Sync {
async fn send_request(
&self,
request: &TransportRequest,
) -> Result<TransportResponse, TransportError>;
}
#[derive(Debug, thiserror::Error)]
pub enum TransportError {
#[error("Connection failed: {0}")]
ConnectionFailed(String),
#[error("Send failed: {0}")]
SendFailed(String),
#[error("Receive failed: {0}")]
ReceiveFailed(String),
#[error("Timeout")]
Timeout,
#[error("Connection closed")]
ConnectionClosed,
#[error("TLS error: {0}")]
TlsError(String),
#[error("HTTP {status}: {message}")]
HttpStatus { status: u16, message: String },
#[error("{0}")]
Other(String),
}