use std::future::Future;
use crate::protocol::customer_registration::CustomerRef;
use super::{ClientError, VersaClient};
pub trait CustomerRegistration {
fn register_customer_reference(
&self,
customer_reference: CustomerRef,
) -> impl Future<Output = Result<(), ClientError>> + Send;
fn deregister_customer_reference(
&self,
customer_reference: CustomerRef,
) -> impl Future<Output = Result<(), ClientError>> + Send;
}
impl CustomerRegistration for VersaClient {
async fn register_customer_reference(
&self,
customer_reference: CustomerRef,
) -> Result<(), ClientError> {
let credential = self.authorization_header_val();
let url = format!("{}/customer", self.registry_url);
let payload_json = serde_json::to_string(&customer_reference).unwrap();
let client = reqwest::Client::new();
let response_result = client
.post(url)
.header("Accept", "application/json")
.header("Authorization", credential)
.header("Content-Type", "application/json")
.body(payload_json)
.send()
.await;
match response_result {
Ok(response) => {
if response.status().is_success() {
Ok(())
} else {
Err(ClientError::RegistryError(
response.status(),
response.text().await.unwrap(),
))
}
}
Err(e) => Err(ClientError::NetworkError(e)),
}
}
async fn deregister_customer_reference(
&self,
customer_reference: CustomerRef,
) -> Result<(), ClientError> {
let credential = self.authorization_header_val();
let url = format!("{}/customer", self.registry_url);
let payload_json = serde_json::to_string(&customer_reference).unwrap();
let client = reqwest::Client::new();
let response_result = client
.delete(url)
.header("Accept", "application/json")
.header("Authorization", credential)
.header("Content-Type", "application/json")
.body(payload_json)
.send()
.await;
match response_result {
Ok(response) => {
if response.status().is_success() {
Ok(())
} else {
Err(ClientError::RegistryError(
response.status(),
response.text().await.unwrap(),
))
}
}
Err(e) => Err(ClientError::NetworkError(e)),
}
}
}