bachs-sdk 0.1.0

Unofficial Rust SDK for the Bachs API — payments and billing for internet businesses.
Documentation
use crate::client::{Bachs, RequestOptions};
use crate::common::{FlexObject, ListParams, ListResponse, Metadata, Timestamp};
use crate::error::BachsResult;
use reqwest::Method;
use serde::{Deserialize, Serialize};

/// Move funds between your platform and a connected account you own.
#[derive(Debug, Clone, Serialize)]
pub struct CreateTransferRequest {
    pub connected_account_id: String,
    /// Decimal string at the currency's precision, e.g. `"25.00"`.
    pub amount: String,
    pub currency: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Metadata>,
}

impl CreateTransferRequest {
    pub fn new(
        connected_account_id: impl Into<String>,
        amount: impl Into<String>,
        currency: impl Into<String>,
    ) -> Self {
        Self {
            connected_account_id: connected_account_id.into(),
            amount: amount.into(),
            currency: currency.into(),
            metadata: None,
        }
    }
}

#[derive(Debug, Clone, Deserialize)]
pub struct Transfer {
    #[serde(default)]
    pub transfer_id: Option<String>,
    #[serde(default)]
    pub connected_account_id: Option<String>,
    #[serde(default)]
    pub status: Option<String>,
    #[serde(default)]
    pub amount: Option<String>,
    #[serde(default)]
    pub currency: Option<String>,
    #[serde(default)]
    pub metadata: Metadata,
    #[serde(default)]
    pub created_at: Option<Timestamp>,
    #[serde(flatten)]
    pub extra: FlexObject,
}

pub struct Transfers<'a> {
    pub(crate) client: &'a Bachs,
}

impl<'a> Transfers<'a> {
    pub async fn create(&self, req: &CreateTransferRequest) -> BachsResult<Transfer> {
        self.create_with_options(req, None).await
    }

    pub async fn create_with_options(
        &self,
        req: &CreateTransferRequest,
        options: Option<&RequestOptions>,
    ) -> BachsResult<Transfer> {
        self.client
            .send(Method::POST, "/v1/transfers", Some(req), None, options)
            .await
    }

    pub async fn get(&self, transfer_id: &str) -> BachsResult<Transfer> {
        self.client
            .send::<(), _>(
                Method::GET,
                &format!("/v1/transfers/{transfer_id}"),
                None,
                None,
                None,
            )
            .await
    }

    pub async fn list(&self, params: &ListParams) -> BachsResult<ListResponse<Transfer>> {
        let query = params.to_query();
        self.client
            .send::<(), _>(Method::GET, "/v1/transfers", None, Some(&query), None)
            .await
    }
}