use async_trait::async_trait; use serde::de::DeserializeOwned; use crate::network::{MockNetwork, NetRequest, NetworkError, RealNetwork, SavedRequest}; use super::NetResponse; #[derive(Debug, Clone)] pub enum Network { Mock(MockNetwork), Real(RealNetwork), } impl Network { pub fn new_mock() -> Self { Self::Mock(MockNetwork::default()) } pub fn new_real() -> Self { Self::Real(RealNetwork::default()) } pub async fn get( &self, net_request: NetRequest, ) -> Result, NetworkError> { match self { Self::Mock(mock) => mock.get(net_request).await, Self::Real(real) => real.get(net_request).await, } } pub async fn get_string( &self, net_request: NetRequest, ) -> Result, NetworkError> { match self { Self::Mock(mock) => mock.get_string(net_request).await, Self::Real(real) => real.get_string(net_request).await, } } pub async fn post_json( &self, net_request: NetRequest, ) -> Result, NetworkError> { match self { Self::Mock(mock) => mock.post_json(net_request).await, Self::Real(real) => real.post_json(net_request).await, } } pub async fn post_string( &self, net_request: NetRequest, ) -> Result, NetworkError> { match self { Self::Mock(mock) => mock.post_string(net_request).await, Self::Real(real) => real.post_string(net_request).await, } } pub async fn put_json( &self, net_request: NetRequest, ) -> Result, NetworkError> { match self { Self::Mock(mock) => mock.put_json(net_request).await, Self::Real(real) => real.put_json(net_request).await, } } pub async fn put_string( &self, net_request: NetRequest, ) -> Result, NetworkError> { match self { Self::Mock(mock) => mock.put_string(net_request).await, Self::Real(real) => real.put_string(net_request).await, } } pub async fn patch_json( &self, net_request: NetRequest, ) -> Result, NetworkError> { match self { Self::Mock(mock) => mock.patch_json(net_request).await, Self::Real(real) => real.patch_json(net_request).await, } } pub async fn delete(&self, net_request: NetRequest) -> Result, NetworkError> { match self { Self::Mock(mock) => mock.delete(net_request).await, Self::Real(real) => real.delete(net_request).await, } } pub async fn propfind( &self, net_request: NetRequest, ) -> Result, NetworkError> { match self { Self::Mock(mock) => mock.propfind(net_request).await, Self::Real(real) => real.propfind(net_request).await, } } pub async fn propfind_string( &self, net_request: NetRequest, ) -> Result, NetworkError> { match self { Self::Mock(mock) => mock.propfind_string(net_request).await, Self::Real(real) => real.propfind_string(net_request).await, } } pub fn mocked_requests(&self) -> Option> { match self { Self::Mock(mock) => Some(mock.requests()), Self::Real(_) => None, } } } #[async_trait] pub(super) trait NetworkTrait: Sync + Send + Clone + std::fmt::Debug { async fn get( &self, net_request: NetRequest, ) -> Result, NetworkError>; async fn get_string( &self, net_request: NetRequest, ) -> Result, NetworkError>; async fn post_json( &self, net_request: NetRequest, ) -> Result, NetworkError>; async fn post_string( &self, net_request: NetRequest, ) -> Result, NetworkError>; async fn put_json( &self, net_request: NetRequest, ) -> Result, NetworkError>; async fn put_string( &self, net_request: NetRequest, ) -> Result, NetworkError>; async fn patch_json( &self, net_request: NetRequest, ) -> Result, NetworkError>; async fn delete(&self, net_request: NetRequest) -> Result, NetworkError>; async fn propfind( &self, net_request: NetRequest, ) -> Result, NetworkError>; async fn propfind_string( &self, net_request: NetRequest, ) -> Result, NetworkError>; }