reevit 0.2.0

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

use crate::{Client, Invoice, InvoiceListOptions, InvoiceUpdateRequest, RequestOptions, Result};

use super::path_segment;

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

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

    /// Lists invoices.
    pub async fn list(&self, options: &InvoiceListOptions) -> Result<Vec<Invoice>> {
        let request = self
            .client
            .request(Method::GET, "/v1/invoices")?
            .query(options);
        self.client.send_collection(request, "invoices").await
    }

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

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

    /// Cancels an invoice.
    pub async fn cancel(&self, id: &str, options: RequestOptions) -> Result<Invoice> {
        self.client
            .post_empty(
                &format!("/v1/invoices/{}/cancel", path_segment(id)),
                options,
            )
            .await
    }

    /// Retries invoice collection.
    pub async fn retry(&self, id: &str, options: RequestOptions) -> Result<Invoice> {
        self.client
            .post_empty(&format!("/v1/invoices/{}/retry", path_segment(id)), options)
            .await
    }
}