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 staking operations.
#[derive(Clone)]
pub struct StakingClient {
    client: crate::client::Client,
}

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

    /// Retrieve the list of stakes.
    pub async fn list_stakes(
        &self,
        query: Option<ListStakesQuery>,
    ) -> Result<ListStakesResponse, crate::error::Error> {
        let mut path = String::from("/staking/stakes");
        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::<ListStakesResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Create a new stake.
    pub async fn create_stake(
        &self,
        body: CreateStakeRequest,
    ) -> Result<CreateStakeResponse, crate::error::Error> {
        let path = String::from("/staking/stakes");
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<CreateStakeResponse>(reqwest::Method::POST, &path, Some(&body), true)
            .await
    }

    /// Retrieve the list of actions for a specific stake.
    pub async fn list_stake_actions(
        &self,
        stake_id: String,
        query: Option<ListStakeActionsQuery>,
    ) -> Result<ListStakeActionsResponse, crate::error::Error> {
        let mut path = format!("/staking/stakes/{}/actions", urlencoding::encode(&stake_id));
        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::<ListStakeActionsResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Create a new action for an existing stake.
    pub async fn create_stake_action(
        &self,
        stake_id: String,
        body: CreateStakeActionRequest,
    ) -> Result<CreateStakeActionResponse, crate::error::Error> {
        let path = format!("/staking/stakes/{}/actions", urlencoding::encode(&stake_id));
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<CreateStakeActionResponse>(reqwest::Method::POST, &path, Some(&body), true)
            .await
    }

    /// Retrieve the details of a specific stake.
    pub async fn get_stakes(
        &self,
        stake_id: String,
        query: Option<GetStakesQuery>,
    ) -> Result<GetStakesResponse, crate::error::Error> {
        let mut path = format!("/staking/stakes/{}", urlencoding::encode(&stake_id));
        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::<GetStakesResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Retrieves the rewards linked to a specific stake.
    pub async fn get_stake_rewards(
        &self,
        stake_id: String,
    ) -> Result<GetStakeRewardsResponse, crate::error::Error> {
        let path = format!("/staking/stakes/{}/rewards", urlencoding::encode(&stake_id));
        self.client
            .request::<GetStakeRewardsResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }
}