versa 1.9.0

Versa types and utilities for developing Versa client applications in Rust
Documentation
use std::future::Future;

use crate::protocol::customer_registration::CustomerRef;

use super::{ClientError, VersaClient};

pub trait CustomerRegistration {
  /// Create or reactivate a customer reference with the registry,
  /// directing receipts matching the handle to a receiver client.
  /// If registering as a sender, a receiver_client_id must be provided.
  /// If registering as a receiver, the receiver_client_id may be excluded (it will be set to your client_id).
  fn register_customer_reference(
    &self,
    customer_reference: CustomerRef,
  ) -> impl Future<Output = Result<(), ClientError>> + Send;
  /// Deactivate a customer reference record with the registry;
  /// Receipts matching the handle will no longer be directed to the receiver client based on that reference.
  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)),
    }
  }
}