reevit 0.2.0

Official Rust SDK for the Reevit payments API
Documentation
use reqwest::Method;

use crate::{
    Client, CreateCustomerRequest, Customer, CustomerListOptions, PaginationOptions,
    PaymentSummary, RequestOptions, Result, TopCustomerOptions, UpdateCustomerRequest,
};

use super::path_segment;

/// Customer operations.
#[derive(Debug, Clone)]
pub struct Customers {
    client: Client,
}

impl Customers {
    pub(crate) fn new(client: Client) -> Self {
        Self { client }
    }

    /// Lists customers for the configured organization.
    pub async fn list(&self, options: &CustomerListOptions) -> Result<Vec<Customer>> {
        let request = self
            .client
            .request(Method::GET, "/v1/customers")?
            .query(options);
        self.client.send_collection(request, "customers").await
    }

    /// Creates a customer.
    pub async fn create(
        &self,
        input: &CreateCustomerRequest,
        options: RequestOptions,
    ) -> Result<Customer> {
        let request = self
            .client
            .request(Method::POST, "/v1/customers")?
            .json(input);
        self.client.send(request, options).await
    }

    /// Retrieves a customer by ID.
    pub async fn get(&self, id: &str) -> Result<Customer> {
        let request = self
            .client
            .request(Method::GET, &format!("/v1/customers/{}", path_segment(id)))?;
        self.client.send(request, RequestOptions::default()).await
    }

    /// Updates a customer.
    pub async fn update(
        &self,
        id: &str,
        input: &UpdateCustomerRequest,
        options: RequestOptions,
    ) -> Result<Customer> {
        let request = self
            .client
            .request(
                Method::PATCH,
                &format!("/v1/customers/{}", path_segment(id)),
            )?
            .json(input);
        self.client.send(request, options).await
    }

    /// Deletes a customer.
    pub async fn delete(&self, id: &str, options: RequestOptions) -> Result<()> {
        let request = self.client.request(
            Method::DELETE,
            &format!("/v1/customers/{}", path_segment(id)),
        )?;
        self.client.send(request, options).await
    }

    /// Looks up a customer by external ID.
    pub async fn lookup(&self, external_id: &str) -> Result<Customer> {
        let request = self
            .client
            .request(Method::GET, "/v1/customers/lookup")?
            .query(&[("external_id", external_id)]);
        self.client.send(request, RequestOptions::default()).await
    }

    /// Returns the highest-value customers for the requested period.
    pub async fn top(&self, options: &TopCustomerOptions) -> Result<Vec<Customer>> {
        let request = self
            .client
            .request(Method::GET, "/v1/customers/top")?
            .query(options);
        self.client.send_collection(request, "customers").await
    }

    /// Lists a customer's payment history.
    pub async fn list_payments(
        &self,
        id: &str,
        options: &PaginationOptions,
    ) -> Result<Vec<PaymentSummary>> {
        let request = self
            .client
            .request(
                Method::GET,
                &format!("/v1/customers/{}/payments", path_segment(id)),
            )?
            .query(options);
        self.client.send_collection(request, "payments").await
    }
}