spacetraders-client 0.1.0

Async Rust client for the SpaceTraders API v2 with priority-aware rate limiting and retry
Documentation
use crate::client::StClient;
use crate::client::{
    ClientError, RequestPriority, RetryPolicy, classify_status, retry_after_from_headers,
};
use crate::types;

impl StClient {
    pub async fn status(&self) -> Result<types::GetStatusResponse, ClientError> {
        self.status_with_priority(RequestPriority::Normal).await
    }

    pub async fn status_with_priority(
        &self,
        priority: RequestPriority,
    ) -> Result<types::GetStatusResponse, ClientError> {
        let url = format!("{}/", self.base_url);
        // Plain GET on the API root — idempotent, safe to retry transiently.
        self.send_retrying("status", priority, RetryPolicy::Idempotent, || {
            let url = url.clone();
            async move {
                let resp = self
                    .http
                    .get(&url)
                    .send()
                    .await
                    .map_err(ClientError::Http)?;

                if !resp.status().is_success() {
                    let status = resp.status();
                    let retry_after = retry_after_from_headers(resp.headers());
                    let body = resp.text().await.unwrap_or_default();
                    return Err(classify_status(status, retry_after, body));
                }

                resp.json().await.map_err(ClientError::Http)
            }
        })
        .await
    }

    pub async fn get_status(&self) -> Result<types::GetStatusResponse, ClientError> {
        self.get_status_with_priority(RequestPriority::Normal).await
    }

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

    pub async fn get_error_codes(&self) -> Result<types::GetErrorCodesResponse, ClientError> {
        self.get_error_codes_with_priority(RequestPriority::Normal)
            .await
    }

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