Compare commits

..

No commits in common. "00fdca952fb0613850943151af65cf22345e1734" and "bf4a2eef53872f6f75d0503d88c17513e8f97881" have entirely different histories.

4 changed files with 56 additions and 29 deletions

View file

@ -1,5 +1,5 @@
name = "spacetraders_sdk"
version = "1.2.0"
version = "1.1.0"
gleam = ">= 1.11.0"
description = "A Gleam SDK for the spacetraders.io game API"
licences = ["MIT"]

View file

@ -5,13 +5,14 @@ import models/agent_event.{type AgentEvent}
import models/agent_symbol.{type AgentSymbol}
import models/public_agent.{type PublicAgent}
import utils/api.{type ApiResponse, type PagedData}
import utils/auth.{type AgentToken, AgentAuth, NoAuth}
import utils/auth.{type AgentToken, AgentAuth}
pub fn list_public_agents(
token: AgentToken,
page: Option(Int),
limit: Option(Int),
) -> ApiResponse(PagedData(List(PublicAgent))) {
let request = api.get_page(NoAuth, "/agents", page, limit)
let request = api.get_page(AgentAuth(token), "/agents", page, limit)
use response <- api.try_send(request)
case response.status {
200 ->
@ -23,9 +24,15 @@ pub fn list_public_agents(
}
}
pub fn get_public_agent(agent_symbol: AgentSymbol) -> ApiResponse(PublicAgent) {
pub fn get_public_agent(
token: AgentToken,
agent_symbol: AgentSymbol,
) -> ApiResponse(PublicAgent) {
let request =
api.get(NoAuth, "/agents/" <> agent_symbol.to_string(agent_symbol))
api.get(
AgentAuth(token),
"/agents/" <> agent_symbol.to_string(agent_symbol),
)
use response <- api.try_send(request)
case response.status {
200 -> api.parse_data_response(response, public_agent.decoder())

View file

@ -3,13 +3,14 @@ import gleam/option.{type Option}
import models/faction.{type Faction}
import models/faction_symbol.{type FactionSymbol}
import utils/api.{type ApiResponse, type PagedData}
import utils/auth.{type AgentToken, AgentAuth, NoAuth}
import utils/auth.{type AgentToken, AgentAuth}
pub fn list_factions(
token: AgentToken,
page: Option(Int),
limit: Option(Int),
) -> ApiResponse(PagedData(List(Faction))) {
let request = api.get_page(NoAuth, "/factions", page, limit)
let request = api.get_page(AgentAuth(token), "/factions", page, limit)
use response <- api.try_send(request)
case response.status {
200 ->
@ -18,9 +19,12 @@ pub fn list_factions(
}
}
pub fn get_faction(symbol: FactionSymbol) -> ApiResponse(Faction) {
pub fn get_faction(
token: AgentToken,
symbol: FactionSymbol,
) -> ApiResponse(Faction) {
let request =
api.get(NoAuth, "/factions/" <> faction_symbol.to_string(symbol))
api.get(AgentAuth(token), "/factions/" <> faction_symbol.to_string(symbol))
use response <- api.try_send(request)
case response.status {
200 -> api.parse_data_response(response, faction.decoder())
@ -32,6 +36,12 @@ pub type FactionReputation {
FactionReputation(symbol: FactionSymbol, reputation: Int)
}
fn faction_reputation_decoder() {
use symbol <- decode.field("symbol", faction_symbol.decoder())
use reputation <- decode.field("reputation", decode.int)
decode.success(FactionReputation(symbol:, reputation:))
}
pub fn get_my_factions(
token: AgentToken,
page: Option(Int),
@ -43,11 +53,7 @@ pub fn get_my_factions(
200 ->
api.parse_paged_data_response(
response,
decode.list({
use symbol <- decode.field("symbol", faction_symbol.decoder())
use reputation <- decode.field("reputation", decode.int)
decode.success(FactionReputation(symbol:, reputation:))
}),
decode.list(faction_reputation_decoder()),
)
_ -> api.parse_error_response(response)
}

View file

@ -16,13 +16,14 @@ import models/waypoint_symbol.{type WaypointSymbol}
import models/waypoint_trait_symbol.{type WaypointTraitSymbol}
import models/waypoint_type.{type WaypointType}
import utils/api.{type ApiResponse, type PagedData}
import utils/auth.{NoAuth}
import utils/auth.{type AgentToken, AgentAuth}
pub fn list_systems(
token: AgentToken,
page: Option(Int),
limit: Option(Int),
) -> ApiResponse(PagedData(List(System))) {
let request = api.get_page(NoAuth, "/systems", page, limit)
let request = api.get_page(AgentAuth(token), "/systems", page, limit)
use response <- api.try_send(request)
case response.status {
200 ->
@ -31,9 +32,15 @@ pub fn list_systems(
}
}
pub fn get_system(system_symbol: SystemSymbol) -> ApiResponse(System) {
pub fn get_system(
token: AgentToken,
system_symbol: SystemSymbol,
) -> ApiResponse(System) {
let request =
api.get(NoAuth, "/systems/" <> system_symbol.to_string(system_symbol))
api.get(
AgentAuth(token),
"/systems/" <> system_symbol.to_string(system_symbol),
)
use response <- api.try_send(request)
case response.status {
200 -> api.parse_data_response(response, system.decoder())
@ -42,6 +49,7 @@ pub fn get_system(system_symbol: SystemSymbol) -> ApiResponse(System) {
}
pub fn list_system_waypoints(
token: AgentToken,
system_symbol: SystemSymbol,
page: Option(Int),
limit: Option(Int),
@ -58,7 +66,7 @@ pub fn list_system_waypoints(
}
let request =
api.get_page_with_query(
NoAuth,
AgentAuth(token),
"/systems/" <> system_symbol.to_string(system_symbol) <> "/waypoints",
page,
limit,
@ -73,12 +81,13 @@ pub fn list_system_waypoints(
}
pub fn get_waypoint(
token: AgentToken,
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
) -> ApiResponse(Waypoint) {
let request =
api.get(
NoAuth,
AgentAuth(token),
"/systems/"
<> system_symbol.to_string(system_symbol)
<> "/waypoints/"
@ -92,12 +101,13 @@ pub fn get_waypoint(
}
pub fn get_construction_site(
token: AgentToken,
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
) -> ApiResponse(Construction) {
let request =
api.get(
NoAuth,
AgentAuth(token),
"/systems/"
<> system_symbol.to_string(system_symbol)
<> "/waypoints/"
@ -116,6 +126,7 @@ pub type ConstructionSiteSupplied {
}
pub fn supply_construction_site(
token: AgentToken,
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
ship_symbol: ShipSymbol,
@ -124,7 +135,7 @@ pub fn supply_construction_site(
) -> ApiResponse(ConstructionSiteSupplied) {
let request =
api.post_json(
NoAuth,
AgentAuth(token),
"/systems/"
<> system_symbol.to_string(system_symbol)
<> "/waypoints/"
@ -149,12 +160,13 @@ pub fn supply_construction_site(
}
pub fn get_market(
token: AgentToken,
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
) -> ApiResponse(Market) {
let request =
api.get(
NoAuth,
AgentAuth(token),
"/systems/"
<> system_symbol.to_string(system_symbol)
<> "/waypoints/"
@ -169,12 +181,13 @@ pub fn get_market(
}
pub fn get_jump_gate(
token: AgentToken,
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
) -> ApiResponse(JumpGate) {
let request =
api.get(
NoAuth,
AgentAuth(token),
"/systems/"
<> system_symbol.to_string(system_symbol)
<> "/waypoints/"
@ -189,16 +202,17 @@ pub fn get_jump_gate(
}
pub fn get_shipyard(
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
token,
system_symbol,
waypoint_symbol,
) -> ApiResponse(Shipyard) {
let request =
api.get(
NoAuth,
AgentAuth(token),
"/systems/"
<> system_symbol.to_string(system_symbol)
<> system_symbol
<> "/waypoints/"
<> waypoint_symbol.to_string(waypoint_symbol)
<> waypoint_symbol
<> "/shipyard",
)
use response <- api.try_send(request)