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 swaps operations. Signed operations are split into
/// init/complete pairs so the challenge can be signed on the user side.
#[derive(Clone)]
pub struct DelegatedSwapsClient {
    client: crate::client::Client,
}

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

    /// List all swaps with pagination
    pub async fn list_swaps(
        &self,
        query: Option<ListSwapsQuery>,
    ) -> Result<ListSwapsResponse, crate::error::Error> {
        let mut path = String::from("/swaps");
        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 !q.is_empty() {
                path.push('?');
                path.push_str(&q.join("&"));
            }
        }
        self.client
            .request::<ListSwapsResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

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

    /// Finishes delegated signing for createSwap: submits the signed challenge
    /// and issues the request.
    pub async fn create_swap_complete(
        &self,
        body: CreateSwapRequest,
        challenge_identifier: String,
        assertion: crate::signer::CredentialAssertion,
    ) -> Result<CreateSwapResponse, crate::error::Error> {
        let path = String::from("/swaps");
        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::<CreateSwapResponse>(
                reqwest::Method::POST,
                &path,
                Some(&body),
                &user_action,
            )
            .await
    }

    /// Request a quote from a given provider for swapping assets. This is the first step of the [Swap flow](https://docs.dfns.co/api-reference/swaps#flow-overview).
    pub async fn request_swap_quote(
        &self,
        body: RequestSwapQuoteRequest,
    ) -> Result<RequestSwapQuoteResponse, crate::error::Error> {
        let path = String::from("/swaps/quotes");
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<RequestSwapQuoteResponse>(reqwest::Method::POST, &path, Some(&body), false)
            .await
    }

    /// Get details of a specific swap by its ID
    pub async fn get_swap(&self, swap_id: String) -> Result<GetSwapResponse, crate::error::Error> {
        let path = format!("/swaps/{}", urlencoding::encode(&swap_id));
        self.client
            .request::<GetSwapResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Get details of a specific swap quote by its ID
    pub async fn get_swap_quote(
        &self,
        quote_id: String,
    ) -> Result<GetSwapQuoteResponse, crate::error::Error> {
        let path = format!("/swaps/quotes/{}", urlencoding::encode(&quote_id));
        self.client
            .request::<GetSwapQuoteResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }
}