Current section
Files
Jump to
Current section
Files
src/spacetraders_api_httpc.gleam
import gleam/dynamic.{type Dynamic}
import gleam/http/request.{type Request}
import gleam/http/response.{type Response}
import gleam/httpc.{type HttpError}
import gleam/json.{type DecodeError}
import gleam/option.{type Option}
import gleam/result
import spacetraders_api.{
type AgentRegistered, type ApiError, type CargoPurchased, type CargoSold,
type CargoTransferred, type ChartCreated, type ConstructionSiteSupplied,
type ContractAccepted, type ContractCargoDelivered, type ContractFulfilled,
type ErrorCode, type ExportToImportMap, type FactionReputation, type PagedData,
type ResourcesExtracted, type ResourcesSiphoned, type ServerStatus,
type ShipJumped, type ShipModuleInstalled, type ShipModuleRemoved,
type ShipMountInstalled, type ShipMountRemoved, type ShipNavPatched,
type ShipNavigated, type ShipPurchased, type ShipRefined, type ShipRefueled,
type ShipRepaired, type ShipScrapped, type ShipWarped, type ShipsScan,
type SurveyCreated, type SystemsScan, type WaypointsScan,
}
import spacetraders_models.{
type Account, type AccountToken, type Agent, type AgentEvent, type AgentSymbol,
type AgentToken, type Contract, type ContractId, type Faction,
type FactionSymbol, type JumpGate, type Market, type PublicAgent, type Ship,
type ShipCargo, type ShipCooldown, type ShipModule, type ShipModuleSymbol,
type ShipMount, type ShipMountSymbol, type ShipNav, type ShipNavFlightMode,
type ShipRefinementProduce, type ShipRepairTransaction,
type ShipScrapTransaction, type ShipSymbol, type ShipType, type Shipyard,
type Survey, type System, type SystemSymbol, type TradeSymbol, type Waypoint,
type WaypointConstruction, type WaypointSymbol, type WaypointTraitSymbol,
type WaypointType,
}
pub type HttpcApiError {
ClientError(HttpError)
JsonDecodeError(DecodeError)
RequestError(
code: Int,
message: String,
data: Option(Dynamic),
request_id: Option(String),
)
}
fn send(request: Request(BitArray)) -> Result(Response(BitArray), HttpcApiError) {
case httpc.send_bits(request) {
Ok(res) -> Ok(res)
Error(err) -> Error(ClientError(err))
}
}
fn try_send(
request: Request(BitArray),
apply fun: fn(Response(BitArray)) -> Result(data, ApiError),
) -> Result(data, HttpcApiError) {
use res <- result.try(send(request))
fun(res)
|> result.map_error(fn(api_error) {
case api_error {
spacetraders_api.JsonDecodeError(e) -> JsonDecodeError(e)
spacetraders_api.RequestError(code:, message:, data:, request_id:) ->
RequestError(code:, message:, data:, request_id:)
}
})
}
pub fn get_account(token: AgentToken) -> Result(Account, HttpcApiError) {
try_send(
spacetraders_api.get_account_request(token),
spacetraders_api.get_account_response,
)
}
pub fn register_new_agent(
token: AccountToken,
agent_symbol: AgentSymbol,
faction_symbol: FactionSymbol,
) -> Result(AgentRegistered, HttpcApiError) {
try_send(
spacetraders_api.register_new_agent_request(
token,
agent_symbol,
faction_symbol,
),
spacetraders_api.register_new_agent_response,
)
}
pub fn list_public_agents(
page: Option(Int),
limit: Option(Int),
) -> Result(PagedData(List(PublicAgent)), HttpcApiError) {
try_send(
spacetraders_api.list_public_agents_request(page, limit),
spacetraders_api.list_public_agents_response,
)
}
pub fn get_public_agent(
agent_symbol: AgentSymbol,
) -> Result(PublicAgent, HttpcApiError) {
try_send(
spacetraders_api.get_public_agent_request(agent_symbol),
spacetraders_api.get_public_agent_response,
)
}
pub fn get_agent(token: AgentToken) -> Result(Agent, HttpcApiError) {
try_send(
spacetraders_api.get_agent_request(token),
spacetraders_api.get_agent_response,
)
}
pub fn get_agent_events(
token: AgentToken,
) -> Result(List(AgentEvent), HttpcApiError) {
try_send(
spacetraders_api.get_agent_events_request(token),
spacetraders_api.get_agent_events_response,
)
}
pub fn list_contracts(
token: AgentToken,
page: Option(Int),
limit: Option(Int),
) -> Result(PagedData(List(Contract)), HttpcApiError) {
try_send(
spacetraders_api.list_contracts_request(token, page, limit),
spacetraders_api.list_contracts_response,
)
}
pub fn get_contract(
token: AgentToken,
contract_id: ContractId,
) -> Result(Contract, HttpcApiError) {
try_send(
spacetraders_api.get_contract_request(token, contract_id),
spacetraders_api.get_contract_response,
)
}
pub fn accept_contract(
token: AgentToken,
contract_id: ContractId,
) -> Result(ContractAccepted, HttpcApiError) {
try_send(
spacetraders_api.accept_contract_request(token, contract_id),
spacetraders_api.accept_contract_response,
)
}
pub fn fulfill_contract(
token: AgentToken,
contract_id: ContractId,
) -> Result(ContractFulfilled, HttpcApiError) {
try_send(
spacetraders_api.fulfill_contract_request(token, contract_id),
spacetraders_api.fulfill_contract_response,
)
}
pub fn deliver_contract_cargo(
token: AgentToken,
contract_id: ContractId,
ship_symbol: ShipSymbol,
trade_symbol: TradeSymbol,
units: Int,
) -> Result(ContractCargoDelivered, HttpcApiError) {
try_send(
spacetraders_api.deliver_contract_cargo_request(
token,
contract_id,
ship_symbol,
trade_symbol,
units,
),
spacetraders_api.deliver_contract_cargo_response,
)
}
pub fn get_supply_chain() -> Result(ExportToImportMap, HttpcApiError) {
try_send(
spacetraders_api.get_supply_chain_request(),
spacetraders_api.get_supply_chain_response,
)
}
pub fn list_factions(
page: Option(Int),
limit: Option(Int),
) -> Result(PagedData(List(Faction)), HttpcApiError) {
try_send(
spacetraders_api.list_factions_request(page, limit),
spacetraders_api.list_factions_response,
)
}
pub fn get_faction(symbol: FactionSymbol) -> Result(Faction, HttpcApiError) {
try_send(
spacetraders_api.get_faction_request(symbol),
spacetraders_api.get_faction_response,
)
}
pub fn get_my_factions(
token: AgentToken,
page: Option(Int),
limit: Option(Int),
) -> Result(PagedData(List(FactionReputation)), HttpcApiError) {
try_send(
spacetraders_api.get_my_factions_request(token, page, limit),
spacetraders_api.get_my_factions_response,
)
}
pub fn list_ships(
token: AgentToken,
page: Option(Int),
limit: Option(Int),
) -> Result(PagedData(List(Ship)), HttpcApiError) {
try_send(
spacetraders_api.list_ships_request(token, page, limit),
spacetraders_api.list_ships_response,
)
}
pub fn purchase_ship(
token: AgentToken,
ship_type: ShipType,
waypoint_symbol: WaypointSymbol,
) -> Result(ShipPurchased, HttpcApiError) {
try_send(
spacetraders_api.purchase_ship_request(token, ship_type, waypoint_symbol),
spacetraders_api.purchase_ship_response,
)
}
pub fn get_ship(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(Ship, HttpcApiError) {
try_send(
spacetraders_api.get_ship_request(token, ship_symbol),
spacetraders_api.get_ship_response,
)
}
pub fn create_chart(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(ChartCreated, HttpcApiError) {
try_send(
spacetraders_api.create_chart_request(token, ship_symbol),
spacetraders_api.create_chart_response,
)
}
pub fn negotiate_contract(
token: AgentToken,
ship_symbol: String,
) -> Result(Contract, HttpcApiError) {
try_send(
spacetraders_api.negotiate_contract_request(token, ship_symbol),
spacetraders_api.negotiate_contract_response,
)
}
pub fn get_ship_cooldown(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(Option(ShipCooldown), HttpcApiError) {
try_send(
spacetraders_api.get_ship_cooldown_request(token, ship_symbol),
spacetraders_api.get_ship_cooldown_response,
)
}
pub fn dock_ship(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(ShipNav, HttpcApiError) {
try_send(
spacetraders_api.dock_ship_request(token, ship_symbol),
spacetraders_api.dock_ship_response,
)
}
pub fn extract_resources(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(ResourcesExtracted, HttpcApiError) {
try_send(
spacetraders_api.extract_resources_request(token, ship_symbol),
spacetraders_api.extract_resources_response,
)
}
pub fn extract_resources_with_survey(
token: AgentToken,
ship_symbol: ShipSymbol,
survey: Survey,
) -> Result(ResourcesExtracted, HttpcApiError) {
try_send(
spacetraders_api.extract_resources_with_survey_request(
token,
ship_symbol,
survey,
),
spacetraders_api.extract_resources_with_survey_response,
)
}
pub fn jettison_cargo(
token: AgentToken,
ship_symbol: ShipSymbol,
trade_symbol: TradeSymbol,
units: Int,
) -> Result(ShipCargo, HttpcApiError) {
try_send(
spacetraders_api.jettison_cargo_request(
token,
ship_symbol,
trade_symbol,
units,
),
spacetraders_api.jettison_cargo_response,
)
}
pub fn jump_ship(
token: AgentToken,
ship_symbol: ShipSymbol,
waypoint_symbol: WaypointSymbol,
) -> Result(ShipJumped, HttpcApiError) {
try_send(
spacetraders_api.jump_ship_request(token, ship_symbol, waypoint_symbol),
spacetraders_api.jump_ship_response,
)
}
pub fn scan_systems(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(SystemsScan, HttpcApiError) {
try_send(
spacetraders_api.scan_systems_request(token, ship_symbol),
spacetraders_api.scan_systems_response,
)
}
pub fn scan_waypoints(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(WaypointsScan, HttpcApiError) {
try_send(
spacetraders_api.scan_waypoints_request(token, ship_symbol),
spacetraders_api.scan_waypoints_response,
)
}
pub fn scan_ships(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(ShipsScan, HttpcApiError) {
try_send(
spacetraders_api.scan_ships_request(token, ship_symbol),
spacetraders_api.scan_ships_response,
)
}
pub fn scrap_ship(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(ShipScrapped, HttpcApiError) {
try_send(
spacetraders_api.scrap_ship_request(token, ship_symbol),
spacetraders_api.scrap_ship_response,
)
}
pub fn get_scrap_ship(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(ShipScrapTransaction, HttpcApiError) {
try_send(
spacetraders_api.get_scrap_ship_request(token, ship_symbol),
spacetraders_api.get_scrap_ship_response,
)
}
pub fn navigate_ship(
token: AgentToken,
ship_symbol: ShipSymbol,
waypoint_symbol: WaypointSymbol,
) -> Result(ShipNavigated, HttpcApiError) {
try_send(
spacetraders_api.navigate_ship_request(token, ship_symbol, waypoint_symbol),
spacetraders_api.navigate_ship_response,
)
}
pub fn warp_ship(
token: AgentToken,
ship_symbol: ShipSymbol,
waypoint_symbol: WaypointSymbol,
) -> Result(ShipWarped, HttpcApiError) {
try_send(
spacetraders_api.warp_ship_request(token, ship_symbol, waypoint_symbol),
spacetraders_api.warp_ship_response,
)
}
pub fn orbit_ship(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(ShipNav, HttpcApiError) {
try_send(
spacetraders_api.orbit_ship_request(token, ship_symbol),
spacetraders_api.orbit_ship_response,
)
}
pub fn purchase_cargo(
token: AgentToken,
ship_symbol: ShipSymbol,
trade_symbol: TradeSymbol,
) -> Result(CargoPurchased, HttpcApiError) {
try_send(
spacetraders_api.purchase_cargo_request(token, ship_symbol, trade_symbol),
spacetraders_api.purchase_cargo_response,
)
}
pub fn refine_ship(
token: AgentToken,
ship_symbol: ShipSymbol,
produce: ShipRefinementProduce,
) -> Result(ShipRefined, HttpcApiError) {
try_send(
spacetraders_api.refine_ship_request(token, ship_symbol, produce),
spacetraders_api.refine_ship_response,
)
}
pub fn refuel_ship(
token: AgentToken,
ship_symbol: ShipSymbol,
units: Option(Int),
from_cargo: Option(Bool),
) -> Result(ShipRefueled, HttpcApiError) {
try_send(
spacetraders_api.refuel_ship_request(token, ship_symbol, units, from_cargo),
spacetraders_api.refuel_ship_response,
)
}
pub fn repair_ship(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(ShipRepaired, HttpcApiError) {
try_send(
spacetraders_api.repair_ship_request(token, ship_symbol),
spacetraders_api.repair_ship_response,
)
}
pub fn get_repair_ship(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(ShipRepairTransaction, HttpcApiError) {
try_send(
spacetraders_api.get_repair_ship_request(token, ship_symbol),
spacetraders_api.get_repair_ship_response,
)
}
pub fn sell_cargo(
token: AgentToken,
ship_symbol: ShipSymbol,
trade_symbol: TradeSymbol,
units: Int,
) -> Result(CargoSold, HttpcApiError) {
try_send(
spacetraders_api.sell_cargo_request(token, ship_symbol, trade_symbol, units),
spacetraders_api.sell_cargo_response,
)
}
pub fn siphon_resources(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(ResourcesSiphoned, HttpcApiError) {
try_send(
spacetraders_api.siphon_resources_request(token, ship_symbol),
spacetraders_api.siphon_resources_response,
)
}
pub fn create_survey(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(SurveyCreated, HttpcApiError) {
try_send(
spacetraders_api.create_survey_request(token, ship_symbol),
spacetraders_api.create_survey_response,
)
}
pub fn transfer_cargo(
token: AgentToken,
ship_symbol: ShipSymbol,
trade_symbol: TradeSymbol,
units: Int,
target_ship_symbol: ShipSymbol,
) -> Result(CargoTransferred, HttpcApiError) {
try_send(
spacetraders_api.transfer_cargo_request(
token,
ship_symbol,
trade_symbol,
units,
target_ship_symbol,
),
spacetraders_api.transfer_cargo_response,
)
}
pub fn get_ship_cargo(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(ShipCargo, HttpcApiError) {
try_send(
spacetraders_api.get_ship_cargo_request(token, ship_symbol),
spacetraders_api.get_ship_cargo_response,
)
}
pub fn get_ship_modules(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(List(ShipModule), HttpcApiError) {
try_send(
spacetraders_api.get_ship_modules_request(token, ship_symbol),
spacetraders_api.get_ship_modules_response,
)
}
pub fn install_ship_module(
token: AgentToken,
ship_symbol: ShipSymbol,
module_symbol: ShipModuleSymbol,
) -> Result(ShipModuleInstalled, HttpcApiError) {
try_send(
spacetraders_api.install_ship_module_request(
token,
ship_symbol,
module_symbol,
),
spacetraders_api.install_ship_module_response,
)
}
pub fn remove_ship_module(
token: AgentToken,
ship_symbol: ShipSymbol,
module_symbol: ShipModuleSymbol,
) -> Result(ShipModuleRemoved, HttpcApiError) {
try_send(
spacetraders_api.remove_ship_module_request(
token,
ship_symbol,
module_symbol,
),
spacetraders_api.remove_ship_module_response,
)
}
pub fn get_ship_mounts(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(List(ShipMount), HttpcApiError) {
try_send(
spacetraders_api.get_ship_mounts_request(token, ship_symbol),
spacetraders_api.get_ship_mounts_response,
)
}
pub fn install_ship_mount(
token: AgentToken,
ship_symbol: ShipSymbol,
mount_symbol: ShipMountSymbol,
) -> Result(ShipMountInstalled, HttpcApiError) {
try_send(
spacetraders_api.install_ship_mount_request(
token,
ship_symbol,
mount_symbol,
),
spacetraders_api.install_ship_mount_response,
)
}
pub fn remove_ship_mount(
token: AgentToken,
ship_symbol: ShipSymbol,
mount_symbol: ShipMountSymbol,
) -> Result(ShipMountRemoved, HttpcApiError) {
try_send(
spacetraders_api.remove_ship_mount_request(token, ship_symbol, mount_symbol),
spacetraders_api.remove_ship_mount_response,
)
}
pub fn get_ship_nav(
token: AgentToken,
ship_symbol: ShipSymbol,
) -> Result(ShipNav, HttpcApiError) {
try_send(
spacetraders_api.get_ship_nav_request(token, ship_symbol),
spacetraders_api.get_ship_nav_response,
)
}
pub fn patch_ship_nav(
token: AgentToken,
ship_symbol: ShipSymbol,
flight_mode: ShipNavFlightMode,
) -> Result(ShipNavPatched, HttpcApiError) {
try_send(
spacetraders_api.patch_ship_nav_request(token, ship_symbol, flight_mode),
spacetraders_api.patch_ship_nav_response,
)
}
pub fn get_server_status() -> Result(ServerStatus, HttpcApiError) {
try_send(
spacetraders_api.get_server_status_request(),
spacetraders_api.get_server_status_response,
)
}
pub fn list_error_codes() -> Result(List(ErrorCode), HttpcApiError) {
try_send(
spacetraders_api.list_error_codes_request(),
spacetraders_api.list_error_codes_response,
)
}
pub fn list_systems(
page: Option(Int),
limit: Option(Int),
) -> Result(PagedData(List(System)), HttpcApiError) {
try_send(
spacetraders_api.list_systems_request(page, limit),
spacetraders_api.list_systems_response,
)
}
pub fn get_system(system_symbol: SystemSymbol) -> Result(System, HttpcApiError) {
try_send(
spacetraders_api.get_system_request(system_symbol),
spacetraders_api.get_system_response,
)
}
pub fn list_system_waypoints(
system_symbol: SystemSymbol,
page: Option(Int),
limit: Option(Int),
type_: Option(WaypointType),
traits: List(WaypointTraitSymbol),
) -> Result(PagedData(List(Waypoint)), HttpcApiError) {
try_send(
spacetraders_api.list_system_waypoints_request(
system_symbol,
page,
limit,
type_,
traits,
),
spacetraders_api.list_system_waypoints_response,
)
}
pub fn get_waypoint(
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
) -> Result(Waypoint, HttpcApiError) {
try_send(
spacetraders_api.get_waypoint_request(system_symbol, waypoint_symbol),
spacetraders_api.get_waypoint_response,
)
}
pub fn get_construction_site(
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
) -> Result(WaypointConstruction, HttpcApiError) {
try_send(
spacetraders_api.get_construction_site_request(
system_symbol,
waypoint_symbol,
),
spacetraders_api.get_construction_site_response,
)
}
pub fn supply_construction_site(
token: AgentToken,
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
ship_symbol: ShipSymbol,
trade_symbol: TradeSymbol,
units: Int,
) -> Result(ConstructionSiteSupplied, HttpcApiError) {
try_send(
spacetraders_api.supply_construction_site_request(
token,
system_symbol,
waypoint_symbol,
ship_symbol,
trade_symbol,
units,
),
spacetraders_api.supply_construction_site_response,
)
}
pub fn get_market(
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
) -> Result(Market, HttpcApiError) {
try_send(
spacetraders_api.get_market_request(system_symbol, waypoint_symbol),
spacetraders_api.get_market_response,
)
}
pub fn get_jump_gate(
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
) -> Result(JumpGate, HttpcApiError) {
try_send(
spacetraders_api.get_jump_gate_request(system_symbol, waypoint_symbol),
spacetraders_api.get_jump_gate_response,
)
}
pub fn get_shipyard(
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
) -> Result(Shipyard, HttpcApiError) {
try_send(
spacetraders_api.get_shipyard_request(system_symbol, waypoint_symbol),
spacetraders_api.get_shipyard_response,
)
}