Simplify response types

This commit is contained in:
LilyRose2798 2025-06-18 14:44:55 +10:00
parent 2c607f64bc
commit 17631a1a0c
10 changed files with 508 additions and 863 deletions

View file

@ -1,4 +1,4 @@
import gleam/dynamic/decode.{type Decoder}
import gleam/dynamic/decode
import gleam/json
import models/account.{type Account}
import models/agent.{type Agent}
@ -10,26 +10,21 @@ import models/ship.{type Ship}
import utils/api.{type ApiResponse}
import utils/auth.{type AccountToken, type AgentToken, AccountAuth, AgentAuth}
pub type GetAccountResponse {
GetAccountResponse(account: Account)
}
fn get_account_response_decoder() -> Decoder(GetAccountResponse) {
use account <- decode.field("account", account.decoder())
decode.success(GetAccountResponse(account:))
}
pub fn get_account(token: AgentToken) -> ApiResponse(GetAccountResponse) {
pub fn get_account(token: AgentToken) -> ApiResponse(Account) {
let request = api.get(AgentAuth(token), "/my/account")
use response <- api.try_send(request)
case response.status {
200 -> api.parse_data_response(response, get_account_response_decoder())
200 ->
api.parse_data_response(
response,
decode.field("account", account.decoder(), decode.success),
)
_ -> api.parse_error_response(response)
}
}
pub type RegisterNewAgentResponse {
RegisterNewAgentResponse(
pub type AgentRegistered {
AgentRegistered(
token: AgentToken,
agent: Agent,
faction: Faction,
@ -38,26 +33,11 @@ pub type RegisterNewAgentResponse {
)
}
fn register_new_agent_response_decoder() -> Decoder(RegisterNewAgentResponse) {
use token <- decode.field("token", auth.agent_token_decoder())
use agent <- decode.field("agent", agent.decoder())
use faction <- decode.field("faction", faction.decoder())
use contract <- decode.field("contract", contract.decoder())
use ships <- decode.field("ships", decode.list(ship.decoder()))
decode.success(RegisterNewAgentResponse(
token:,
agent:,
faction:,
contract:,
ships:,
))
}
pub fn register_new_agent(
token: AccountToken,
agent_symbol: AgentSymbol,
faction_symbol: FactionSymbol,
) -> ApiResponse(RegisterNewAgentResponse) {
) -> ApiResponse(AgentRegistered) {
let request =
api.post_json(
AccountAuth(token),
@ -70,7 +50,20 @@ pub fn register_new_agent(
use response <- api.try_send(request)
case response.status {
201 ->
api.parse_data_response(response, register_new_agent_response_decoder())
api.parse_data_response(response, {
use token <- decode.field("token", auth.agent_token_decoder())
use agent <- decode.field("agent", agent.decoder())
use faction <- decode.field("faction", faction.decoder())
use contract <- decode.field("contract", contract.decoder())
use ships <- decode.field("ships", decode.list(ship.decoder()))
decode.success(AgentRegistered(
token:,
agent:,
faction:,
contract:,
ships:,
))
})
_ -> api.parse_error_response(response)
}
}

View file

@ -1,4 +1,4 @@
import gleam/dynamic/decode.{type Decoder}
import gleam/dynamic/decode
import gleam/option.{type Option}
import models/agent.{type Agent}
import models/agent_event.{type AgentEvent}
@ -7,45 +7,27 @@ import models/public_agent.{type PublicAgent}
import utils/api.{type ApiResponse, type PagedData}
import utils/auth.{type AgentToken, AgentAuth}
pub type ListPublicAgentsResponse {
ListPublicAgentsResponse(agents: List(PublicAgent))
}
fn list_public_agents_response_decoder() -> Decoder(ListPublicAgentsResponse) {
use agents <- decode.then(decode.list(public_agent.decoder()))
decode.success(ListPublicAgentsResponse(agents:))
}
pub fn list_public_agents(
token: AgentToken,
page: Option(Int),
limit: Option(Int),
) -> ApiResponse(PagedData(ListPublicAgentsResponse)) {
) -> ApiResponse(PagedData(List(PublicAgent))) {
let request = api.get_page(AgentAuth(token), "/agents", page, limit)
use response <- api.try_send(request)
case response.status {
200 ->
api.parse_paged_data_response(
response,
list_public_agents_response_decoder(),
decode.list(public_agent.decoder()),
)
_ -> api.parse_error_response(response)
}
}
pub type GetPublicAgentResponse {
GetPublicAgentResponse(agent: PublicAgent)
}
fn get_public_agent_response_decoder() -> Decoder(GetPublicAgentResponse) {
use agent <- decode.then(public_agent.decoder())
decode.success(GetPublicAgentResponse(agent:))
}
pub fn get_public_agent(
token: AgentToken,
agent_symbol: AgentSymbol,
) -> ApiResponse(GetPublicAgentResponse) {
) -> ApiResponse(PublicAgent) {
let request =
api.get(
AgentAuth(token),
@ -53,47 +35,25 @@ pub fn get_public_agent(
)
use response <- api.try_send(request)
case response.status {
200 ->
api.parse_data_response(response, get_public_agent_response_decoder())
200 -> api.parse_data_response(response, public_agent.decoder())
_ -> api.parse_error_response(response)
}
}
pub type GetAgentResponse {
GetAgentResponse(agent: Agent)
}
fn get_agent_response_decoder() -> decode.Decoder(GetAgentResponse) {
use agent <- decode.then(agent.decoder())
decode.success(GetAgentResponse(agent:))
}
pub fn get_agent(token: AgentToken) -> ApiResponse(GetAgentResponse) {
pub fn get_agent(token: AgentToken) -> ApiResponse(Agent) {
let request = api.get(AgentAuth(token), "/my/agent")
use response <- api.try_send(request)
case response.status {
200 -> api.parse_data_response(response, get_agent_response_decoder())
200 -> api.parse_data_response(response, agent.decoder())
_ -> api.parse_error_response(response)
}
}
pub type GetAgentEventsResponse {
GetAgentEventsResponse(events: List(AgentEvent))
}
fn get_agent_events_response_decoder() -> Decoder(GetAgentEventsResponse) {
use events <- decode.then(decode.list(agent_event.decoder()))
decode.success(GetAgentEventsResponse(events:))
}
pub fn get_agent_events(
token: AgentToken,
) -> ApiResponse(GetAgentEventsResponse) {
pub fn get_agent_events(token: AgentToken) -> ApiResponse(List(AgentEvent)) {
let request = api.get(AgentAuth(token), "/my/agent/events")
use response <- api.try_send(request)
case response.status {
200 ->
api.parse_data_response(response, get_agent_events_response_decoder())
200 -> api.parse_data_response(response, decode.list(agent_event.decoder()))
_ -> api.parse_error_response(response)
}
}

View file

@ -1,5 +1,5 @@
import endpoints/fleet
import gleam/dynamic/decode.{type Decoder}
import gleam/dynamic/decode
import gleam/json
import gleam/option.{type Option}
import models/agent.{type Agent}
@ -11,42 +11,24 @@ import models/trade_symbol.{type TradeSymbol}
import utils/api.{type ApiResponse, type PagedData}
import utils/auth.{type AgentToken, AgentAuth}
pub type ListContractsResponse {
ListContractsResponse(contracts: List(Contract))
}
fn list_contracts_response_decoder() -> Decoder(ListContractsResponse) {
use contracts <- decode.then(decode.list(contract.decoder()))
decode.success(ListContractsResponse(contracts:))
}
pub fn list_contracts(
token: AgentToken,
page: Option(Int),
limit: Option(Int),
) -> ApiResponse(PagedData(ListContractsResponse)) {
) -> ApiResponse(PagedData(List(Contract))) {
let request = api.get_page(AgentAuth(token), "/my/contracts", page, limit)
use response <- api.try_send(request)
case response.status {
200 ->
api.parse_paged_data_response(response, list_contracts_response_decoder())
api.parse_paged_data_response(response, decode.list(contract.decoder()))
_ -> api.parse_error_response(response)
}
}
pub type GetContractResponse {
GetContractResponse(contract: Contract)
}
fn get_contract_response_decoder() -> Decoder(GetContractResponse) {
use contract <- decode.then(contract.decoder())
decode.success(GetContractResponse(contract:))
}
pub fn get_contract(
token: AgentToken,
contract_id: ContractId,
) -> ApiResponse(GetContractResponse) {
) -> ApiResponse(Contract) {
let request =
api.get(
AgentAuth(token),
@ -54,25 +36,19 @@ pub fn get_contract(
)
use response <- api.try_send(request)
case response.status {
200 -> api.parse_data_response(response, get_contract_response_decoder())
200 -> api.parse_data_response(response, contract.decoder())
_ -> api.parse_error_response(response)
}
}
pub type AcceptContractResponse {
AcceptContractResponse(contract: Contract, agent: Agent)
}
fn accept_contract_response_decoder() -> Decoder(AcceptContractResponse) {
use contract <- decode.field("contract", contract.decoder())
use agent <- decode.field("agent", agent.decoder())
decode.success(AcceptContractResponse(contract:, agent:))
pub type ContractAccepted {
ContractAccepted(contract: Contract, agent: Agent)
}
pub fn accept_contract(
token: AgentToken,
contract_id: ContractId,
) -> ApiResponse(AcceptContractResponse) {
) -> ApiResponse(ContractAccepted) {
let request =
api.post(
AgentAuth(token),
@ -80,25 +56,24 @@ pub fn accept_contract(
)
use response <- api.try_send(request)
case response.status {
200 -> api.parse_data_response(response, accept_contract_response_decoder())
200 ->
api.parse_data_response(response, {
use contract <- decode.field("contract", contract.decoder())
use agent <- decode.field("agent", agent.decoder())
decode.success(ContractAccepted(contract:, agent:))
})
_ -> api.parse_error_response(response)
}
}
pub type FulfillContractResponse {
FulfillContractResponse(contract: Contract, agent: Agent)
}
fn fulfill_contract_response_decoder() -> Decoder(FulfillContractResponse) {
use contract <- decode.field("contract", contract.decoder())
use agent <- decode.field("agent", agent.decoder())
decode.success(FulfillContractResponse(contract:, agent:))
pub type ContractFulfilled {
ContractFulfilled(contract: Contract, agent: Agent)
}
pub fn fulfill_contract(
token: AgentToken,
contract_id: ContractId,
) -> ApiResponse(FulfillContractResponse) {
) -> ApiResponse(ContractFulfilled) {
let request =
api.post(
AgentAuth(token),
@ -107,21 +82,17 @@ pub fn fulfill_contract(
use response <- api.try_send(request)
case response.status {
200 ->
api.parse_data_response(response, fulfill_contract_response_decoder())
api.parse_data_response(response, {
use contract <- decode.field("contract", contract.decoder())
use agent <- decode.field("agent", agent.decoder())
decode.success(ContractFulfilled(contract:, agent:))
})
_ -> api.parse_error_response(response)
}
}
pub type DeliverContractCargoResponse {
DeliverContractCargoResponse(contract: Contract, cargo: ShipCargo)
}
fn deliver_contract_cargo_response_decoder() -> Decoder(
DeliverContractCargoResponse,
) {
use contract <- decode.field("contract", contract.decoder())
use cargo <- decode.field("cargo", ship_cargo.decoder())
decode.success(DeliverContractCargoResponse(contract:, cargo:))
pub type ContractCargoDelivered {
ContractCargoDelivered(contract: Contract, cargo: ShipCargo)
}
pub fn deliver_contract_cargo(
@ -130,7 +101,7 @@ pub fn deliver_contract_cargo(
ship_symbol: ShipSymbol,
trade_symbol: TradeSymbol,
units: Int,
) -> ApiResponse(DeliverContractCargoResponse) {
) -> ApiResponse(ContractCargoDelivered) {
let request =
api.post_json(
AgentAuth(token),
@ -144,10 +115,11 @@ pub fn deliver_contract_cargo(
use response <- api.try_send(request)
case response.status {
200 ->
api.parse_data_response(
response,
deliver_contract_cargo_response_decoder(),
)
api.parse_data_response(response, {
use contract <- decode.field("contract", contract.decoder())
use cargo <- decode.field("cargo", ship_cargo.decoder())
decode.success(ContractCargoDelivered(contract:, cargo:))
})
_ -> api.parse_error_response(response)
}
}

View file

@ -1,30 +1,28 @@
import gleam/dict.{type Dict}
import gleam/dynamic/decode.{type Decoder}
import gleam/dynamic/decode
import models/trade_symbol.{type TradeSymbol}
import utils/api.{type ApiResponse}
import utils/auth.{NoAuth}
pub type GetSupplyChainResponse {
GetSupplyChainResponse(
export_to_import_map: Dict(TradeSymbol, List(TradeSymbol)),
)
}
pub type ExportToImportMap =
Dict(TradeSymbol, List(TradeSymbol))
fn get_supply_chain_response_decoder() -> Decoder(GetSupplyChainResponse) {
use export_to_import_map <- decode.field(
"exportToImportMap",
decode.dict(trade_symbol.decoder(), decode.list(trade_symbol.decoder())),
)
decode.success(GetSupplyChainResponse(export_to_import_map:))
}
pub fn get_supply_chain() -> ApiResponse(GetSupplyChainResponse) {
pub fn get_supply_chain() -> ApiResponse(ExportToImportMap) {
let request = api.get(NoAuth, "/market/supply-chain")
use response <- api.try_send(request)
echo response.body
case response.status {
200 ->
api.parse_data_response(response, get_supply_chain_response_decoder())
api.parse_data_response(
response,
decode.field(
"exportToImportMap",
decode.dict(
trade_symbol.decoder(),
decode.list(trade_symbol.decoder()),
),
decode.success,
),
)
_ -> api.parse_error_response(response)
}
}

View file

@ -1,51 +1,33 @@
import gleam/dynamic/decode.{type Decoder}
import gleam/dynamic/decode
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}
pub type ListFactionsResponse {
ListFactionsResponse(factions: List(Faction))
}
fn list_factions_response_decoder() -> Decoder(ListFactionsResponse) {
use factions <- decode.then(decode.list(faction.decoder()))
decode.success(ListFactionsResponse(factions:))
}
pub fn list_factions(
token: AgentToken,
page: Option(Int),
limit: Option(Int),
) -> ApiResponse(PagedData(ListFactionsResponse)) {
) -> ApiResponse(PagedData(List(Faction))) {
let request = api.get_page(AgentAuth(token), "/factions", page, limit)
use response <- api.try_send(request)
case response.status {
200 ->
api.parse_paged_data_response(response, list_factions_response_decoder())
api.parse_paged_data_response(response, decode.list(faction.decoder()))
_ -> api.parse_error_response(response)
}
}
pub type GetFactionResponse {
GetFactionResponse(faction: Faction)
}
fn get_faction_response_decoder() -> Decoder(GetFactionResponse) {
use faction <- decode.then(faction.decoder())
decode.success(GetFactionResponse(faction:))
}
pub fn get_faction(
token: AgentToken,
symbol: FactionSymbol,
) -> ApiResponse(GetFactionResponse) {
) -> ApiResponse(Faction) {
let request =
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, get_faction_response_decoder())
200 -> api.parse_data_response(response, faction.decoder())
_ -> api.parse_error_response(response)
}
}
@ -60,29 +42,18 @@ fn faction_reputation_decoder() {
decode.success(FactionReputation(symbol:, reputation:))
}
pub type GetMyFactionsResponse {
GetMyFactionsResponse(faction_reputations: List(FactionReputation))
}
fn get_my_factions_response_decoder() -> Decoder(GetMyFactionsResponse) {
use faction_reputations <- decode.then(
decode.list(faction_reputation_decoder()),
)
decode.success(GetMyFactionsResponse(faction_reputations:))
}
pub fn get_my_factions(
token: AgentToken,
page: Option(Int),
limit: Option(Int),
) -> ApiResponse(PagedData(GetMyFactionsResponse)) {
) -> ApiResponse(PagedData(List(FactionReputation))) {
let request = api.get_page(AgentAuth(token), "/my/factions", page, limit)
use response <- api.try_send(request)
case response.status {
200 ->
api.parse_paged_data_response(
response,
get_my_factions_response_decoder(),
decode.list(faction_reputation_decoder()),
)
_ -> api.parse_error_response(response)
}

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,8 @@
import birl.{type Time}
import gleam/dynamic/decode.{type Decoder}
import gleam/option.{type Option}
import gleam/uri.{type Uri}
import models/agent_symbol.{type AgentSymbol}
import utils/api.{type ApiResponse}
import utils/auth.{NoAuth}
@ -27,34 +30,34 @@ fn stats_decoder() -> Decoder(Stats) {
}
pub type Health {
Health(last_market_update: Option(String))
Health(last_market_update: Option(Time))
}
fn health_decoder() -> Decoder(Health) {
use last_market_update <- decode.optional_field(
"lastMarketUpdate",
option.None,
decode.optional(decode.string),
decode.optional(api.time_decoder()),
)
decode.success(Health(last_market_update:))
}
pub type CreditLeaderboardEntry {
CreditLeaderboardEntry(agent_symbol: String, credits: Int)
CreditLeaderboardEntry(agent_symbol: AgentSymbol, credits: Int)
}
fn credit_leaderboard_entry_decoder() -> Decoder(CreditLeaderboardEntry) {
use agent_symbol <- decode.field("agentSymbol", decode.string)
use agent_symbol <- decode.field("agentSymbol", agent_symbol.decoder())
use credits <- decode.field("credits", decode.int)
decode.success(CreditLeaderboardEntry(agent_symbol:, credits:))
}
pub type ChartLeaderboardEntry {
ChartLeaderboardEntry(agent_symbol: String, chart_count: Int)
ChartLeaderboardEntry(agent_symbol: AgentSymbol, chart_count: Int)
}
fn chart_leaderboard_entry_decoder() -> Decoder(ChartLeaderboardEntry) {
use agent_symbol <- decode.field("agentSymbol", decode.string)
use agent_symbol <- decode.field("agentSymbol", agent_symbol.decoder())
use chart_count <- decode.field("chartCount", decode.int)
decode.success(ChartLeaderboardEntry(agent_symbol:, chart_count:))
}
@ -79,11 +82,11 @@ fn leaderboards_decoder() -> Decoder(Leaderboards) {
}
pub type ServerResets {
ServerResets(next: String, frequency: String)
ServerResets(next: Time, frequency: String)
}
fn server_resets_decoder() -> Decoder(ServerResets) {
use next <- decode.field("next", decode.string)
use next <- decode.field("next", api.time_decoder())
use frequency <- decode.field("frequency", decode.string)
decode.success(ServerResets(next:, frequency:))
}
@ -99,20 +102,26 @@ fn announcement_decoder() -> Decoder(Announcement) {
}
pub type Link {
Link(name: String, url: String)
Link(name: String, url: Uri)
}
fn link_decoder() -> Decoder(Link) {
use name <- decode.field("name", decode.string)
use url <- decode.field("url", decode.string)
use url <- decode.field("url", {
use value <- decode.then(decode.string)
case uri.parse(value) {
Ok(time) -> decode.success(time)
Error(Nil) -> decode.failure(uri.empty, "Uri")
}
})
decode.success(Link(name:, url:))
}
pub type GetServerStatusResponse {
GetServerStatusResponse(
pub type ServerStatus {
ServerStatus(
status: String,
version: String,
reset_date: String,
reset_date: Time,
description: String,
stats: Stats,
health: Health,
@ -123,10 +132,16 @@ pub type GetServerStatusResponse {
)
}
fn get_server_status_response_decoder() -> Decoder(GetServerStatusResponse) {
fn server_status_decoder() -> Decoder(ServerStatus) {
use status <- decode.field("status", decode.string)
use version <- decode.field("version", decode.string)
use reset_date <- decode.field("resetDate", decode.string)
use reset_date <- decode.field("resetDate", {
use value <- decode.then(decode.string)
case birl.from_naive(value) {
Ok(time) -> decode.success(time)
Error(Nil) -> decode.failure(birl.now(), "Time")
}
})
use description <- decode.field("description", decode.string)
use stats <- decode.field("stats", stats_decoder())
use health <- decode.field("health", health_decoder())
@ -137,7 +152,7 @@ fn get_server_status_response_decoder() -> Decoder(GetServerStatusResponse) {
decode.list(announcement_decoder()),
)
use links <- decode.field("links", decode.list(link_decoder()))
decode.success(GetServerStatusResponse(
decode.success(ServerStatus(
status:,
version:,
reset_date:,
@ -151,11 +166,11 @@ fn get_server_status_response_decoder() -> Decoder(GetServerStatusResponse) {
))
}
pub fn get_server_status() -> ApiResponse(GetServerStatusResponse) {
pub fn get_server_status() -> ApiResponse(ServerStatus) {
let request = api.get(NoAuth, "/")
use response <- api.try_send(request)
case response.status {
200 -> api.parse_response(response, get_server_status_response_decoder())
200 -> api.parse_response(response, server_status_decoder())
_ -> api.parse_error_response(response)
}
}
@ -170,23 +185,19 @@ fn error_code_decoder() -> Decoder(ErrorCode) {
decode.success(ErrorCode(code:, name:))
}
pub type ListErrorCodesResponse {
ListErrorCodesResponse(error_codes: List(ErrorCode))
}
fn list_error_codes_response_decoder() -> Decoder(ListErrorCodesResponse) {
use error_codes <- decode.field(
"errorCodes",
decode.list(error_code_decoder()),
)
decode.success(ListErrorCodesResponse(error_codes:))
}
pub fn list_error_codes() -> ApiResponse(ListErrorCodesResponse) {
pub fn list_error_codes() -> ApiResponse(List(ErrorCode)) {
let request = api.get(NoAuth, "/error-codes")
use response <- api.try_send(request)
case response.status {
200 -> api.parse_response(response, list_error_codes_response_decoder())
200 ->
api.parse_response(
response,
decode.field(
"errorCodes",
decode.list(error_code_decoder()),
decode.success,
),
)
_ -> api.parse_error_response(response)
}
}

View file

@ -1,4 +1,4 @@
import gleam/dynamic/decode.{type Decoder}
import gleam/dynamic/decode
import gleam/json
import gleam/list
import gleam/option.{type Option}
@ -18,42 +18,24 @@ import models/waypoint_type.{type WaypointType}
import utils/api.{type ApiResponse, type PagedData}
import utils/auth.{type AgentToken, AgentAuth}
pub type ListSystemsResponse {
ListSystemsResponse(systems: List(System))
}
fn list_systems_response_decoder() -> Decoder(ListSystemsResponse) {
use systems <- decode.then(decode.list(system.decoder()))
decode.success(ListSystemsResponse(systems:))
}
pub fn list_systems(
token: AgentToken,
page: Option(Int),
limit: Option(Int),
) -> ApiResponse(PagedData(ListSystemsResponse)) {
) -> ApiResponse(PagedData(List(System))) {
let request = api.get_page(AgentAuth(token), "/systems", page, limit)
use response <- api.try_send(request)
case response.status {
200 ->
api.parse_paged_data_response(response, list_systems_response_decoder())
api.parse_paged_data_response(response, decode.list(system.decoder()))
_ -> api.parse_error_response(response)
}
}
pub type GetSystemResponse {
GetSystemResponse(system: System)
}
fn get_system_response_decoder() -> Decoder(GetSystemResponse) {
use system <- decode.then(system.decoder())
decode.success(GetSystemResponse(system:))
}
pub fn get_system(
token: AgentToken,
system_symbol: SystemSymbol,
) -> ApiResponse(GetSystemResponse) {
) -> ApiResponse(System) {
let request =
api.get(
AgentAuth(token),
@ -61,22 +43,11 @@ pub fn get_system(
)
use response <- api.try_send(request)
case response.status {
200 -> api.parse_data_response(response, get_system_response_decoder())
200 -> api.parse_data_response(response, system.decoder())
_ -> api.parse_error_response(response)
}
}
pub type ListSystemWaypointsResponse {
ListSystemWaypointsResponse(waypoints: List(Waypoint))
}
fn list_system_waypoints_response_decoder() -> Decoder(
ListSystemWaypointsResponse,
) {
use waypoints <- decode.then(decode.list(waypoint.decoder()))
decode.success(ListSystemWaypointsResponse(waypoints:))
}
pub fn list_system_waypoints(
token: AgentToken,
system_symbol: SystemSymbol,
@ -84,7 +55,7 @@ pub fn list_system_waypoints(
limit: Option(Int),
type_: Option(WaypointType),
traits: List(WaypointTraitSymbol),
) -> ApiResponse(PagedData(ListSystemWaypointsResponse)) {
) -> ApiResponse(PagedData(List(Waypoint))) {
let query =
list.map(traits, fn(trait) {
#("traits", waypoint_trait_symbol.to_string(trait))
@ -94,7 +65,7 @@ pub fn list_system_waypoints(
option.None -> query
}
let request =
api.get_page_query(
api.get_page_with_query(
AgentAuth(token),
"/systems/" <> system_symbol.to_string(system_symbol) <> "/waypoints",
page,
@ -104,28 +75,16 @@ pub fn list_system_waypoints(
use response <- api.try_send(request)
case response.status {
200 ->
api.parse_paged_data_response(
response,
list_system_waypoints_response_decoder(),
)
api.parse_paged_data_response(response, decode.list(waypoint.decoder()))
_ -> api.parse_error_response(response)
}
}
pub type GetWaypointResponse {
GetWaypointResponse(waypoint: Waypoint)
}
fn get_waypoint_response_decoder() -> Decoder(GetWaypointResponse) {
use waypoint <- decode.then(waypoint.decoder())
decode.success(GetWaypointResponse(waypoint:))
}
pub fn get_waypoint(
token: AgentToken,
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
) -> ApiResponse(GetWaypointResponse) {
) -> ApiResponse(Waypoint) {
let request =
api.get(
AgentAuth(token),
@ -136,27 +95,16 @@ pub fn get_waypoint(
)
use response <- api.try_send(request)
case response.status {
200 -> api.parse_data_response(response, get_waypoint_response_decoder())
200 -> api.parse_data_response(response, waypoint.decoder())
_ -> api.parse_error_response(response)
}
}
pub type GetConstructionSiteResponse {
GetConstructionSiteResponse(construction: Construction)
}
fn get_construction_site_response_decoder() -> Decoder(
GetConstructionSiteResponse,
) {
use construction <- decode.then(construction.decoder())
decode.success(GetConstructionSiteResponse(construction:))
}
pub fn get_construction_site(
token: AgentToken,
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
) -> ApiResponse(GetConstructionSiteResponse) {
) -> ApiResponse(Construction) {
let request =
api.get(
AgentAuth(token),
@ -168,25 +116,13 @@ pub fn get_construction_site(
)
use response <- api.try_send(request)
case response.status {
200 ->
api.parse_data_response(
response,
get_construction_site_response_decoder(),
)
200 -> api.parse_data_response(response, construction.decoder())
_ -> api.parse_error_response(response)
}
}
pub type SupplyConstructionSiteResponse {
SupplyConstructionSiteResponse(construction: Construction, cargo: ShipCargo)
}
fn supply_construction_site_response_decoder() -> Decoder(
SupplyConstructionSiteResponse,
) {
use construction <- decode.field("construction", construction.decoder())
use cargo <- decode.field("cargo", ship_cargo.decoder())
decode.success(SupplyConstructionSiteResponse(construction:, cargo:))
pub type ConstructionSiteSupplied {
ConstructionSiteSupplied(construction: Construction, cargo: ShipCargo)
}
pub fn supply_construction_site(
@ -196,7 +132,7 @@ pub fn supply_construction_site(
ship_symbol: ShipSymbol,
trade_symbol: TradeSymbol,
units: Int,
) -> ApiResponse(SupplyConstructionSiteResponse) {
) -> ApiResponse(ConstructionSiteSupplied) {
let request =
api.post_json(
AgentAuth(token),
@ -214,28 +150,20 @@ pub fn supply_construction_site(
use response <- api.try_send(request)
case response.status {
201 ->
api.parse_data_response(
response,
supply_construction_site_response_decoder(),
)
api.parse_data_response(response, {
use construction <- decode.field("construction", construction.decoder())
use cargo <- decode.field("cargo", ship_cargo.decoder())
decode.success(ConstructionSiteSupplied(construction:, cargo:))
})
_ -> api.parse_error_response(response)
}
}
pub type GetMarketResponse {
GetMarketResponse(market: Market)
}
fn get_market_response_decoder() -> Decoder(GetMarketResponse) {
use market <- decode.then(market.decoder())
decode.success(GetMarketResponse(market:))
}
pub fn get_market(
token: AgentToken,
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
) -> ApiResponse(GetMarketResponse) {
) -> ApiResponse(Market) {
let request =
api.get(
AgentAuth(token),
@ -247,25 +175,16 @@ pub fn get_market(
)
use response <- api.try_send(request)
case response.status {
200 -> api.parse_data_response(response, get_market_response_decoder())
200 -> api.parse_data_response(response, market.decoder())
_ -> api.parse_error_response(response)
}
}
pub type GetJumpGateResponse {
GetJumpGateResponse(jump_gate: JumpGate)
}
fn get_jump_gate_response_decoder() -> Decoder(GetJumpGateResponse) {
use jump_gate <- decode.then(jump_gate.decoder())
decode.success(GetJumpGateResponse(jump_gate:))
}
pub fn get_jump_gate(
token: AgentToken,
system_symbol: SystemSymbol,
waypoint_symbol: WaypointSymbol,
) -> ApiResponse(GetJumpGateResponse) {
) -> ApiResponse(JumpGate) {
let request =
api.get(
AgentAuth(token),
@ -277,25 +196,16 @@ pub fn get_jump_gate(
)
use response <- api.try_send(request)
case response.status {
200 -> api.parse_data_response(response, get_jump_gate_response_decoder())
200 -> api.parse_data_response(response, jump_gate.decoder())
_ -> api.parse_error_response(response)
}
}
pub type GetShipyardResponse {
GetShipyardResponse(shipyard: Shipyard)
}
fn get_shipyard_response_decoder() -> Decoder(GetShipyardResponse) {
use shipyard <- decode.then(shipyard.decoder())
decode.success(GetShipyardResponse(shipyard:))
}
pub fn get_shipyard(
token,
system_symbol,
waypoint_symbol,
) -> ApiResponse(GetShipyardResponse) {
) -> ApiResponse(Shipyard) {
let request =
api.get(
AgentAuth(token),
@ -307,7 +217,7 @@ pub fn get_shipyard(
)
use response <- api.try_send(request)
case response.status {
200 -> api.parse_data_response(response, get_shipyard_response_decoder())
200 -> api.parse_data_response(response, shipyard.decoder())
_ -> api.parse_error_response(response)
}
}

View file

@ -118,7 +118,7 @@ pub fn get_page(
|> request.set_query(page_query_params([], page, limit))
}
pub fn get_page_query(
pub fn get_page_with_query(
auth_method: AuthMethod,
path: String,
page: Option(Int),

View file

@ -83,21 +83,19 @@ pub type JwtPayload {
)
}
fn reset_date_decoder() -> Decoder(Time) {
use value <- decode.then(decode.string)
case birl.from_naive(value) {
Ok(time) -> decode.success(time)
Error(Nil) -> decode.failure(birl.now(), "Time")
}
}
fn jwt_payload_decoder() -> Decoder(JwtPayload) {
use identifier <- decode.field("identifier", decode.string)
use version <- decode.field("version", decode.string)
use reset_date <- decode.optional_field(
"reset_date",
option.None,
decode.optional(reset_date_decoder()),
decode.optional({
use value <- decode.then(decode.string)
case birl.from_naive(value) {
Ok(time) -> decode.success(time)
Error(Nil) -> decode.failure(birl.now(), "Time")
}
}),
)
use issued_at_int <- decode.field("iat", decode.int)
let issued_at = birl.from_unix(issued_at_int)