use async_trait::async_trait;
use http_client::{Config, Error, HttpClient, Request, Response};
#[derive(Debug, Clone)]
pub struct NoOpClient {
error_message: String,
config: Config,
}
impl NoOpClient {
pub fn new() -> Self {
Self {
error_message: "NoOpClient: Real HTTP requests are not allowed. This indicates a VCR configuration issue - requests should be replayed from cassette.".to_string(),
config: Config::new(),
}
}
pub fn with_message(message: impl Into<String>) -> Self {
Self {
error_message: message.into(),
config: Config::new(),
}
}
pub fn panicking() -> PanickingNoOpClient {
PanickingNoOpClient::new()
}
}
impl Default for NoOpClient {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl HttpClient for NoOpClient {
async fn send(&self, req: Request) -> Result<Response, Error> {
Err(Error::from_str(
500,
format!(
"{} Attempted request: {} {}",
self.error_message,
req.method(),
req.url()
),
))
}
fn set_config(&mut self, config: Config) -> Result<(), Error> {
self.config = config;
Ok(())
}
fn config(&self) -> &Config {
&self.config
}
}
#[derive(Debug, Clone)]
pub struct PanickingNoOpClient {
panic_message: String,
config: Config,
}
impl PanickingNoOpClient {
pub fn new() -> Self {
Self {
panic_message: "PanickingNoOpClient: Unexpected HTTP request detected! This should not happen in VCR replay mode.".to_string(),
config: Config::new(),
}
}
pub fn with_message(message: impl Into<String>) -> Self {
Self {
panic_message: message.into(),
config: Config::new(),
}
}
}
impl Default for PanickingNoOpClient {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl HttpClient for PanickingNoOpClient {
async fn send(&self, req: Request) -> Result<Response, Error> {
panic!(
"{} Attempted request: {} {} - Check your VCR configuration and cassette contents.",
self.panic_message,
req.method(),
req.url()
);
}
fn set_config(&mut self, config: Config) -> Result<(), Error> {
self.config = config;
Ok(())
}
fn config(&self) -> &Config {
&self.config
}
}