dfns-sdk-rust 0.2.0

Dfns API SDK for Rust
Documentation
// Code generated by rust-sdk-generator. DO NOT EDIT.

#[allow(unused_imports)]
use super::types::*;

/// Delegated client for payins operations. Signed operations are split into
/// init/complete pairs so the challenge can be signed on the user side.
#[derive(Clone)]
pub struct DelegatedPayinsClient {
    client: crate::client::Client,
}

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

    /// List payins with optional filtering and pagination.
    pub async fn list_payins(
        &self,
        query: Option<ListPayinsQuery>,
    ) -> Result<ListPayinsResponse, crate::error::Error> {
        let mut path = String::from("/payins");
        if let Some(query) = &query {
            let mut q: Vec<String> = Vec::new();
            if let Some(v) = &query.limit {
                q.push(format!("limit={}", urlencoding::encode(&v.to_string())));
            }
            if let Some(v) = &query.pagination_token {
                q.push(format!(
                    "paginationToken={}",
                    urlencoding::encode(&v.to_string())
                ));
            }
            if let Some(v) = &query.wallet_id {
                q.push(format!("walletId={}", urlencoding::encode(&v.to_string())));
            }
            if let Some(v) = &query.status {
                for item in v {
                    q.push(format!("status={}", urlencoding::encode(&item.to_string())));
                }
            }
            if let Some(v) = &query.provider {
                for item in v {
                    q.push(format!(
                        "provider={}",
                        urlencoding::encode(&item.to_string())
                    ));
                }
            }
            if !q.is_empty() {
                path.push('?');
                path.push_str(&q.join("&"));
            }
        }
        self.client
            .request::<ListPayinsResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Starts delegated signing for createPayin: returns the challenge to sign.
    /// Pass the signed assertion to create_payin_complete with the same arguments.
    pub async fn create_payin_init(
        &self,
        body: CreatePayinRequest,
    ) -> Result<crate::signer::UserActionChallenge, crate::error::Error> {
        let path = String::from("/payins");
        let body = serde_json::to_value(&body)?;
        self.client
            .create_user_action_challenge(reqwest::Method::POST, &path, Some(&body))
            .await
    }

    /// Finishes delegated signing for createPayin: submits the signed challenge
    /// and issues the request.
    pub async fn create_payin_complete(
        &self,
        body: CreatePayinRequest,
        challenge_identifier: String,
        assertion: crate::signer::CredentialAssertion,
    ) -> Result<serde_json::Value, crate::error::Error> {
        let path = String::from("/payins");
        let body = serde_json::to_value(&body)?;
        let user_action = self
            .client
            .complete_user_action_signing(challenge_identifier, &assertion)
            .await?;
        self.client
            .request_with_user_action::<serde_json::Value>(
                reqwest::Method::POST,
                &path,
                Some(&body),
                &user_action,
            )
            .await
    }

    /// Check whether a wallet's address is registered (and approved) as an payin recipient with the provider.
    pub async fn get_payin_recipient(
        &self,
        query: Option<GetPayinRecipientQuery>,
    ) -> Result<GetPayinRecipientResponse, crate::error::Error> {
        let mut path = String::from("/payins/recipients");
        if let Some(query) = &query {
            let mut q: Vec<String> = Vec::new();
            q.push(format!(
                "provider={}",
                urlencoding::encode(&query.provider.to_string())
            ));
            q.push(format!(
                "walletId={}",
                urlencoding::encode(&query.wallet_id.to_string())
            ));
            q.push(format!(
                "currency={}",
                urlencoding::encode(&query.currency.to_string())
            ));
            if !q.is_empty() {
                path.push('?');
                path.push_str(&q.join("&"));
            }
        }
        self.client
            .request::<GetPayinRecipientResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Starts delegated signing for registerPayinRecipient: returns the challenge to sign.
    /// Pass the signed assertion to register_payin_recipient_complete with the same arguments.
    pub async fn register_payin_recipient_init(
        &self,
        body: RegisterPayinRecipientRequest,
    ) -> Result<crate::signer::UserActionChallenge, crate::error::Error> {
        let path = String::from("/payins/recipients");
        let body = serde_json::to_value(&body)?;
        self.client
            .create_user_action_challenge(reqwest::Method::POST, &path, Some(&body))
            .await
    }

    /// Finishes delegated signing for registerPayinRecipient: submits the signed challenge
    /// and issues the request.
    pub async fn register_payin_recipient_complete(
        &self,
        body: RegisterPayinRecipientRequest,
        challenge_identifier: String,
        assertion: crate::signer::CredentialAssertion,
    ) -> Result<RegisterPayinRecipientResponse, crate::error::Error> {
        let path = String::from("/payins/recipients");
        let body = serde_json::to_value(&body)?;
        let user_action = self
            .client
            .complete_user_action_signing(challenge_identifier, &assertion)
            .await?;
        self.client
            .request_with_user_action::<RegisterPayinRecipientResponse>(
                reqwest::Method::POST,
                &path,
                Some(&body),
                &user_action,
            )
            .await
    }

    /// Retrieve the current status of an payin by its ID.
    pub async fn get_payin_status(
        &self,
        payin_id: String,
    ) -> Result<serde_json::Value, crate::error::Error> {
        let path = format!("/payins/{}", urlencoding::encode(&payin_id));
        self.client
            .request::<serde_json::Value>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// The organisation's available balance at the payin provider, one entry per currency —
    ///     the funds payins can deliver on-chain.
    pub async fn list_payin_balances(
        &self,
        query: Option<ListPayinBalancesQuery>,
    ) -> Result<ListPayinBalancesResponse, crate::error::Error> {
        let mut path = String::from("/payins/balances");
        if let Some(query) = &query {
            let mut q: Vec<String> = Vec::new();
            q.push(format!(
                "provider={}",
                urlencoding::encode(&query.provider.to_string())
            ));
            if !q.is_empty() {
                path.push('?');
                path.push_str(&q.join("&"));
            }
        }
        self.client
            .request::<ListPayinBalancesResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }
}