spacetraders-client 0.1.0

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

use std::num::NonZeroU64;

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

impl StClient {
    pub async fn get_my_ships(
        &self,
        limit: Option<NonZeroU64>,
        page: Option<NonZeroU64>,
    ) -> Result<types::GetMyShipsResponse, ClientError> {
        self.get_my_ships_with_priority(limit, page, RequestPriority::Normal)
            .await
    }

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

    pub async fn purchase_ship(
        &self,
        ship_type: types::ShipType,
        waypoint_symbol: &str,
    ) -> Result<types::PurchaseShipResponse, ClientError> {
        self.purchase_ship_with_priority(ship_type, waypoint_symbol, RequestPriority::Normal)
            .await
    }

    pub async fn purchase_ship_with_priority(
        &self,
        ship_type: types::ShipType,
        waypoint_symbol: &str,
        priority: RequestPriority,
    ) -> Result<types::PurchaseShipResponse, ClientError> {
        let body = types::PurchaseShipBody {
            ship_type,
            waypoint_symbol: waypoint_symbol.to_string(),
        };
        self.send_mutating("purchase_ship", priority, || {
            self.inner.purchase_ship(&body)
        })
        .await
    }

    pub async fn get_my_ship(
        &self,
        ship_symbol: &str,
    ) -> Result<types::GetMyShipResponse, ClientError> {
        self.get_my_ship_with_priority(ship_symbol, RequestPriority::Normal)
            .await
    }

    pub async fn get_my_ship_with_priority(
        &self,
        ship_symbol: &str,
        priority: RequestPriority,
    ) -> Result<types::GetMyShipResponse, ClientError> {
        self.send("get_my_ship", priority, || {
            self.inner.get_my_ship(ship_symbol)
        })
        .await
    }

    pub async fn get_my_ship_cargo(
        &self,
        ship_symbol: &str,
    ) -> Result<types::GetMyShipCargoResponse, ClientError> {
        self.get_my_ship_cargo_with_priority(ship_symbol, RequestPriority::Normal)
            .await
    }

    pub async fn get_my_ship_cargo_with_priority(
        &self,
        ship_symbol: &str,
        priority: RequestPriority,
    ) -> Result<types::GetMyShipCargoResponse, ClientError> {
        self.send("get_my_ship_cargo", priority, || {
            self.inner.get_my_ship_cargo(ship_symbol)
        })
        .await
    }

    pub async fn orbit_ship(
        &self,
        ship_symbol: &str,
    ) -> Result<types::OrbitShip200Response, ClientError> {
        self.orbit_ship_with_priority(ship_symbol, RequestPriority::Normal)
            .await
    }

    pub async fn orbit_ship_with_priority(
        &self,
        ship_symbol: &str,
        priority: RequestPriority,
    ) -> Result<types::OrbitShip200Response, ClientError> {
        self.send_mutating("orbit_ship", priority, || {
            self.inner.orbit_ship(ship_symbol)
        })
        .await
    }

    pub async fn dock_ship(
        &self,
        ship_symbol: &str,
    ) -> Result<types::DockShip200Response, ClientError> {
        self.dock_ship_with_priority(ship_symbol, RequestPriority::Normal)
            .await
    }

    pub async fn dock_ship_with_priority(
        &self,
        ship_symbol: &str,
        priority: RequestPriority,
    ) -> Result<types::DockShip200Response, ClientError> {
        self.send_mutating("dock_ship", priority, || self.inner.dock_ship(ship_symbol))
            .await
    }

    pub async fn navigate_ship(
        &self,
        ship_symbol: &str,
        waypoint_symbol: &str,
    ) -> Result<types::NavigateShipResponse, ClientError> {
        self.navigate_ship_with_priority(ship_symbol, waypoint_symbol, RequestPriority::Normal)
            .await
    }

    pub async fn navigate_ship_with_priority(
        &self,
        ship_symbol: &str,
        waypoint_symbol: &str,
        priority: RequestPriority,
    ) -> Result<types::NavigateShipResponse, ClientError> {
        let body = types::NavigateShipBody {
            waypoint_symbol: waypoint_symbol
                .parse()
                .map_err(|e| ClientError::Api(format!("invalid waypoint symbol: {e:?}")))?,
        };
        self.send_mutating("navigate_ship", priority, || {
            self.inner.navigate_ship(ship_symbol, &body)
        })
        .await
    }

    pub async fn refuel_ship(
        &self,
        ship_symbol: &str,
        units: Option<NonZeroU64>,
        from_cargo: bool,
    ) -> Result<types::RefuelShipResponse, ClientError> {
        self.refuel_ship_with_priority(ship_symbol, units, from_cargo, RequestPriority::Normal)
            .await
    }

    pub async fn refuel_ship_with_priority(
        &self,
        ship_symbol: &str,
        units: Option<NonZeroU64>,
        from_cargo: bool,
        priority: RequestPriority,
    ) -> Result<types::RefuelShipResponse, ClientError> {
        let body = types::RefuelShipBody { units, from_cargo };
        self.send_mutating("refuel_ship", priority, || {
            self.inner.refuel_ship(ship_symbol, &body)
        })
        .await
    }

    pub async fn get_ship_cooldown(
        &self,
        ship_symbol: &str,
    ) -> Result<types::GetShipCooldownResponse, ClientError> {
        self.get_ship_cooldown_with_priority(ship_symbol, RequestPriority::Normal)
            .await
    }

    pub async fn get_ship_cooldown_with_priority(
        &self,
        ship_symbol: &str,
        priority: RequestPriority,
    ) -> Result<types::GetShipCooldownResponse, ClientError> {
        self.send("get_ship_cooldown", priority, || {
            self.inner.get_ship_cooldown(ship_symbol)
        })
        .await
    }

    pub async fn get_ship_nav(
        &self,
        ship_symbol: &str,
    ) -> Result<types::GetShipNavResponse, ClientError> {
        self.get_ship_nav_with_priority(ship_symbol, RequestPriority::Normal)
            .await
    }

    pub async fn get_ship_nav_with_priority(
        &self,
        ship_symbol: &str,
        priority: RequestPriority,
    ) -> Result<types::GetShipNavResponse, ClientError> {
        self.send("get_ship_nav", priority, || {
            self.inner.get_ship_nav(ship_symbol)
        })
        .await
    }

    pub async fn patch_ship_nav(
        &self,
        ship_symbol: &str,
        flight_mode: Option<types::ShipNavFlightMode>,
    ) -> Result<types::PatchShipNavResponse, ClientError> {
        self.patch_ship_nav_with_priority(ship_symbol, flight_mode, RequestPriority::Normal)
            .await
    }

    pub async fn patch_ship_nav_with_priority(
        &self,
        ship_symbol: &str,
        flight_mode: Option<types::ShipNavFlightMode>,
        priority: RequestPriority,
    ) -> Result<types::PatchShipNavResponse, ClientError> {
        let body = types::PatchShipNavBody { flight_mode };
        self.send_mutating("patch_ship_nav", priority, || {
            self.inner.patch_ship_nav(ship_symbol, &body)
        })
        .await
    }

    pub async fn jump_ship(
        &self,
        ship_symbol: &str,
        waypoint_symbol: &str,
    ) -> Result<types::JumpShipResponse, ClientError> {
        self.jump_ship_with_priority(ship_symbol, waypoint_symbol, RequestPriority::Normal)
            .await
    }

    pub async fn jump_ship_with_priority(
        &self,
        ship_symbol: &str,
        waypoint_symbol: &str,
        priority: RequestPriority,
    ) -> Result<types::JumpShipResponse, ClientError> {
        let body = types::JumpShipBody {
            waypoint_symbol: waypoint_symbol.to_string(),
        };
        self.send_mutating("jump_ship", priority, || {
            self.inner.jump_ship(ship_symbol, &body)
        })
        .await
    }

    pub async fn warp_ship(
        &self,
        ship_symbol: &str,
        waypoint_symbol: &str,
    ) -> Result<types::WarpShipResponse, ClientError> {
        self.warp_ship_with_priority(ship_symbol, waypoint_symbol, RequestPriority::Normal)
            .await
    }

    pub async fn warp_ship_with_priority(
        &self,
        ship_symbol: &str,
        waypoint_symbol: &str,
        priority: RequestPriority,
    ) -> Result<types::WarpShipResponse, ClientError> {
        let body = types::WarpShipBody {
            waypoint_symbol: waypoint_symbol
                .parse()
                .map_err(|e| ClientError::Api(format!("invalid waypoint symbol: {e:?}")))?,
        };
        self.send_mutating("warp_ship", priority, || {
            self.inner.warp_ship(ship_symbol, &body)
        })
        .await
    }
}