dfns-sdk-rust 0.2.0

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

pub mod delegated;
pub mod types;

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

/// Client for swaps operations.
#[derive(Clone)]
pub struct SwapsClient {
    client: crate::client::Client,
}

impl SwapsClient {
    pub fn new(client: crate::client::Client) -> Self {
        SwapsClient { 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
    }

    /// Create a new swap based on an existing quote. This is the second step of the [Swap flow](https://docs.dfns.co/api-reference/swaps#flow-overview).
    pub async fn create_swap(
        &self,
        body: CreateSwapRequest,
    ) -> Result<CreateSwapResponse, crate::error::Error> {
        let path = String::from("/swaps");
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<CreateSwapResponse>(reqwest::Method::POST, &path, Some(&body), true)
            .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
    }
}