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

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

    /// Lists the allocations of your organization.
    pub async fn list_allocations(
        &self,
        query: Option<ListAllocationsQuery>,
    ) -> Result<ListAllocationsResponse, crate::error::Error> {
        let mut path = String::from("/allocations");
        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::<ListAllocationsResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

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

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

    /// Retrieve the list of actions for a specific allocation.
    pub async fn list_allocation_actions(
        &self,
        allocation_id: String,
        query: Option<ListAllocationActionsQuery>,
    ) -> Result<ListAllocationActionsResponse, crate::error::Error> {
        let mut path = format!(
            "/allocations/{}/actions",
            urlencoding::encode(&allocation_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::<ListAllocationActionsResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Starts delegated signing for createAllocationAction: returns the challenge to sign.
    /// Pass the signed assertion to create_allocation_action_complete with the same arguments.
    pub async fn create_allocation_action_init(
        &self,
        allocation_id: String,
        body: CreateAllocationActionRequest,
    ) -> Result<crate::signer::UserActionChallenge, crate::error::Error> {
        let path = format!(
            "/allocations/{}/actions",
            urlencoding::encode(&allocation_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 createAllocationAction: submits the signed challenge
    /// and issues the request.
    pub async fn create_allocation_action_complete(
        &self,
        allocation_id: String,
        body: CreateAllocationActionRequest,
        challenge_identifier: String,
        assertion: crate::signer::CredentialAssertion,
    ) -> Result<CreateAllocationActionResponse, crate::error::Error> {
        let path = format!(
            "/allocations/{}/actions",
            urlencoding::encode(&allocation_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::<CreateAllocationActionResponse>(
                reqwest::Method::POST,
                &path,
                Some(&body),
                &user_action,
            )
            .await
    }

    /// Retrieve the details of a specific allocation.
    pub async fn get_allocation(
        &self,
        allocation_id: String,
    ) -> Result<GetAllocationResponse, crate::error::Error> {
        let path = format!("/allocations/{}", urlencoding::encode(&allocation_id));
        self.client
            .request::<GetAllocationResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Retrieve the current reward rate (APY) for each supported allocation protocol.
    pub async fn get_allocations_info(
        &self,
    ) -> Result<GetAllocationsInfoResponse, crate::error::Error> {
        let path = String::from("/allocations/info");
        self.client
            .request::<GetAllocationsInfoResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }
}