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

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

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

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

    /// Starts delegated signing for createStakeAction: returns the challenge to sign.
    /// Pass the signed assertion to create_stake_action_complete with the same arguments.
    pub async fn create_stake_action_init(
        &self,
        stake_id: String,
        body: CreateStakeActionRequest,
    ) -> Result<crate::signer::UserActionChallenge, crate::error::Error> {
        let path = format!("/staking/stakes/{}/actions", urlencoding::encode(&stake_id));
        let body = serde_json::to_value(&body)?;
        self.client
            .create_user_action_challenge(reqwest::Method::POST, &path, Some(&body))
            .await
    }

    /// Finishes delegated signing for createStakeAction: submits the signed challenge
    /// and issues the request.
    pub async fn create_stake_action_complete(
        &self,
        stake_id: String,
        body: CreateStakeActionRequest,
        challenge_identifier: String,
        assertion: crate::signer::CredentialAssertion,
    ) -> Result<CreateStakeActionResponse, crate::error::Error> {
        let path = format!("/staking/stakes/{}/actions", urlencoding::encode(&stake_id));
        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::<CreateStakeActionResponse>(
                reqwest::Method::POST,
                &path,
                Some(&body),
                &user_action,
            )
            .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
    }
}