Make page and limit optional
Some checks are pending
test / test (push) Waiting to run

This commit is contained in:
Lily Rose 2025-06-17 23:20:23 +10:00
parent 64f3729d0c
commit 07793041b6
6 changed files with 48 additions and 34 deletions

View file

@ -1,4 +1,5 @@
import gleam/dynamic/decode.{type Decoder} import gleam/dynamic/decode.{type Decoder}
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}
import models/agent_symbol.{type AgentSymbol} import models/agent_symbol.{type AgentSymbol}
@ -17,8 +18,8 @@ fn list_public_agents_response_decoder() -> Decoder(ListPublicAgentsResponse) {
pub fn list_public_agents( pub fn list_public_agents(
token: AgentToken, token: AgentToken,
page: Int, page: Option(Int),
limit: Int, limit: Option(Int),
) -> ApiResponse(PagedData(ListPublicAgentsResponse)) { ) -> ApiResponse(PagedData(ListPublicAgentsResponse)) {
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)

View file

@ -1,6 +1,7 @@
import endpoints/fleet import endpoints/fleet
import gleam/dynamic/decode.{type Decoder} import gleam/dynamic/decode.{type Decoder}
import gleam/json import gleam/json
import gleam/option.{type Option}
import models/agent.{type Agent} import models/agent.{type Agent}
import models/contract.{type Contract} import models/contract.{type Contract}
import models/contract_id.{type ContractId} import models/contract_id.{type ContractId}
@ -21,8 +22,8 @@ fn list_contracts_response_decoder() -> Decoder(ListContractsResponse) {
pub fn list_contracts( pub fn list_contracts(
token: AgentToken, token: AgentToken,
page: Int, page: Option(Int),
limit: Int, limit: Option(Int),
) -> ApiResponse(PagedData(ListContractsResponse)) { ) -> ApiResponse(PagedData(ListContractsResponse)) {
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)

View file

@ -1,4 +1,5 @@
import gleam/dynamic/decode.{type Decoder} import gleam/dynamic/decode.{type Decoder}
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}
@ -15,8 +16,8 @@ fn list_factions_response_decoder() -> Decoder(ListFactionsResponse) {
pub fn list_factions( pub fn list_factions(
token: AgentToken, token: AgentToken,
page: Int, page: Option(Int),
limit: Int, limit: Option(Int),
) -> ApiResponse(PagedData(ListFactionsResponse)) { ) -> ApiResponse(PagedData(ListFactionsResponse)) {
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)
@ -72,8 +73,8 @@ fn get_my_factions_response_decoder() -> Decoder(GetMyFactionsResponse) {
pub fn get_my_factions( pub fn get_my_factions(
token: AgentToken, token: AgentToken,
page: Int, page: Option(Int),
limit: Int, limit: Option(Int),
) -> ApiResponse(PagedData(GetMyFactionsResponse)) { ) -> ApiResponse(PagedData(GetMyFactionsResponse)) {
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)

View file

@ -49,8 +49,8 @@ pub fn list_ships_decoder() -> Decoder(ListShipsResponse) {
pub fn list_ships( pub fn list_ships(
token: AgentToken, token: AgentToken,
page: Int, page: Option(Int),
limit: Int, limit: Option(Int),
) -> ApiResponse(PagedData(ListShipsResponse)) { ) -> ApiResponse(PagedData(ListShipsResponse)) {
let request = api.get_page(AgentAuth(token), "/my/ships", page, limit) let request = api.get_page(AgentAuth(token), "/my/ships", page, limit)
use response <- api.try_send(request) use response <- api.try_send(request)

View file

@ -29,8 +29,8 @@ fn list_systems_response_decoder() -> Decoder(ListSystemsResponse) {
pub fn list_systems( pub fn list_systems(
token: AgentToken, token: AgentToken,
page: Int, page: Option(Int),
limit: Int, limit: Option(Int),
) -> ApiResponse(PagedData(ListSystemsResponse)) { ) -> ApiResponse(PagedData(ListSystemsResponse)) {
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)
@ -80,22 +80,18 @@ fn list_system_waypoints_response_decoder() -> Decoder(
pub fn list_system_waypoints( pub fn list_system_waypoints(
token: AgentToken, token: AgentToken,
system_symbol: SystemSymbol, system_symbol: SystemSymbol,
page: Int, page: Option(Int),
limit: Int, limit: Option(Int),
type_: Option(WaypointType), type_: Option(WaypointType),
traits: List(WaypointTraitSymbol), traits: List(WaypointTraitSymbol),
) -> ApiResponse(PagedData(ListSystemWaypointsResponse)) { ) -> ApiResponse(PagedData(ListSystemWaypointsResponse)) {
let traits_query = let query =
traits list.map(traits, fn(trait) {
|> list.map(fn(trait) {
#("traits", waypoint_trait_symbol.to_string(trait)) #("traits", waypoint_trait_symbol.to_string(trait))
}) })
let query = case type_ { let query = case type_ {
option.Some(type_) -> [ option.Some(type_) -> [#("type", waypoint_type.to_string(type_)), ..query]
#("type", waypoint_type.to_string(type_)), option.None -> query
..traits_query
]
option.None -> traits_query
} }
let request = let request =
api.get_page_query( api.get_page_query(

View file

@ -92,27 +92,42 @@ pub fn get_query(
make_request(http.Get, auth_method, path) |> request.set_query(query) make_request(http.Get, auth_method, path) |> request.set_query(query)
} }
pub fn get_page(auth_method: AuthMethod, path: String, page: Int, limit: Int) { fn page_query_params(
initial_query: List(#(String, String)),
page: Option(Int),
limit: Option(Int),
) -> List(#(String, String)) {
let query = initial_query
let query = case page {
option.Some(page) -> [#("page", int.to_string(page)), ..query]
option.None -> query
}
let query = case limit {
option.Some(page) -> [#("limit", int.to_string(page)), ..query]
option.None -> query
}
query
}
pub fn get_page(
auth_method: AuthMethod,
path: String,
page: Option(Int),
limit: Option(Int),
) {
make_request(http.Get, auth_method, path) make_request(http.Get, auth_method, path)
|> request.set_query([ |> request.set_query(page_query_params([], page, limit))
#("page", int.to_string(page)),
#("limit", int.to_string(limit)),
])
} }
pub fn get_page_query( pub fn get_page_query(
auth_method: AuthMethod, auth_method: AuthMethod,
path: String, path: String,
page: Int, page: Option(Int),
limit: Int, limit: Option(Int),
query: List(#(String, String)), query: List(#(String, String)),
) { ) {
make_request(http.Get, auth_method, path) make_request(http.Get, auth_method, path)
|> request.set_query([ |> request.set_query(page_query_params(query, page, limit))
#("page", int.to_string(page)),
#("limit", int.to_string(limit)),
..query
])
} }
pub fn post(auth_method: AuthMethod, path: String) { pub fn post(auth_method: AuthMethod, path: String) {