kxio/src/network/network_env.rs

175 lines
5.2 KiB
Rust
Raw Normal View History

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<T: DeserializeOwned + std::fmt::Debug>(
&self,
net_request: NetRequest,
) -> Result<NetResponse<T>, 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<NetResponse<String>, 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<Reply: DeserializeOwned + std::fmt::Debug>(
&self,
net_request: NetRequest,
) -> Result<NetResponse<Reply>, 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<NetResponse<String>, 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<Reply: DeserializeOwned + std::fmt::Debug>(
&self,
net_request: NetRequest,
) -> Result<NetResponse<Reply>, 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<NetResponse<String>, 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<Reply: DeserializeOwned + std::fmt::Debug>(
&self,
net_request: NetRequest,
) -> Result<NetResponse<Reply>, 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<NetResponse<()>, NetworkError> {
match self {
Self::Mock(mock) => mock.delete(net_request).await,
Self::Real(real) => real.delete(net_request).await,
}
}
pub async fn propfind<Reply: DeserializeOwned + std::fmt::Debug>(
&self,
net_request: NetRequest,
) -> Result<NetResponse<Reply>, 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<NetResponse<String>, 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<Vec<SavedRequest>> {
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<T: DeserializeOwned + std::fmt::Debug>(
&self,
net_request: NetRequest,
) -> Result<NetResponse<T>, NetworkError>;
async fn get_string(
&self,
net_request: NetRequest,
) -> Result<NetResponse<String>, NetworkError>;
async fn post_json<Reply: DeserializeOwned + std::fmt::Debug>(
&self,
net_request: NetRequest,
) -> Result<NetResponse<Reply>, NetworkError>;
async fn post_string(
&self,
net_request: NetRequest,
) -> Result<NetResponse<String>, NetworkError>;
async fn put_json<Reply: DeserializeOwned + std::fmt::Debug>(
&self,
net_request: NetRequest,
) -> Result<NetResponse<Reply>, NetworkError>;
async fn put_string(
&self,
net_request: NetRequest,
) -> Result<NetResponse<String>, NetworkError>;
async fn patch_json<Reply: DeserializeOwned + std::fmt::Debug>(
&self,
net_request: NetRequest,
) -> Result<NetResponse<Reply>, NetworkError>;
async fn delete(&self, net_request: NetRequest) -> Result<NetResponse<()>, NetworkError>;
async fn propfind<Reply: DeserializeOwned + std::fmt::Debug>(
&self,
net_request: NetRequest,
) -> Result<NetResponse<Reply>, NetworkError>;
async fn propfind_string(
&self,
net_request: NetRequest,
) -> Result<NetResponse<String>, NetworkError>;
}