spacetraders-client 0.1.0

Async Rust client for the SpaceTraders API v2 with priority-aware rate limiting and retry
Documentation
mod factions;

use std::num::NonZeroU64;

use crate::client::StClient;
use crate::client::{ClientError, RequestPriority};
use crate::types;

impl StClient {
    pub async fn register(
        &self,
        symbol: &str,
        faction: types::FactionSymbol,
    ) -> Result<types::RegisterResponse, ClientError> {
        self.register_with_priority(symbol, faction, RequestPriority::Normal)
            .await
    }

    pub async fn register_with_priority(
        &self,
        symbol: &str,
        faction: types::FactionSymbol,
        priority: RequestPriority,
    ) -> Result<types::RegisterResponse, ClientError> {
        let body = types::RegisterBody {
            symbol: symbol
                .parse()
                .map_err(|e| ClientError::Api(format!("invalid symbol: {e:?}")))?,
            faction,
        };
        self.send_mutating("register", priority, || self.inner.register(&body))
            .await
    }

    pub async fn my_agent(&self) -> Result<types::GetMyAgentResponse, ClientError> {
        self.my_agent_with_priority(RequestPriority::Normal).await
    }

    pub async fn my_agent_with_priority(
        &self,
        priority: RequestPriority,
    ) -> Result<types::GetMyAgentResponse, ClientError> {
        self.send("get_my_agent", priority, || self.inner.get_my_agent())
            .await
    }

    pub async fn get_agents(
        &self,
        limit: Option<NonZeroU64>,
        page: Option<NonZeroU64>,
    ) -> Result<types::GetAgentsResponse, ClientError> {
        self.get_agents_with_priority(limit, page, RequestPriority::Normal)
            .await
    }

    pub async fn get_agents_with_priority(
        &self,
        limit: Option<NonZeroU64>,
        page: Option<NonZeroU64>,
        priority: RequestPriority,
    ) -> Result<types::GetAgentsResponse, ClientError> {
        self.send("get_agents", priority, || {
            self.inner.get_agents(limit, page)
        })
        .await
    }

    pub async fn get_agent(
        &self,
        agent_symbol: &str,
    ) -> Result<types::GetAgentResponse, ClientError> {
        self.get_agent_with_priority(agent_symbol, RequestPriority::Normal)
            .await
    }

    pub async fn get_agent_with_priority(
        &self,
        agent_symbol: &str,
        priority: RequestPriority,
    ) -> Result<types::GetAgentResponse, ClientError> {
        self.send("get_agent", priority, || self.inner.get_agent(agent_symbol))
            .await
    }

    pub async fn get_my_agent_events(
        &self,
    ) -> Result<types::GetMyAgentEventsResponse, ClientError> {
        self.get_my_agent_events_with_priority(RequestPriority::Normal)
            .await
    }

    pub async fn get_my_agent_events_with_priority(
        &self,
        priority: RequestPriority,
    ) -> Result<types::GetMyAgentEventsResponse, ClientError> {
        self.send("get_my_agent_events", priority, || {
            self.inner.get_my_agent_events()
        })
        .await
    }

    pub async fn get_my_account(&self) -> Result<types::GetMyAccountResponse, ClientError> {
        self.get_my_account_with_priority(RequestPriority::Normal)
            .await
    }

    pub async fn get_my_account_with_priority(
        &self,
        priority: RequestPriority,
    ) -> Result<types::GetMyAccountResponse, ClientError> {
        self.send("get_my_account", priority, || self.inner.get_my_account())
            .await
    }
}