mod data;
use crate::client::product::{Product, ProductClient};
use crate::client::reqwest::{fetch_entities_list, fetch_entity, post_batches};
use crate::client::Client;
use crate::error::ClientError;
use sawtooth_sdk::messages::batch::BatchList;
const PRODUCT_ROUTE: &str = "product";
pub struct ReqwestProductClient {
url: String,
}
impl ReqwestProductClient {
pub fn new(url: String) -> Self {
Self { url }
}
}
impl Client for ReqwestProductClient {
fn post_batches(
&self,
wait: u64,
batch_list: &BatchList,
service_id: Option<&str>,
) -> Result<(), ClientError> {
post_batches(&self.url, wait, batch_list, service_id)
}
}
impl ProductClient for ReqwestProductClient {
fn get_product(&self, id: String, service_id: Option<&str>) -> Result<Product, ClientError> {
let dto = fetch_entity::<data::Product>(
&self.url,
format!("{}/{}", PRODUCT_ROUTE, id),
service_id,
)?;
Ok(Product::from(&dto))
}
fn list_products(&self, service_id: Option<&str>) -> Result<Vec<Product>, ClientError> {
let dto_vec = fetch_entities_list::<data::Product>(
&self.url,
PRODUCT_ROUTE.to_string(),
service_id,
None,
)?;
Ok(dto_vec.iter().map(Product::from).collect())
}
}