From 17631a1a0c9e918a65ce69147b82301f49b5bf9d Mon Sep 17 00:00:00 2001 From: LilyRose2798 Date: Wed, 18 Jun 2025 14:44:55 +1000 Subject: [PATCH] Simplify response types --- src/endpoints/accounts.gleam | 55 +-- src/endpoints/agents.gleam | 58 +-- src/endpoints/contracts.gleam | 88 ++-- src/endpoints/data.gleam | 32 +- src/endpoints/factions.gleam | 43 +- src/endpoints/fleet.gleam | 860 ++++++++++++++-------------------- src/endpoints/global.gleam | 75 +-- src/endpoints/systems.gleam | 142 +----- src/utils/api.gleam | 2 +- src/utils/jwt.gleam | 16 +- 10 files changed, 508 insertions(+), 863 deletions(-) diff --git a/src/endpoints/accounts.gleam b/src/endpoints/accounts.gleam index 4d722b5..d7ffde4 100644 --- a/src/endpoints/accounts.gleam +++ b/src/endpoints/accounts.gleam @@ -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) } } diff --git a/src/endpoints/agents.gleam b/src/endpoints/agents.gleam index 7790f35..1cd7f58 100644 --- a/src/endpoints/agents.gleam +++ b/src/endpoints/agents.gleam @@ -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) } } diff --git a/src/endpoints/contracts.gleam b/src/endpoints/contracts.gleam index 539668a..d1616de 100644 --- a/src/endpoints/contracts.gleam +++ b/src/endpoints/contracts.gleam @@ -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) } } diff --git a/src/endpoints/data.gleam b/src/endpoints/data.gleam index d9083ce..8db1b18 100644 --- a/src/endpoints/data.gleam +++ b/src/endpoints/data.gleam @@ -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) } } diff --git a/src/endpoints/factions.gleam b/src/endpoints/factions.gleam index 3d4f11e..6b3ecd8 100644 --- a/src/endpoints/factions.gleam +++ b/src/endpoints/factions.gleam @@ -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) } diff --git a/src/endpoints/fleet.gleam b/src/endpoints/fleet.gleam index 08e506e..fc83b7b 100644 --- a/src/endpoints/fleet.gleam +++ b/src/endpoints/fleet.gleam @@ -38,48 +38,28 @@ import models/waypoint_symbol.{type WaypointSymbol} import utils/api.{type ApiResponse, type PagedData} import utils/auth.{type AgentToken, AgentAuth} -pub type ListShipsResponse { - ListShipsResponse(ships: List(Ship)) -} - -pub fn list_ships_decoder() -> Decoder(ListShipsResponse) { - use ships <- decode.then(decode.list(ship.decoder())) - decode.success(ListShipsResponse(ships:)) -} - pub fn list_ships( token: AgentToken, page: Option(Int), limit: Option(Int), -) -> ApiResponse(PagedData(ListShipsResponse)) { +) -> ApiResponse(PagedData(List(Ship))) { let request = api.get_page(AgentAuth(token), "/my/ships", page, limit) use response <- api.try_send(request) case response.status { - 200 -> api.parse_paged_data_response(response, list_ships_decoder()) + 200 -> api.parse_paged_data_response(response, decode.list(ship.decoder())) _ -> api.parse_error_response(response) } } -pub type PurchaseShipResponse { - PurchaseShipResponse( - ship: Ship, - agent: Agent, - transaction: ShipyardTransaction, - ) -} - -pub fn purchase_ship_decoder() -> Decoder(PurchaseShipResponse) { - use ship <- decode.field("ship", ship.decoder()) - use agent <- decode.field("agent", agent.decoder()) - use transaction <- decode.field("transaction", shipyard_transaction.decoder()) - decode.success(PurchaseShipResponse(ship:, agent:, transaction:)) +pub type ShipPurchased { + ShipPurchased(ship: Ship, agent: Agent, transaction: ShipyardTransaction) } pub fn purchase_ship( token: AgentToken, ship_type: ShipType, waypoint_symbol: WaypointSymbol, -) -> ApiResponse(PurchaseShipResponse) { +) -> ApiResponse(ShipPurchased) { let request = api.post_json( AgentAuth(token), @@ -91,24 +71,21 @@ pub fn purchase_ship( ) use response <- api.try_send(request) case response.status { - 201 -> api.parse_data_response(response, purchase_ship_decoder()) + 201 -> + api.parse_data_response(response, { + use ship <- decode.field("ship", ship.decoder()) + use agent <- decode.field("agent", agent.decoder()) + use transaction <- decode.field( + "transaction", + shipyard_transaction.decoder(), + ) + decode.success(ShipPurchased(ship:, agent:, transaction:)) + }) _ -> api.parse_error_response(response) } } -pub type GetShipResponse { - GetShipResponse(ship: Ship) -} - -pub fn get_ship_decoder() -> Decoder(GetShipResponse) { - use ship <- decode.then(ship.decoder()) - decode.success(GetShipResponse(ship:)) -} - -pub fn get_ship( - token: AgentToken, - ship_symbol: ShipSymbol, -) -> ApiResponse(GetShipResponse) { +pub fn get_ship(token: AgentToken, ship_symbol: ShipSymbol) -> ApiResponse(Ship) { let request = api.get( AgentAuth(token), @@ -116,13 +93,13 @@ pub fn get_ship( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, get_ship_decoder()) + 200 -> api.parse_data_response(response, ship.decoder()) _ -> api.parse_error_response(response) } } -pub type CreateChartResponse { - CreateChartResponse( +pub type ChartCreated { + ChartCreated( chart: Chart, waypoint: Waypoint, transaction: ChartTransaction, @@ -130,18 +107,10 @@ pub type CreateChartResponse { ) } -pub fn create_chart_response_decoder() -> Decoder(CreateChartResponse) { - use chart <- decode.field("chart", chart.decoder()) - use waypoint <- decode.field("waypoint", waypoint.decoder()) - use transaction <- decode.field("transaction", chart_transaction.decoder()) - use agent <- decode.field("agent", agent.decoder()) - decode.success(CreateChartResponse(chart:, waypoint:, transaction:, agent:)) -} - pub fn create_chart( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(CreateChartResponse) { +) -> ApiResponse(ChartCreated) { let request = api.post( AgentAuth(token), @@ -149,24 +118,25 @@ pub fn create_chart( ) use response <- api.try_send(request) case response.status { - 201 -> api.parse_data_response(response, create_chart_response_decoder()) + 201 -> + api.parse_data_response(response, { + use chart <- decode.field("chart", chart.decoder()) + use waypoint <- decode.field("waypoint", waypoint.decoder()) + use transaction <- decode.field( + "transaction", + chart_transaction.decoder(), + ) + use agent <- decode.field("agent", agent.decoder()) + decode.success(ChartCreated(chart:, waypoint:, transaction:, agent:)) + }) _ -> api.parse_error_response(response) } } -pub type NegotiateContractResponse { - NegotiateContractResponse(contract: Contract) -} - -fn negotiate_contract_response_decoder() -> Decoder(NegotiateContractResponse) { - use contract <- decode.field("contract", contract.decoder()) - decode.success(NegotiateContractResponse(contract:)) -} - pub fn negotiate_contract( token: AgentToken, ship_symbol: String, -) -> ApiResponse(NegotiateContractResponse) { +) -> ApiResponse(Contract) { let request = api.post( AgentAuth(token), @@ -175,25 +145,18 @@ pub fn negotiate_contract( use response <- api.try_send(request) case response.status { 201 -> - api.parse_data_response(response, negotiate_contract_response_decoder()) + api.parse_data_response( + response, + decode.field("contract", contract.decoder(), decode.success), + ) _ -> api.parse_error_response(response) } } -pub type GetShipCooldownResponse { - GetShipCooldownResponse(cooldown: Option(Cooldown)) -} - -fn get_ship_cooldown_response_decoder() -> Decoder(GetShipCooldownResponse) { - use cooldown <- decode.field("cooldown", cooldown.decoder()) - let cooldown = option.Some(cooldown) - decode.success(GetShipCooldownResponse(cooldown:)) -} - pub fn get_ship_cooldown( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(GetShipCooldownResponse) { +) -> ApiResponse(Option(Cooldown)) { let request = api.get( AgentAuth(token), @@ -202,25 +165,21 @@ pub fn get_ship_cooldown( use response <- api.try_send(request) case response.status { 200 -> - api.parse_data_response(response, get_ship_cooldown_response_decoder()) - 204 -> Ok(GetShipCooldownResponse(option.None)) + api.parse_data_response( + response, + decode.field("cooldown", cooldown.decoder(), fn(cooldown) { + decode.success(option.Some(cooldown)) + }), + ) + 204 -> Ok(option.None) _ -> api.parse_error_response(response) } } -pub type DockShipResponse { - DockShipResponse(nav: ShipNav) -} - -fn dock_ship_response_decoder() -> Decoder(DockShipResponse) { - use nav <- decode.field("nav", ship_nav.decoder()) - decode.success(DockShipResponse(nav:)) -} - pub fn dock_ship( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(DockShipResponse) { +) -> ApiResponse(ShipNav) { let request = api.post( AgentAuth(token), @@ -228,13 +187,17 @@ pub fn dock_ship( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, dock_ship_response_decoder()) + 200 -> + api.parse_data_response( + response, + decode.field("nav", ship_nav.decoder(), decode.success), + ) _ -> api.parse_error_response(response) } } -pub type ExtractResourcesResponse { - ExtractResourcesResponse( +pub type ResourcesExtracted { + ResourcesExtracted( extraction: Extraction, cooldown: Cooldown, cargo: ShipCargo, @@ -243,7 +206,7 @@ pub type ExtractResourcesResponse { ) } -fn extract_resources_response_decoder() -> Decoder(ExtractResourcesResponse) { +fn resources_extracted_decoder() -> Decoder(ResourcesExtracted) { use extraction <- decode.field("extraction", extraction.decoder()) use cooldown <- decode.field("cooldown", cooldown.decoder()) use cargo <- decode.field("cargo", ship_cargo.decoder()) @@ -255,7 +218,7 @@ fn extract_resources_response_decoder() -> Decoder(ExtractResourcesResponse) { "events", decode.list(ship_condition_event.decoder()), ) - decode.success(ExtractResourcesResponse( + decode.success(ResourcesExtracted( extraction:, cooldown:, cargo:, @@ -267,7 +230,7 @@ fn extract_resources_response_decoder() -> Decoder(ExtractResourcesResponse) { pub fn extract_resources( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(ExtractResourcesResponse) { +) -> ApiResponse(ResourcesExtracted) { let request = api.post( AgentAuth(token), @@ -275,50 +238,16 @@ pub fn extract_resources( ) use response <- api.try_send(request) case response.status { - 201 -> - api.parse_data_response(response, extract_resources_response_decoder()) + 201 -> api.parse_data_response(response, resources_extracted_decoder()) _ -> api.parse_error_response(response) } } -pub type ExtractResourcesWithSurveyResponse { - ExtractResourcesWithSurveyResponse( - extraction: Extraction, - cooldown: Cooldown, - cargo: ShipCargo, - modifiers: List(WaypointModifier), - events: List(ShipConditionEvent), - ) -} - -fn extract_resources_with_survey_response_decoder() -> Decoder( - ExtractResourcesWithSurveyResponse, -) { - use extraction <- decode.field("extraction", extraction.decoder()) - use cooldown <- decode.field("cooldown", cooldown.decoder()) - use cargo <- decode.field("cargo", ship_cargo.decoder()) - use modifiers <- decode.field( - "modifiers", - decode.list(waypoint_modifier.decoder()), - ) - use events <- decode.field( - "events", - decode.list(ship_condition_event.decoder()), - ) - decode.success(ExtractResourcesWithSurveyResponse( - extraction:, - cooldown:, - cargo:, - modifiers:, - events:, - )) -} - pub fn extract_resources_with_survey( token: AgentToken, ship_symbol: ShipSymbol, survey: Survey, -) -> ApiResponse(ExtractResourcesWithSurveyResponse) { +) -> ApiResponse(ResourcesExtracted) { let request = api.post_json( AgentAuth(token), @@ -327,30 +256,17 @@ pub fn extract_resources_with_survey( ) use response <- api.try_send(request) case response.status { - 201 -> - api.parse_data_response( - response, - extract_resources_with_survey_response_decoder(), - ) + 201 -> api.parse_data_response(response, resources_extracted_decoder()) _ -> api.parse_error_response(response) } } -pub type JettisonCargoResponse { - JettisonCargoResponse(cargo: ShipCargo) -} - -fn jettison_cargo_response_decoder() -> Decoder(JettisonCargoResponse) { - use cargo <- decode.field("cargo", ship_cargo.decoder()) - decode.success(JettisonCargoResponse(cargo:)) -} - pub fn jettison_cargo( token: AgentToken, ship_symbol: ShipSymbol, trade_symbol: TradeSymbol, units: Int, -) -> ApiResponse(JettisonCargoResponse) { +) -> ApiResponse(ShipCargo) { let request = api.post_json( AgentAuth(token), @@ -362,13 +278,17 @@ pub fn jettison_cargo( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, jettison_cargo_response_decoder()) + 200 -> + api.parse_data_response( + response, + decode.field("cargo", ship_cargo.decoder(), decode.success), + ) _ -> api.parse_error_response(response) } } -pub type JumpShipResponse { - JumpShipResponse( +pub type ShipJumped { + ShipJumped( nav: ShipNav, cooldown: Cooldown, transaction: MarketTransaction, @@ -376,19 +296,11 @@ pub type JumpShipResponse { ) } -fn jump_ship_response_decoder() -> Decoder(JumpShipResponse) { - use nav <- decode.field("nav", ship_nav.decoder()) - use cooldown <- decode.field("cooldown", cooldown.decoder()) - use transaction <- decode.field("transaction", market_transaction.decoder()) - use agent <- decode.field("agent", agent.decoder()) - decode.success(JumpShipResponse(nav:, cooldown:, transaction:, agent:)) -} - pub fn jump_ship( token: AgentToken, ship_symbol: ShipSymbol, waypoint_symbol: WaypointSymbol, -) -> ApiResponse(JumpShipResponse) { +) -> ApiResponse(ShipJumped) { let request = api.post_json( AgentAuth(token), @@ -397,25 +309,35 @@ pub fn jump_ship( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, jump_ship_response_decoder()) + 200 -> + api.parse_data_response(response, { + use nav <- decode.field("nav", ship_nav.decoder()) + use cooldown <- decode.field("cooldown", cooldown.decoder()) + use transaction <- decode.field( + "transaction", + market_transaction.decoder(), + ) + use agent <- decode.field("agent", agent.decoder()) + decode.success(ShipJumped(nav:, cooldown:, transaction:, agent:)) + }) _ -> api.parse_error_response(response) } } -pub type ScanSystemsResponse { - ScanSystemsResponse(cooldown: Cooldown, systems: List(ScannedSystem)) +pub type Scan(data) { + Scan(cooldown: Cooldown, data: List(data)) } -fn scan_systems_response_decoder() -> Decoder(ScanSystemsResponse) { +fn scan_decoder(field: String, decoder: Decoder(data)) -> Decoder(Scan(data)) { use cooldown <- decode.field("cooldown", cooldown.decoder()) - use systems <- decode.field("systems", decode.list(scanned_system.decoder())) - decode.success(ScanSystemsResponse(cooldown:, systems:)) + use data <- decode.field(field, decode.list(decoder)) + decode.success(Scan(cooldown:, data:)) } pub fn scan_systems( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(ScanSystemsResponse) { +) -> ApiResponse(Scan(ScannedSystem)) { let request = api.post( AgentAuth(token), @@ -423,28 +345,19 @@ pub fn scan_systems( ) use response <- api.try_send(request) case response.status { - 201 -> api.parse_data_response(response, scan_systems_response_decoder()) + 201 -> + api.parse_data_response( + response, + scan_decoder("systems", scanned_system.decoder()), + ) _ -> api.parse_error_response(response) } } -pub type ScanWaypointsResponse { - ScanWaypointsResponse(cooldown: Cooldown, waypoints: List(ScannedWaypoint)) -} - -fn scan_waypoints_response_decoder() -> Decoder(ScanWaypointsResponse) { - use cooldown <- decode.field("cooldown", cooldown.decoder()) - use waypoints <- decode.field( - "waypoints", - decode.list(scanned_waypoint.decoder()), - ) - decode.success(ScanWaypointsResponse(cooldown:, waypoints:)) -} - pub fn scan_waypoints( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(ScanWaypointsResponse) { +) -> ApiResponse(Scan(ScannedWaypoint)) { let request = api.post( AgentAuth(token), @@ -452,25 +365,19 @@ pub fn scan_waypoints( ) use response <- api.try_send(request) case response.status { - 201 -> api.parse_data_response(response, scan_waypoints_response_decoder()) + 201 -> + api.parse_data_response( + response, + scan_decoder("waypoints", scanned_waypoint.decoder()), + ) _ -> api.parse_error_response(response) } } -pub type ScanShipsResponse { - ScanShipsResponse(cooldown: Cooldown, ships: List(ScannedShip)) -} - -fn scan_ships_response_decoder() -> Decoder(ScanShipsResponse) { - use cooldown <- decode.field("cooldown", cooldown.decoder()) - use ships <- decode.field("ships", decode.list(scanned_ship.decoder())) - decode.success(ScanShipsResponse(cooldown:, ships:)) -} - pub fn scan_ships( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(ScanShipsResponse) { +) -> ApiResponse(Scan(ScannedShip)) { let request = api.post( AgentAuth(token), @@ -478,25 +385,23 @@ pub fn scan_ships( ) use response <- api.try_send(request) case response.status { - 201 -> api.parse_data_response(response, scan_ships_response_decoder()) + 201 -> + api.parse_data_response( + response, + scan_decoder("ships", scanned_ship.decoder()), + ) _ -> api.parse_error_response(response) } } -pub type ScrapShipResponse { - ScrapShipResponse(agent: Agent, transaction: ScrapTransaction) -} - -fn scrap_ship_response_decoder() -> Decoder(ScrapShipResponse) { - use agent <- decode.field("agent", agent.decoder()) - use transaction <- decode.field("transaction", scrap_transaction.decoder()) - decode.success(ScrapShipResponse(agent:, transaction:)) +pub type ShipScrapped { + ShipScrapped(agent: Agent, transaction: ScrapTransaction) } pub fn scrap_ship( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(ScrapShipResponse) { +) -> ApiResponse(ShipScrapped) { let request = api.post( AgentAuth(token), @@ -504,24 +409,23 @@ pub fn scrap_ship( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, scrap_ship_response_decoder()) + 200 -> + api.parse_data_response(response, { + use agent <- decode.field("agent", agent.decoder()) + use transaction <- decode.field( + "transaction", + scrap_transaction.decoder(), + ) + decode.success(ShipScrapped(agent:, transaction:)) + }) _ -> api.parse_error_response(response) } } -pub type GetScrapShipResponse { - GetScrapShipResponse(transaction: ScrapTransaction) -} - -fn get_scrap_ship_response_decoder() -> Decoder(GetScrapShipResponse) { - use transaction <- decode.field("transaction", scrap_transaction.decoder()) - decode.success(GetScrapShipResponse(transaction:)) -} - pub fn get_scrap_ship( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(GetScrapShipResponse) { +) -> ApiResponse(ScrapTransaction) { let request = api.get( AgentAuth(token), @@ -529,34 +433,24 @@ pub fn get_scrap_ship( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, get_scrap_ship_response_decoder()) + 200 -> + api.parse_data_response( + response, + decode.field("transaction", scrap_transaction.decoder(), decode.success), + ) _ -> api.parse_error_response(response) } } -pub type NavigateShipResponse { - NavigateShipResponse( - nav: ShipNav, - fuel: ShipFuel, - events: List(ShipConditionEvent), - ) -} - -fn navigate_ship_response_decoder() -> Decoder(NavigateShipResponse) { - use nav <- decode.field("nav", ship_nav.decoder()) - use fuel <- decode.field("fuel", ship_fuel.decoder()) - use events <- decode.field( - "events", - decode.list(ship_condition_event.decoder()), - ) - decode.success(NavigateShipResponse(nav:, fuel:, events:)) +pub type ShipNavigated { + ShipNavigated(nav: ShipNav, fuel: ShipFuel, events: List(ShipConditionEvent)) } pub fn navigate_ship( token: AgentToken, ship_symbol: ShipSymbol, waypoint_symbol: WaypointSymbol, -) -> ApiResponse(NavigateShipResponse) { +) -> ApiResponse(ShipNavigated) { let request = api.post_json( AgentAuth(token), @@ -565,34 +459,29 @@ pub fn navigate_ship( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, navigate_ship_response_decoder()) + 200 -> + api.parse_data_response(response, { + use nav <- decode.field("nav", ship_nav.decoder()) + use fuel <- decode.field("fuel", ship_fuel.decoder()) + use events <- decode.field( + "events", + decode.list(ship_condition_event.decoder()), + ) + decode.success(ShipNavigated(nav:, fuel:, events:)) + }) _ -> api.parse_error_response(response) } } -pub type WarpShipResponse { - WarpShipResponse( - nav: ShipNav, - fuel: ShipFuel, - events: List(ShipConditionEvent), - ) -} - -fn warp_ship_response_decoder() -> Decoder(WarpShipResponse) { - use nav <- decode.field("nav", ship_nav.decoder()) - use fuel <- decode.field("fuel", ship_fuel.decoder()) - use events <- decode.field( - "events", - decode.list(ship_condition_event.decoder()), - ) - decode.success(WarpShipResponse(nav:, fuel:, events:)) +pub type ShipWarped { + ShipWarped(nav: ShipNav, fuel: ShipFuel, events: List(ShipConditionEvent)) } pub fn warp_ship( token: AgentToken, ship_symbol: ShipSymbol, waypoint_symbol: WaypointSymbol, -) -> ApiResponse(WarpShipResponse) { +) -> ApiResponse(ShipWarped) { let request = api.post_json( AgentAuth(token), @@ -601,24 +490,24 @@ pub fn warp_ship( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, warp_ship_response_decoder()) + 200 -> + api.parse_data_response(response, { + use nav <- decode.field("nav", ship_nav.decoder()) + use fuel <- decode.field("fuel", ship_fuel.decoder()) + use events <- decode.field( + "events", + decode.list(ship_condition_event.decoder()), + ) + decode.success(ShipWarped(nav:, fuel:, events:)) + }) _ -> api.parse_error_response(response) } } -pub type OrbitShipResponse { - OrbitShipResponse(nav: ShipNav) -} - -fn orbit_ship_response_decoder() -> Decoder(OrbitShipResponse) { - use nav <- decode.field("nav", ship_nav.decoder()) - decode.success(OrbitShipResponse(nav:)) -} - pub fn orbit_ship( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(OrbitShipResponse) { +) -> ApiResponse(ShipNav) { let request = api.post( AgentAuth(token), @@ -626,31 +515,24 @@ pub fn orbit_ship( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, orbit_ship_response_decoder()) + 200 -> + api.parse_data_response( + response, + decode.field("nav", ship_nav.decoder(), decode.success), + ) _ -> api.parse_error_response(response) } } -pub type PurchaseCargoResponse { - PurchaseCargoResponse( - cargo: ShipCargo, - transaction: MarketTransaction, - agent: Agent, - ) -} - -fn purchase_cargo_response_decoder() -> Decoder(PurchaseCargoResponse) { - use cargo <- decode.field("cargo", ship_cargo.decoder()) - use transaction <- decode.field("transaction", market_transaction.decoder()) - use agent <- decode.field("agent", agent.decoder()) - decode.success(PurchaseCargoResponse(cargo:, transaction:, agent:)) +pub type CargoPurchased { + CargoPurchased(cargo: ShipCargo, transaction: MarketTransaction, agent: Agent) } pub fn purchase_cargo( token: AgentToken, ship_symbol: ShipSymbol, trade_symbol: TradeSymbol, -) -> ApiResponse(PurchaseCargoResponse) { +) -> ApiResponse(CargoPurchased) { let request = api.post_json( AgentAuth(token), @@ -659,13 +541,22 @@ pub fn purchase_cargo( ) use response <- api.try_send(request) case response.status { - 201 -> api.parse_data_response(response, purchase_cargo_response_decoder()) + 201 -> + api.parse_data_response(response, { + use cargo <- decode.field("cargo", ship_cargo.decoder()) + use transaction <- decode.field( + "transaction", + market_transaction.decoder(), + ) + use agent <- decode.field("agent", agent.decoder()) + decode.success(CargoPurchased(cargo:, transaction:, agent:)) + }) _ -> api.parse_error_response(response) } } -pub type RefineShipResponse { - RefineShipResponse( +pub type ShipRefined { + ShipRefined( cargo: ShipCargo, cooldown: Cooldown, produced: List(RefinementYield), @@ -673,25 +564,11 @@ pub type RefineShipResponse { ) } -fn refine_ship_response_decoder() -> Decoder(RefineShipResponse) { - use cargo <- decode.field("cargo", ship_cargo.decoder()) - use cooldown <- decode.field("cooldown", cooldown.decoder()) - use produced <- decode.field( - "produced", - decode.list(refinement_yield.decoder()), - ) - use consumed <- decode.field( - "consumed", - decode.list(refinement_yield.decoder()), - ) - decode.success(RefineShipResponse(cargo:, cooldown:, produced:, consumed:)) -} - pub fn refine_ship( token: AgentToken, ship_symbol: ShipSymbol, produce: RefinementProduce, -) -> ApiResponse(RefineShipResponse) { +) -> ApiResponse(ShipRefined) { let request = api.post_json( AgentAuth(token), @@ -700,13 +577,26 @@ pub fn refine_ship( ) use response <- api.try_send(request) case response.status { - 201 -> api.parse_data_response(response, refine_ship_response_decoder()) + 201 -> + api.parse_data_response(response, { + use cargo <- decode.field("cargo", ship_cargo.decoder()) + use cooldown <- decode.field("cooldown", cooldown.decoder()) + use produced <- decode.field( + "produced", + decode.list(refinement_yield.decoder()), + ) + use consumed <- decode.field( + "consumed", + decode.list(refinement_yield.decoder()), + ) + decode.success(ShipRefined(cargo:, cooldown:, produced:, consumed:)) + }) _ -> api.parse_error_response(response) } } -pub type RefuelShipResponse { - RefuelShipResponse( +pub type ShipRefueled { + ShipRefueled( agent: Agent, fuel: ShipFuel, cargo: ShipCargo, @@ -714,20 +604,12 @@ pub type RefuelShipResponse { ) } -pub fn refuel_ship_response_decoder() -> Decoder(RefuelShipResponse) { - use agent <- decode.field("agent", agent.decoder()) - use fuel <- decode.field("fuel", ship_fuel.decoder()) - use cargo <- decode.field("cargo", ship_cargo.decoder()) - use transaction <- decode.field("transaction", market_transaction.decoder()) - decode.success(RefuelShipResponse(agent:, fuel:, cargo:, transaction:)) -} - pub fn refuel_ship( token: AgentToken, ship_symbol: ShipSymbol, units: Option(Int), from_cargo: Option(Bool), -) -> ApiResponse(RefuelShipResponse) { +) -> ApiResponse(ShipRefueled) { let request = api.post_json( AgentAuth(token), @@ -739,26 +621,29 @@ pub fn refuel_ship( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, refuel_ship_response_decoder()) + 200 -> + api.parse_data_response(response, { + use agent <- decode.field("agent", agent.decoder()) + use fuel <- decode.field("fuel", ship_fuel.decoder()) + use cargo <- decode.field("cargo", ship_cargo.decoder()) + use transaction <- decode.field( + "transaction", + market_transaction.decoder(), + ) + decode.success(ShipRefueled(agent:, fuel:, cargo:, transaction:)) + }) _ -> api.parse_error_response(response) } } -pub type RepairShipResponse { - RepairShipResponse(agent: Agent, ship: Ship, transaction: RepairTransaction) -} - -pub fn repair_ship_response_decoder() -> Decoder(RepairShipResponse) { - use agent <- decode.field("agent", agent.decoder()) - use ship <- decode.field("ship", ship.decoder()) - use transaction <- decode.field("transaction", repair_transaction.decoder()) - decode.success(RepairShipResponse(agent:, ship:, transaction:)) +pub type ShipRepaired { + ShipRepaired(agent: Agent, ship: Ship, transaction: RepairTransaction) } pub fn repair_ship( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(RepairShipResponse) { +) -> ApiResponse(ShipRepaired) { let request = api.post( AgentAuth(token), @@ -766,24 +651,24 @@ pub fn repair_ship( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, repair_ship_response_decoder()) + 200 -> + api.parse_data_response(response, { + use agent <- decode.field("agent", agent.decoder()) + use ship <- decode.field("ship", ship.decoder()) + use transaction <- decode.field( + "transaction", + repair_transaction.decoder(), + ) + decode.success(ShipRepaired(agent:, ship:, transaction:)) + }) _ -> api.parse_error_response(response) } } -pub type GetRepairShipResponse { - GetRepairShipResponse(transaction: RepairTransaction) -} - -pub fn get_repair_ship_response_decoder() -> Decoder(GetRepairShipResponse) { - use transaction <- decode.field("transaction", repair_transaction.decoder()) - decode.success(GetRepairShipResponse(transaction:)) -} - pub fn get_repair_ship( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(GetRepairShipResponse) { +) -> ApiResponse(RepairTransaction) { let request = api.get( AgentAuth(token), @@ -791,24 +676,21 @@ pub fn get_repair_ship( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, get_repair_ship_response_decoder()) + 200 -> + api.parse_data_response( + response, + decode.field( + "transaction", + repair_transaction.decoder(), + decode.success, + ), + ) _ -> api.parse_error_response(response) } } -pub type SellCargoResponse { - SellCargoResponse( - cargo: ShipCargo, - transaction: MarketTransaction, - agent: Agent, - ) -} - -pub fn sell_cargo_response_decoder() -> Decoder(SellCargoResponse) { - use cargo <- decode.field("cargo", ship_cargo.decoder()) - use transaction <- decode.field("transaction", market_transaction.decoder()) - use agent <- decode.field("agent", agent.decoder()) - decode.success(SellCargoResponse(cargo:, transaction:, agent:)) +pub type CargoSold { + CargoSold(cargo: ShipCargo, transaction: MarketTransaction, agent: Agent) } pub fn sell_cargo( @@ -816,7 +698,7 @@ pub fn sell_cargo( ship_symbol: ShipSymbol, trade_symbol: TradeSymbol, units: Int, -) -> ApiResponse(SellCargoResponse) { +) -> ApiResponse(CargoSold) { let request = api.post_json( AgentAuth(token), @@ -828,13 +710,22 @@ pub fn sell_cargo( ) use response <- api.try_send(request) case response.status { - 201 -> api.parse_data_response(response, sell_cargo_response_decoder()) + 201 -> + api.parse_data_response(response, { + use cargo <- decode.field("cargo", ship_cargo.decoder()) + use transaction <- decode.field( + "transaction", + market_transaction.decoder(), + ) + use agent <- decode.field("agent", agent.decoder()) + decode.success(CargoSold(cargo:, transaction:, agent:)) + }) _ -> api.parse_error_response(response) } } -pub type SiphonResourcesResponse { - SiphonResourcesResponse( +pub type ResourcesSiphoned { + ResourcesSiphoned( siphon: Siphon, cooldown: Cooldown, cargo: ShipCargo, @@ -842,21 +733,10 @@ pub type SiphonResourcesResponse { ) } -pub fn siphon_resources_response_decoder() -> Decoder(SiphonResourcesResponse) { - use siphon <- decode.field("siphon", siphon.decoder()) - use cooldown <- decode.field("cooldown", cooldown.decoder()) - use cargo <- decode.field("cargo", ship_cargo.decoder()) - use events <- decode.field( - "events", - decode.list(ship_condition_event.decoder()), - ) - decode.success(SiphonResourcesResponse(siphon:, cooldown:, cargo:, events:)) -} - pub fn siphon_resources( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(SiphonResourcesResponse) { +) -> ApiResponse(ResourcesSiphoned) { let request = api.post( AgentAuth(token), @@ -865,25 +745,28 @@ pub fn siphon_resources( use response <- api.try_send(request) case response.status { 201 -> - api.parse_data_response(response, siphon_resources_response_decoder()) + api.parse_data_response(response, { + use siphon <- decode.field("siphon", siphon.decoder()) + use cooldown <- decode.field("cooldown", cooldown.decoder()) + use cargo <- decode.field("cargo", ship_cargo.decoder()) + use events <- decode.field( + "events", + decode.list(ship_condition_event.decoder()), + ) + decode.success(ResourcesSiphoned(siphon:, cooldown:, cargo:, events:)) + }) _ -> api.parse_error_response(response) } } -pub type CreateSurveyResponse { - CreateSurveyResponse(cooldown: Cooldown, surveys: List(Survey)) -} - -pub fn create_survey_response_decoder() -> Decoder(CreateSurveyResponse) { - use cooldown <- decode.field("cooldown", cooldown.decoder()) - use surveys <- decode.field("surveys", decode.list(survey.decoder())) - decode.success(CreateSurveyResponse(cooldown:, surveys:)) +pub type SurveyCreated { + SurveyCreated(cooldown: Cooldown, surveys: List(Survey)) } pub fn create_survey( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(CreateSurveyResponse) { +) -> ApiResponse(SurveyCreated) { let request = api.post( AgentAuth(token), @@ -891,19 +774,18 @@ pub fn create_survey( ) use response <- api.try_send(request) case response.status { - 201 -> api.parse_data_response(response, create_survey_response_decoder()) + 201 -> + api.parse_data_response(response, { + use cooldown <- decode.field("cooldown", cooldown.decoder()) + use surveys <- decode.field("surveys", decode.list(survey.decoder())) + decode.success(SurveyCreated(cooldown:, surveys:)) + }) _ -> api.parse_error_response(response) } } -pub type TransferCargoResponse { - TransferCargoResponse(cargo: ShipCargo, target_cargo: ShipCargo) -} - -pub fn transfer_cargo_response_decoder() -> Decoder(TransferCargoResponse) { - use cargo <- decode.field("cargo", ship_cargo.decoder()) - use target_cargo <- decode.field("targetCargo", ship_cargo.decoder()) - decode.success(TransferCargoResponse(cargo:, target_cargo:)) +pub type CargoTransferred { + CargoTransferred(cargo: ShipCargo, target_cargo: ShipCargo) } pub fn transfer_cargo( @@ -912,7 +794,7 @@ pub fn transfer_cargo( trade_symbol: TradeSymbol, units: Int, target_ship_symbol: ShipSymbol, -) -> ApiResponse(TransferCargoResponse) { +) -> ApiResponse(CargoTransferred) { let request = api.post_json( AgentAuth(token), @@ -925,24 +807,20 @@ pub fn transfer_cargo( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, transfer_cargo_response_decoder()) + 200 -> + api.parse_data_response(response, { + use cargo <- decode.field("cargo", ship_cargo.decoder()) + use target_cargo <- decode.field("targetCargo", ship_cargo.decoder()) + decode.success(CargoTransferred(cargo:, target_cargo:)) + }) _ -> api.parse_error_response(response) } } -pub type GetShipCargoResponse { - GetShipCargoResponse(cargo: ShipCargo) -} - -pub fn get_ship_cargo_response_decoder() -> Decoder(GetShipCargoResponse) { - use cargo <- decode.then(ship_cargo.decoder()) - decode.success(GetShipCargoResponse(cargo:)) -} - pub fn get_ship_cargo( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(GetShipCargoResponse) { +) -> ApiResponse(ShipCargo) { let request = api.get( AgentAuth(token), @@ -950,24 +828,15 @@ pub fn get_ship_cargo( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, get_ship_cargo_response_decoder()) + 200 -> api.parse_data_response(response, ship_cargo.decoder()) _ -> api.parse_error_response(response) } } -pub type GetShipModulesResponse { - GetShipModulesResponse(modules: List(ShipModule)) -} - -pub fn get_ship_modules_response_decoder() -> Decoder(GetShipModulesResponse) { - use modules <- decode.then(decode.list(ship_module.decoder())) - decode.success(GetShipModulesResponse(modules:)) -} - pub fn get_ship_modules( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(GetShipModulesResponse) { +) -> ApiResponse(List(ShipModule)) { let request = api.get( AgentAuth(token), @@ -975,14 +844,13 @@ pub fn get_ship_modules( ) use response <- api.try_send(request) case response.status { - 200 -> - api.parse_data_response(response, get_ship_modules_response_decoder()) + 200 -> api.parse_data_response(response, decode.list(ship_module.decoder())) _ -> api.parse_error_response(response) } } -pub type InstallShipModuleResponse { - InstallShipModuleResponse( +pub type ShipModuleInstalled { + ShipModuleInstalled( agent: Agent, modules: List(ShipModule), cargo: ShipCargo, @@ -990,29 +858,11 @@ pub type InstallShipModuleResponse { ) } -pub fn install_ship_module_response_decoder() -> Decoder( - InstallShipModuleResponse, -) { - use agent <- decode.field("agent", agent.decoder()) - use modules <- decode.field("modules", decode.list(ship_module.decoder())) - use cargo <- decode.field("cargo", ship_cargo.decoder()) - use transaction <- decode.field( - "transaction", - ship_modification_transaction.decoder(), - ) - decode.success(InstallShipModuleResponse( - agent:, - modules:, - cargo:, - transaction:, - )) -} - pub fn install_ship_module( token: AgentToken, ship_symbol: ShipSymbol, module_symbol: ModuleSymbol, -) -> ApiResponse(InstallShipModuleResponse) { +) -> ApiResponse(ShipModuleInstalled) { let request = api.post_json( AgentAuth(token), @@ -1022,13 +872,30 @@ pub fn install_ship_module( use response <- api.try_send(request) case response.status { 201 -> - api.parse_data_response(response, install_ship_module_response_decoder()) + api.parse_data_response(response, { + use agent <- decode.field("agent", agent.decoder()) + use modules <- decode.field( + "modules", + decode.list(ship_module.decoder()), + ) + use cargo <- decode.field("cargo", ship_cargo.decoder()) + use transaction <- decode.field( + "transaction", + ship_modification_transaction.decoder(), + ) + decode.success(ShipModuleInstalled( + agent:, + modules:, + cargo:, + transaction:, + )) + }) _ -> api.parse_error_response(response) } } -pub type RemoveShipModuleResponse { - RemoveShipModuleResponse( +pub type ShipModuleRemoved { + ShipModuleRemoved( agent: Agent, modules: List(ShipModule), cargo: ShipCargo, @@ -1036,29 +903,11 @@ pub type RemoveShipModuleResponse { ) } -pub fn remove_ship_module_response_decoder() -> Decoder( - RemoveShipModuleResponse, -) { - use agent <- decode.field("agent", agent.decoder()) - use modules <- decode.field("modules", decode.list(ship_module.decoder())) - use cargo <- decode.field("cargo", ship_cargo.decoder()) - use transaction <- decode.field( - "transaction", - ship_modification_transaction.decoder(), - ) - decode.success(RemoveShipModuleResponse( - agent:, - modules:, - cargo:, - transaction:, - )) -} - pub fn remove_ship_module( token: AgentToken, ship_symbol: ShipSymbol, module_symbol: ModuleSymbol, -) -> ApiResponse(RemoveShipModuleResponse) { +) -> ApiResponse(ShipModuleRemoved) { let request = api.post_json( AgentAuth(token), @@ -1068,24 +917,27 @@ pub fn remove_ship_module( use response <- api.try_send(request) case response.status { 201 -> - api.parse_data_response(response, remove_ship_module_response_decoder()) + api.parse_data_response(response, { + use agent <- decode.field("agent", agent.decoder()) + use modules <- decode.field( + "modules", + decode.list(ship_module.decoder()), + ) + use cargo <- decode.field("cargo", ship_cargo.decoder()) + use transaction <- decode.field( + "transaction", + ship_modification_transaction.decoder(), + ) + decode.success(ShipModuleRemoved(agent:, modules:, cargo:, transaction:)) + }) _ -> api.parse_error_response(response) } } -pub type GetShipMountsResponse { - GetShipMountsResponse(mounts: List(ShipMount)) -} - -pub fn get_ship_mounts_response_decoder() -> Decoder(GetShipMountsResponse) { - use mounts <- decode.then(decode.list(ship_mount.decoder())) - decode.success(GetShipMountsResponse(mounts:)) -} - pub fn get_ship_mounts( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(GetShipMountsResponse) { +) -> ApiResponse(List(ShipMount)) { let request = api.get( AgentAuth(token), @@ -1093,13 +945,13 @@ pub fn get_ship_mounts( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, get_ship_mounts_response_decoder()) + 200 -> api.parse_data_response(response, decode.list(ship_mount.decoder())) _ -> api.parse_error_response(response) } } -pub type InstallShipMountResponse { - InstallShipMountResponse( +pub type ShipMountInstalled { + ShipMountInstalled( agent: Agent, mounts: List(ShipMount), cargo: ShipCargo, @@ -1107,24 +959,11 @@ pub type InstallShipMountResponse { ) } -pub fn install_ship_mount_response_decoder() -> Decoder( - InstallShipMountResponse, -) { - use agent <- decode.field("agent", agent.decoder()) - use mounts <- decode.field("mounts", decode.list(ship_mount.decoder())) - use cargo <- decode.field("cargo", ship_cargo.decoder()) - use transaction <- decode.field( - "transaction", - ship_modification_transaction.decoder(), - ) - decode.success(InstallShipMountResponse(agent:, mounts:, cargo:, transaction:)) -} - pub fn install_ship_mount( token: AgentToken, ship_symbol: ShipSymbol, mount_symbol: MountSymbol, -) -> ApiResponse(InstallShipMountResponse) { +) -> ApiResponse(ShipMountInstalled) { let request = api.post_json( AgentAuth(token), @@ -1134,13 +973,22 @@ pub fn install_ship_mount( use response <- api.try_send(request) case response.status { 201 -> - api.parse_data_response(response, install_ship_mount_response_decoder()) + api.parse_data_response(response, { + use agent <- decode.field("agent", agent.decoder()) + use mounts <- decode.field("mounts", decode.list(ship_mount.decoder())) + use cargo <- decode.field("cargo", ship_cargo.decoder()) + use transaction <- decode.field( + "transaction", + ship_modification_transaction.decoder(), + ) + decode.success(ShipMountInstalled(agent:, mounts:, cargo:, transaction:)) + }) _ -> api.parse_error_response(response) } } -pub type RemoveShipMountResponse { - RemoveShipMountResponse( +pub type ShipMountRemoved { + ShipMountRemoved( agent: Agent, mounts: List(ShipMount), cargo: ShipCargo, @@ -1148,22 +996,11 @@ pub type RemoveShipMountResponse { ) } -pub fn remove_ship_mount_response_decoder() -> Decoder(RemoveShipMountResponse) { - use agent <- decode.field("agent", agent.decoder()) - use mounts <- decode.field("mounts", decode.list(ship_mount.decoder())) - use cargo <- decode.field("cargo", ship_cargo.decoder()) - use transaction <- decode.field( - "transaction", - ship_modification_transaction.decoder(), - ) - decode.success(RemoveShipMountResponse(agent:, mounts:, cargo:, transaction:)) -} - pub fn remove_ship_mount( token: AgentToken, ship_symbol: ShipSymbol, mount_symbol: MountSymbol, -) -> ApiResponse(RemoveShipMountResponse) { +) -> ApiResponse(ShipMountRemoved) { let request = api.post_json( AgentAuth(token), @@ -1173,24 +1010,24 @@ pub fn remove_ship_mount( use response <- api.try_send(request) case response.status { 201 -> - api.parse_data_response(response, remove_ship_mount_response_decoder()) + api.parse_data_response(response, { + use agent <- decode.field("agent", agent.decoder()) + use mounts <- decode.field("mounts", decode.list(ship_mount.decoder())) + use cargo <- decode.field("cargo", ship_cargo.decoder()) + use transaction <- decode.field( + "transaction", + ship_modification_transaction.decoder(), + ) + decode.success(ShipMountRemoved(agent:, mounts:, cargo:, transaction:)) + }) _ -> api.parse_error_response(response) } } -pub type GetShipNavResponse { - GetShipNavResponse(nav: ShipNav) -} - -pub fn get_ship_nav_response_decoder() -> Decoder(GetShipNavResponse) { - use nav <- decode.then(ship_nav.decoder()) - decode.success(GetShipNavResponse(nav:)) -} - pub fn get_ship_nav( token: AgentToken, ship_symbol: ShipSymbol, -) -> ApiResponse(GetShipNavResponse) { +) -> ApiResponse(ShipNav) { let request = api.get( AgentAuth(token), @@ -1198,34 +1035,20 @@ pub fn get_ship_nav( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, get_ship_nav_response_decoder()) + 200 -> api.parse_data_response(response, ship_nav.decoder()) _ -> api.parse_error_response(response) } } -pub type PatchShipNavResponse { - PatchShipNavResponse( - nav: ShipNav, - fuel: ShipFuel, - events: List(ShipConditionEvent), - ) -} - -pub fn patch_ship_nav_response_decoder() -> Decoder(PatchShipNavResponse) { - use nav <- decode.field("nav", ship_nav.decoder()) - use fuel <- decode.field("fuel", ship_fuel.decoder()) - use events <- decode.field( - "events", - decode.list(ship_condition_event.decoder()), - ) - decode.success(PatchShipNavResponse(nav:, fuel:, events:)) +pub type ShipNavPatched { + ShipNavPatched(nav: ShipNav, fuel: ShipFuel, events: List(ShipConditionEvent)) } pub fn patch_ship_nav( token: AgentToken, ship_symbol: ShipSymbol, flight_mode: ShipNavFlightMode, -) -> ApiResponse(PatchShipNavResponse) { +) -> ApiResponse(ShipNavPatched) { let request = api.patch_json( AgentAuth(token), @@ -1234,7 +1057,16 @@ pub fn patch_ship_nav( ) use response <- api.try_send(request) case response.status { - 200 -> api.parse_data_response(response, patch_ship_nav_response_decoder()) + 200 -> + api.parse_data_response(response, { + use nav <- decode.field("nav", ship_nav.decoder()) + use fuel <- decode.field("fuel", ship_fuel.decoder()) + use events <- decode.field( + "events", + decode.list(ship_condition_event.decoder()), + ) + decode.success(ShipNavPatched(nav:, fuel:, events:)) + }) _ -> api.parse_error_response(response) } } diff --git a/src/endpoints/global.gleam b/src/endpoints/global.gleam index 840dca2..432fc0d 100644 --- a/src/endpoints/global.gleam +++ b/src/endpoints/global.gleam @@ -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) } } diff --git a/src/endpoints/systems.gleam b/src/endpoints/systems.gleam index 084d789..9999b65 100644 --- a/src/endpoints/systems.gleam +++ b/src/endpoints/systems.gleam @@ -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) } } diff --git a/src/utils/api.gleam b/src/utils/api.gleam index 69eba01..8dd955b 100644 --- a/src/utils/api.gleam +++ b/src/utils/api.gleam @@ -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), diff --git a/src/utils/jwt.gleam b/src/utils/jwt.gleam index 20817a3..e4bc584 100644 --- a/src/utils/jwt.gleam +++ b/src/utils/jwt.gleam @@ -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)