Simplify response types
This commit is contained in:
parent
2c607f64bc
commit
17631a1a0c
10 changed files with 508 additions and 863 deletions
|
@ -1,4 +1,4 @@
|
||||||
import gleam/dynamic/decode.{type Decoder}
|
import gleam/dynamic/decode
|
||||||
import gleam/json
|
import gleam/json
|
||||||
import models/account.{type Account}
|
import models/account.{type Account}
|
||||||
import models/agent.{type Agent}
|
import models/agent.{type Agent}
|
||||||
|
@ -10,26 +10,21 @@ import models/ship.{type Ship}
|
||||||
import utils/api.{type ApiResponse}
|
import utils/api.{type ApiResponse}
|
||||||
import utils/auth.{type AccountToken, type AgentToken, AccountAuth, AgentAuth}
|
import utils/auth.{type AccountToken, type AgentToken, AccountAuth, AgentAuth}
|
||||||
|
|
||||||
pub type GetAccountResponse {
|
pub fn get_account(token: AgentToken) -> ApiResponse(Account) {
|
||||||
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) {
|
|
||||||
let request = api.get(AgentAuth(token), "/my/account")
|
let request = api.get(AgentAuth(token), "/my/account")
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
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)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type RegisterNewAgentResponse {
|
pub type AgentRegistered {
|
||||||
RegisterNewAgentResponse(
|
AgentRegistered(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
agent: Agent,
|
agent: Agent,
|
||||||
faction: Faction,
|
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(
|
pub fn register_new_agent(
|
||||||
token: AccountToken,
|
token: AccountToken,
|
||||||
agent_symbol: AgentSymbol,
|
agent_symbol: AgentSymbol,
|
||||||
faction_symbol: FactionSymbol,
|
faction_symbol: FactionSymbol,
|
||||||
) -> ApiResponse(RegisterNewAgentResponse) {
|
) -> ApiResponse(AgentRegistered) {
|
||||||
let request =
|
let request =
|
||||||
api.post_json(
|
api.post_json(
|
||||||
AccountAuth(token),
|
AccountAuth(token),
|
||||||
|
@ -70,7 +50,20 @@ pub fn register_new_agent(
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
case response.status {
|
||||||
201 ->
|
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)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import gleam/dynamic/decode.{type Decoder}
|
import gleam/dynamic/decode
|
||||||
import gleam/option.{type Option}
|
import gleam/option.{type Option}
|
||||||
import models/agent.{type Agent}
|
import models/agent.{type Agent}
|
||||||
import models/agent_event.{type AgentEvent}
|
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/api.{type ApiResponse, type PagedData}
|
||||||
import utils/auth.{type AgentToken, AgentAuth}
|
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(
|
pub fn list_public_agents(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
page: Option(Int),
|
page: Option(Int),
|
||||||
limit: Option(Int),
|
limit: Option(Int),
|
||||||
) -> ApiResponse(PagedData(ListPublicAgentsResponse)) {
|
) -> ApiResponse(PagedData(List(PublicAgent))) {
|
||||||
let request = api.get_page(AgentAuth(token), "/agents", page, limit)
|
let request = api.get_page(AgentAuth(token), "/agents", page, limit)
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
case response.status {
|
||||||
200 ->
|
200 ->
|
||||||
api.parse_paged_data_response(
|
api.parse_paged_data_response(
|
||||||
response,
|
response,
|
||||||
list_public_agents_response_decoder(),
|
decode.list(public_agent.decoder()),
|
||||||
)
|
)
|
||||||
_ -> api.parse_error_response(response)
|
_ -> 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(
|
pub fn get_public_agent(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
agent_symbol: AgentSymbol,
|
agent_symbol: AgentSymbol,
|
||||||
) -> ApiResponse(GetPublicAgentResponse) {
|
) -> ApiResponse(PublicAgent) {
|
||||||
let request =
|
let request =
|
||||||
api.get(
|
api.get(
|
||||||
AgentAuth(token),
|
AgentAuth(token),
|
||||||
|
@ -53,47 +35,25 @@ pub fn get_public_agent(
|
||||||
)
|
)
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
case response.status {
|
||||||
200 ->
|
200 -> api.parse_data_response(response, public_agent.decoder())
|
||||||
api.parse_data_response(response, get_public_agent_response_decoder())
|
|
||||||
_ -> api.parse_error_response(response)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type GetAgentResponse {
|
pub fn get_agent(token: AgentToken) -> ApiResponse(Agent) {
|
||||||
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) {
|
|
||||||
let request = api.get(AgentAuth(token), "/my/agent")
|
let request = api.get(AgentAuth(token), "/my/agent")
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
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)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type GetAgentEventsResponse {
|
pub fn get_agent_events(token: AgentToken) -> ApiResponse(List(AgentEvent)) {
|
||||||
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) {
|
|
||||||
let request = api.get(AgentAuth(token), "/my/agent/events")
|
let request = api.get(AgentAuth(token), "/my/agent/events")
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
case response.status {
|
||||||
200 ->
|
200 -> api.parse_data_response(response, decode.list(agent_event.decoder()))
|
||||||
api.parse_data_response(response, get_agent_events_response_decoder())
|
|
||||||
_ -> api.parse_error_response(response)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import endpoints/fleet
|
import endpoints/fleet
|
||||||
import gleam/dynamic/decode.{type Decoder}
|
import gleam/dynamic/decode
|
||||||
import gleam/json
|
import gleam/json
|
||||||
import gleam/option.{type Option}
|
import gleam/option.{type Option}
|
||||||
import models/agent.{type Agent}
|
import models/agent.{type Agent}
|
||||||
|
@ -11,42 +11,24 @@ import models/trade_symbol.{type TradeSymbol}
|
||||||
import utils/api.{type ApiResponse, type PagedData}
|
import utils/api.{type ApiResponse, type PagedData}
|
||||||
import utils/auth.{type AgentToken, AgentAuth}
|
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(
|
pub fn list_contracts(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
page: Option(Int),
|
page: Option(Int),
|
||||||
limit: Option(Int),
|
limit: Option(Int),
|
||||||
) -> ApiResponse(PagedData(ListContractsResponse)) {
|
) -> ApiResponse(PagedData(List(Contract))) {
|
||||||
let request = api.get_page(AgentAuth(token), "/my/contracts", page, limit)
|
let request = api.get_page(AgentAuth(token), "/my/contracts", page, limit)
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
case response.status {
|
||||||
200 ->
|
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)
|
_ -> 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(
|
pub fn get_contract(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
contract_id: ContractId,
|
contract_id: ContractId,
|
||||||
) -> ApiResponse(GetContractResponse) {
|
) -> ApiResponse(Contract) {
|
||||||
let request =
|
let request =
|
||||||
api.get(
|
api.get(
|
||||||
AgentAuth(token),
|
AgentAuth(token),
|
||||||
|
@ -54,25 +36,19 @@ pub fn get_contract(
|
||||||
)
|
)
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
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)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type AcceptContractResponse {
|
pub type ContractAccepted {
|
||||||
AcceptContractResponse(contract: Contract, agent: Agent)
|
ContractAccepted(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 fn accept_contract(
|
pub fn accept_contract(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
contract_id: ContractId,
|
contract_id: ContractId,
|
||||||
) -> ApiResponse(AcceptContractResponse) {
|
) -> ApiResponse(ContractAccepted) {
|
||||||
let request =
|
let request =
|
||||||
api.post(
|
api.post(
|
||||||
AgentAuth(token),
|
AgentAuth(token),
|
||||||
|
@ -80,25 +56,24 @@ pub fn accept_contract(
|
||||||
)
|
)
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
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)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type FulfillContractResponse {
|
pub type ContractFulfilled {
|
||||||
FulfillContractResponse(contract: Contract, agent: Agent)
|
ContractFulfilled(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 fn fulfill_contract(
|
pub fn fulfill_contract(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
contract_id: ContractId,
|
contract_id: ContractId,
|
||||||
) -> ApiResponse(FulfillContractResponse) {
|
) -> ApiResponse(ContractFulfilled) {
|
||||||
let request =
|
let request =
|
||||||
api.post(
|
api.post(
|
||||||
AgentAuth(token),
|
AgentAuth(token),
|
||||||
|
@ -107,21 +82,17 @@ pub fn fulfill_contract(
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
case response.status {
|
||||||
200 ->
|
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)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type DeliverContractCargoResponse {
|
pub type ContractCargoDelivered {
|
||||||
DeliverContractCargoResponse(contract: Contract, cargo: ShipCargo)
|
ContractCargoDelivered(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 fn deliver_contract_cargo(
|
pub fn deliver_contract_cargo(
|
||||||
|
@ -130,7 +101,7 @@ pub fn deliver_contract_cargo(
|
||||||
ship_symbol: ShipSymbol,
|
ship_symbol: ShipSymbol,
|
||||||
trade_symbol: TradeSymbol,
|
trade_symbol: TradeSymbol,
|
||||||
units: Int,
|
units: Int,
|
||||||
) -> ApiResponse(DeliverContractCargoResponse) {
|
) -> ApiResponse(ContractCargoDelivered) {
|
||||||
let request =
|
let request =
|
||||||
api.post_json(
|
api.post_json(
|
||||||
AgentAuth(token),
|
AgentAuth(token),
|
||||||
|
@ -144,10 +115,11 @@ pub fn deliver_contract_cargo(
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
case response.status {
|
||||||
200 ->
|
200 ->
|
||||||
api.parse_data_response(
|
api.parse_data_response(response, {
|
||||||
response,
|
use contract <- decode.field("contract", contract.decoder())
|
||||||
deliver_contract_cargo_response_decoder(),
|
use cargo <- decode.field("cargo", ship_cargo.decoder())
|
||||||
)
|
decode.success(ContractCargoDelivered(contract:, cargo:))
|
||||||
|
})
|
||||||
_ -> api.parse_error_response(response)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,30 +1,28 @@
|
||||||
import gleam/dict.{type Dict}
|
import gleam/dict.{type Dict}
|
||||||
import gleam/dynamic/decode.{type Decoder}
|
import gleam/dynamic/decode
|
||||||
import models/trade_symbol.{type TradeSymbol}
|
import models/trade_symbol.{type TradeSymbol}
|
||||||
import utils/api.{type ApiResponse}
|
import utils/api.{type ApiResponse}
|
||||||
import utils/auth.{NoAuth}
|
import utils/auth.{NoAuth}
|
||||||
|
|
||||||
pub type GetSupplyChainResponse {
|
pub type ExportToImportMap =
|
||||||
GetSupplyChainResponse(
|
Dict(TradeSymbol, List(TradeSymbol))
|
||||||
export_to_import_map: Dict(TradeSymbol, List(TradeSymbol)),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_supply_chain_response_decoder() -> Decoder(GetSupplyChainResponse) {
|
pub fn get_supply_chain() -> ApiResponse(ExportToImportMap) {
|
||||||
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) {
|
|
||||||
let request = api.get(NoAuth, "/market/supply-chain")
|
let request = api.get(NoAuth, "/market/supply-chain")
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
echo response.body
|
|
||||||
case response.status {
|
case response.status {
|
||||||
200 ->
|
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)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,51 +1,33 @@
|
||||||
import gleam/dynamic/decode.{type Decoder}
|
import gleam/dynamic/decode
|
||||||
import gleam/option.{type Option}
|
import gleam/option.{type Option}
|
||||||
import models/faction.{type Faction}
|
import models/faction.{type Faction}
|
||||||
import models/faction_symbol.{type FactionSymbol}
|
import models/faction_symbol.{type FactionSymbol}
|
||||||
import utils/api.{type ApiResponse, type PagedData}
|
import utils/api.{type ApiResponse, type PagedData}
|
||||||
import utils/auth.{type AgentToken, AgentAuth}
|
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(
|
pub fn list_factions(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
page: Option(Int),
|
page: Option(Int),
|
||||||
limit: Option(Int),
|
limit: Option(Int),
|
||||||
) -> ApiResponse(PagedData(ListFactionsResponse)) {
|
) -> ApiResponse(PagedData(List(Faction))) {
|
||||||
let request = api.get_page(AgentAuth(token), "/factions", page, limit)
|
let request = api.get_page(AgentAuth(token), "/factions", page, limit)
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
case response.status {
|
||||||
200 ->
|
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)
|
_ -> 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(
|
pub fn get_faction(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
symbol: FactionSymbol,
|
symbol: FactionSymbol,
|
||||||
) -> ApiResponse(GetFactionResponse) {
|
) -> ApiResponse(Faction) {
|
||||||
let request =
|
let request =
|
||||||
api.get(AgentAuth(token), "/factions/" <> faction_symbol.to_string(symbol))
|
api.get(AgentAuth(token), "/factions/" <> faction_symbol.to_string(symbol))
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
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)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,29 +42,18 @@ fn faction_reputation_decoder() {
|
||||||
decode.success(FactionReputation(symbol:, reputation:))
|
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(
|
pub fn get_my_factions(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
page: Option(Int),
|
page: Option(Int),
|
||||||
limit: Option(Int),
|
limit: Option(Int),
|
||||||
) -> ApiResponse(PagedData(GetMyFactionsResponse)) {
|
) -> ApiResponse(PagedData(List(FactionReputation))) {
|
||||||
let request = api.get_page(AgentAuth(token), "/my/factions", page, limit)
|
let request = api.get_page(AgentAuth(token), "/my/factions", page, limit)
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
case response.status {
|
||||||
200 ->
|
200 ->
|
||||||
api.parse_paged_data_response(
|
api.parse_paged_data_response(
|
||||||
response,
|
response,
|
||||||
get_my_factions_response_decoder(),
|
decode.list(faction_reputation_decoder()),
|
||||||
)
|
)
|
||||||
_ -> api.parse_error_response(response)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,5 +1,8 @@
|
||||||
|
import birl.{type Time}
|
||||||
import gleam/dynamic/decode.{type Decoder}
|
import gleam/dynamic/decode.{type Decoder}
|
||||||
import gleam/option.{type Option}
|
import gleam/option.{type Option}
|
||||||
|
import gleam/uri.{type Uri}
|
||||||
|
import models/agent_symbol.{type AgentSymbol}
|
||||||
import utils/api.{type ApiResponse}
|
import utils/api.{type ApiResponse}
|
||||||
import utils/auth.{NoAuth}
|
import utils/auth.{NoAuth}
|
||||||
|
|
||||||
|
@ -27,34 +30,34 @@ fn stats_decoder() -> Decoder(Stats) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Health {
|
pub type Health {
|
||||||
Health(last_market_update: Option(String))
|
Health(last_market_update: Option(Time))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn health_decoder() -> Decoder(Health) {
|
fn health_decoder() -> Decoder(Health) {
|
||||||
use last_market_update <- decode.optional_field(
|
use last_market_update <- decode.optional_field(
|
||||||
"lastMarketUpdate",
|
"lastMarketUpdate",
|
||||||
option.None,
|
option.None,
|
||||||
decode.optional(decode.string),
|
decode.optional(api.time_decoder()),
|
||||||
)
|
)
|
||||||
decode.success(Health(last_market_update:))
|
decode.success(Health(last_market_update:))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type CreditLeaderboardEntry {
|
pub type CreditLeaderboardEntry {
|
||||||
CreditLeaderboardEntry(agent_symbol: String, credits: Int)
|
CreditLeaderboardEntry(agent_symbol: AgentSymbol, credits: Int)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn credit_leaderboard_entry_decoder() -> Decoder(CreditLeaderboardEntry) {
|
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)
|
use credits <- decode.field("credits", decode.int)
|
||||||
decode.success(CreditLeaderboardEntry(agent_symbol:, credits:))
|
decode.success(CreditLeaderboardEntry(agent_symbol:, credits:))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type ChartLeaderboardEntry {
|
pub type ChartLeaderboardEntry {
|
||||||
ChartLeaderboardEntry(agent_symbol: String, chart_count: Int)
|
ChartLeaderboardEntry(agent_symbol: AgentSymbol, chart_count: Int)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn chart_leaderboard_entry_decoder() -> Decoder(ChartLeaderboardEntry) {
|
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)
|
use chart_count <- decode.field("chartCount", decode.int)
|
||||||
decode.success(ChartLeaderboardEntry(agent_symbol:, chart_count:))
|
decode.success(ChartLeaderboardEntry(agent_symbol:, chart_count:))
|
||||||
}
|
}
|
||||||
|
@ -79,11 +82,11 @@ fn leaderboards_decoder() -> Decoder(Leaderboards) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type ServerResets {
|
pub type ServerResets {
|
||||||
ServerResets(next: String, frequency: String)
|
ServerResets(next: Time, frequency: String)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn server_resets_decoder() -> Decoder(ServerResets) {
|
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)
|
use frequency <- decode.field("frequency", decode.string)
|
||||||
decode.success(ServerResets(next:, frequency:))
|
decode.success(ServerResets(next:, frequency:))
|
||||||
}
|
}
|
||||||
|
@ -99,20 +102,26 @@ fn announcement_decoder() -> Decoder(Announcement) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Link {
|
pub type Link {
|
||||||
Link(name: String, url: String)
|
Link(name: String, url: Uri)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn link_decoder() -> Decoder(Link) {
|
fn link_decoder() -> Decoder(Link) {
|
||||||
use name <- decode.field("name", decode.string)
|
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:))
|
decode.success(Link(name:, url:))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type GetServerStatusResponse {
|
pub type ServerStatus {
|
||||||
GetServerStatusResponse(
|
ServerStatus(
|
||||||
status: String,
|
status: String,
|
||||||
version: String,
|
version: String,
|
||||||
reset_date: String,
|
reset_date: Time,
|
||||||
description: String,
|
description: String,
|
||||||
stats: Stats,
|
stats: Stats,
|
||||||
health: Health,
|
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 status <- decode.field("status", decode.string)
|
||||||
use version <- decode.field("version", 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 description <- decode.field("description", decode.string)
|
||||||
use stats <- decode.field("stats", stats_decoder())
|
use stats <- decode.field("stats", stats_decoder())
|
||||||
use health <- decode.field("health", health_decoder())
|
use health <- decode.field("health", health_decoder())
|
||||||
|
@ -137,7 +152,7 @@ fn get_server_status_response_decoder() -> Decoder(GetServerStatusResponse) {
|
||||||
decode.list(announcement_decoder()),
|
decode.list(announcement_decoder()),
|
||||||
)
|
)
|
||||||
use links <- decode.field("links", decode.list(link_decoder()))
|
use links <- decode.field("links", decode.list(link_decoder()))
|
||||||
decode.success(GetServerStatusResponse(
|
decode.success(ServerStatus(
|
||||||
status:,
|
status:,
|
||||||
version:,
|
version:,
|
||||||
reset_date:,
|
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, "/")
|
let request = api.get(NoAuth, "/")
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
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)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -170,23 +185,19 @@ fn error_code_decoder() -> Decoder(ErrorCode) {
|
||||||
decode.success(ErrorCode(code:, name:))
|
decode.success(ErrorCode(code:, name:))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type ListErrorCodesResponse {
|
pub fn list_error_codes() -> ApiResponse(List(ErrorCode)) {
|
||||||
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) {
|
|
||||||
let request = api.get(NoAuth, "/error-codes")
|
let request = api.get(NoAuth, "/error-codes")
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
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)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import gleam/dynamic/decode.{type Decoder}
|
import gleam/dynamic/decode
|
||||||
import gleam/json
|
import gleam/json
|
||||||
import gleam/list
|
import gleam/list
|
||||||
import gleam/option.{type Option}
|
import gleam/option.{type Option}
|
||||||
|
@ -18,42 +18,24 @@ import models/waypoint_type.{type WaypointType}
|
||||||
import utils/api.{type ApiResponse, type PagedData}
|
import utils/api.{type ApiResponse, type PagedData}
|
||||||
import utils/auth.{type AgentToken, AgentAuth}
|
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(
|
pub fn list_systems(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
page: Option(Int),
|
page: Option(Int),
|
||||||
limit: Option(Int),
|
limit: Option(Int),
|
||||||
) -> ApiResponse(PagedData(ListSystemsResponse)) {
|
) -> ApiResponse(PagedData(List(System))) {
|
||||||
let request = api.get_page(AgentAuth(token), "/systems", page, limit)
|
let request = api.get_page(AgentAuth(token), "/systems", page, limit)
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
case response.status {
|
||||||
200 ->
|
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)
|
_ -> 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(
|
pub fn get_system(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
system_symbol: SystemSymbol,
|
system_symbol: SystemSymbol,
|
||||||
) -> ApiResponse(GetSystemResponse) {
|
) -> ApiResponse(System) {
|
||||||
let request =
|
let request =
|
||||||
api.get(
|
api.get(
|
||||||
AgentAuth(token),
|
AgentAuth(token),
|
||||||
|
@ -61,22 +43,11 @@ pub fn get_system(
|
||||||
)
|
)
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
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)
|
_ -> 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(
|
pub fn list_system_waypoints(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
system_symbol: SystemSymbol,
|
system_symbol: SystemSymbol,
|
||||||
|
@ -84,7 +55,7 @@ pub fn list_system_waypoints(
|
||||||
limit: Option(Int),
|
limit: Option(Int),
|
||||||
type_: Option(WaypointType),
|
type_: Option(WaypointType),
|
||||||
traits: List(WaypointTraitSymbol),
|
traits: List(WaypointTraitSymbol),
|
||||||
) -> ApiResponse(PagedData(ListSystemWaypointsResponse)) {
|
) -> ApiResponse(PagedData(List(Waypoint))) {
|
||||||
let query =
|
let query =
|
||||||
list.map(traits, fn(trait) {
|
list.map(traits, fn(trait) {
|
||||||
#("traits", waypoint_trait_symbol.to_string(trait))
|
#("traits", waypoint_trait_symbol.to_string(trait))
|
||||||
|
@ -94,7 +65,7 @@ pub fn list_system_waypoints(
|
||||||
option.None -> query
|
option.None -> query
|
||||||
}
|
}
|
||||||
let request =
|
let request =
|
||||||
api.get_page_query(
|
api.get_page_with_query(
|
||||||
AgentAuth(token),
|
AgentAuth(token),
|
||||||
"/systems/" <> system_symbol.to_string(system_symbol) <> "/waypoints",
|
"/systems/" <> system_symbol.to_string(system_symbol) <> "/waypoints",
|
||||||
page,
|
page,
|
||||||
|
@ -104,28 +75,16 @@ pub fn list_system_waypoints(
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
case response.status {
|
||||||
200 ->
|
200 ->
|
||||||
api.parse_paged_data_response(
|
api.parse_paged_data_response(response, decode.list(waypoint.decoder()))
|
||||||
response,
|
|
||||||
list_system_waypoints_response_decoder(),
|
|
||||||
)
|
|
||||||
_ -> api.parse_error_response(response)
|
_ -> 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(
|
pub fn get_waypoint(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
system_symbol: SystemSymbol,
|
system_symbol: SystemSymbol,
|
||||||
waypoint_symbol: WaypointSymbol,
|
waypoint_symbol: WaypointSymbol,
|
||||||
) -> ApiResponse(GetWaypointResponse) {
|
) -> ApiResponse(Waypoint) {
|
||||||
let request =
|
let request =
|
||||||
api.get(
|
api.get(
|
||||||
AgentAuth(token),
|
AgentAuth(token),
|
||||||
|
@ -136,27 +95,16 @@ pub fn get_waypoint(
|
||||||
)
|
)
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
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)
|
_ -> 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(
|
pub fn get_construction_site(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
system_symbol: SystemSymbol,
|
system_symbol: SystemSymbol,
|
||||||
waypoint_symbol: WaypointSymbol,
|
waypoint_symbol: WaypointSymbol,
|
||||||
) -> ApiResponse(GetConstructionSiteResponse) {
|
) -> ApiResponse(Construction) {
|
||||||
let request =
|
let request =
|
||||||
api.get(
|
api.get(
|
||||||
AgentAuth(token),
|
AgentAuth(token),
|
||||||
|
@ -168,25 +116,13 @@ pub fn get_construction_site(
|
||||||
)
|
)
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
case response.status {
|
||||||
200 ->
|
200 -> api.parse_data_response(response, construction.decoder())
|
||||||
api.parse_data_response(
|
|
||||||
response,
|
|
||||||
get_construction_site_response_decoder(),
|
|
||||||
)
|
|
||||||
_ -> api.parse_error_response(response)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type SupplyConstructionSiteResponse {
|
pub type ConstructionSiteSupplied {
|
||||||
SupplyConstructionSiteResponse(construction: Construction, cargo: ShipCargo)
|
ConstructionSiteSupplied(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 fn supply_construction_site(
|
pub fn supply_construction_site(
|
||||||
|
@ -196,7 +132,7 @@ pub fn supply_construction_site(
|
||||||
ship_symbol: ShipSymbol,
|
ship_symbol: ShipSymbol,
|
||||||
trade_symbol: TradeSymbol,
|
trade_symbol: TradeSymbol,
|
||||||
units: Int,
|
units: Int,
|
||||||
) -> ApiResponse(SupplyConstructionSiteResponse) {
|
) -> ApiResponse(ConstructionSiteSupplied) {
|
||||||
let request =
|
let request =
|
||||||
api.post_json(
|
api.post_json(
|
||||||
AgentAuth(token),
|
AgentAuth(token),
|
||||||
|
@ -214,28 +150,20 @@ pub fn supply_construction_site(
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
case response.status {
|
||||||
201 ->
|
201 ->
|
||||||
api.parse_data_response(
|
api.parse_data_response(response, {
|
||||||
response,
|
use construction <- decode.field("construction", construction.decoder())
|
||||||
supply_construction_site_response_decoder(),
|
use cargo <- decode.field("cargo", ship_cargo.decoder())
|
||||||
)
|
decode.success(ConstructionSiteSupplied(construction:, cargo:))
|
||||||
|
})
|
||||||
_ -> api.parse_error_response(response)
|
_ -> 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(
|
pub fn get_market(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
system_symbol: SystemSymbol,
|
system_symbol: SystemSymbol,
|
||||||
waypoint_symbol: WaypointSymbol,
|
waypoint_symbol: WaypointSymbol,
|
||||||
) -> ApiResponse(GetMarketResponse) {
|
) -> ApiResponse(Market) {
|
||||||
let request =
|
let request =
|
||||||
api.get(
|
api.get(
|
||||||
AgentAuth(token),
|
AgentAuth(token),
|
||||||
|
@ -247,25 +175,16 @@ pub fn get_market(
|
||||||
)
|
)
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
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)
|
_ -> 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(
|
pub fn get_jump_gate(
|
||||||
token: AgentToken,
|
token: AgentToken,
|
||||||
system_symbol: SystemSymbol,
|
system_symbol: SystemSymbol,
|
||||||
waypoint_symbol: WaypointSymbol,
|
waypoint_symbol: WaypointSymbol,
|
||||||
) -> ApiResponse(GetJumpGateResponse) {
|
) -> ApiResponse(JumpGate) {
|
||||||
let request =
|
let request =
|
||||||
api.get(
|
api.get(
|
||||||
AgentAuth(token),
|
AgentAuth(token),
|
||||||
|
@ -277,25 +196,16 @@ pub fn get_jump_gate(
|
||||||
)
|
)
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
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)
|
_ -> 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(
|
pub fn get_shipyard(
|
||||||
token,
|
token,
|
||||||
system_symbol,
|
system_symbol,
|
||||||
waypoint_symbol,
|
waypoint_symbol,
|
||||||
) -> ApiResponse(GetShipyardResponse) {
|
) -> ApiResponse(Shipyard) {
|
||||||
let request =
|
let request =
|
||||||
api.get(
|
api.get(
|
||||||
AgentAuth(token),
|
AgentAuth(token),
|
||||||
|
@ -307,7 +217,7 @@ pub fn get_shipyard(
|
||||||
)
|
)
|
||||||
use response <- api.try_send(request)
|
use response <- api.try_send(request)
|
||||||
case response.status {
|
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)
|
_ -> api.parse_error_response(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,7 +118,7 @@ pub fn get_page(
|
||||||
|> request.set_query(page_query_params([], page, limit))
|
|> request.set_query(page_query_params([], page, limit))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_page_query(
|
pub fn get_page_with_query(
|
||||||
auth_method: AuthMethod,
|
auth_method: AuthMethod,
|
||||||
path: String,
|
path: String,
|
||||||
page: Option(Int),
|
page: Option(Int),
|
||||||
|
|
|
@ -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) {
|
fn jwt_payload_decoder() -> Decoder(JwtPayload) {
|
||||||
use identifier <- decode.field("identifier", decode.string)
|
use identifier <- decode.field("identifier", decode.string)
|
||||||
use version <- decode.field("version", decode.string)
|
use version <- decode.field("version", decode.string)
|
||||||
use reset_date <- decode.optional_field(
|
use reset_date <- decode.optional_field(
|
||||||
"reset_date",
|
"reset_date",
|
||||||
option.None,
|
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)
|
use issued_at_int <- decode.field("iat", decode.int)
|
||||||
let issued_at = birl.from_unix(issued_at_int)
|
let issued_at = birl.from_unix(issued_at_int)
|
||||||
|
|
Loading…
Reference in a new issue