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/option.{type Option}
import models/agent.{type Agent}
import models/agent_event.{type AgentEvent}
import models/agent_symbol.{type AgentSymbol}
@ -17,8 +18,8 @@ fn list_public_agents_response_decoder() -> Decoder(ListPublicAgentsResponse) {
pub fn list_public_agents(
token: AgentToken,
page: Int,
limit: Int,
page: Option(Int),
limit: Option(Int),
) -> ApiResponse(PagedData(ListPublicAgentsResponse)) {
let request = api.get_page(AgentAuth(token), "/agents", page, limit)
use response <- api.try_send(request)

View file

@ -1,6 +1,7 @@
import endpoints/fleet
import gleam/dynamic/decode.{type Decoder}
import gleam/json
import gleam/option.{type Option}
import models/agent.{type Agent}
import models/contract.{type Contract}
import models/contract_id.{type ContractId}
@ -21,8 +22,8 @@ fn list_contracts_response_decoder() -> Decoder(ListContractsResponse) {
pub fn list_contracts(
token: AgentToken,
page: Int,
limit: Int,
page: Option(Int),
limit: Option(Int),
) -> ApiResponse(PagedData(ListContractsResponse)) {
let request = api.get_page(AgentAuth(token), "/my/contracts", page, limit)
use response <- api.try_send(request)

View file

@ -1,4 +1,5 @@
import gleam/dynamic/decode.{type Decoder}
import gleam/option.{type Option}
import models/faction.{type Faction}
import models/faction_symbol.{type FactionSymbol}
import utils/api.{type ApiResponse, type PagedData}
@ -15,8 +16,8 @@ fn list_factions_response_decoder() -> Decoder(ListFactionsResponse) {
pub fn list_factions(
token: AgentToken,
page: Int,
limit: Int,
page: Option(Int),
limit: Option(Int),
) -> ApiResponse(PagedData(ListFactionsResponse)) {
let request = api.get_page(AgentAuth(token), "/factions", page, limit)
use response <- api.try_send(request)
@ -72,8 +73,8 @@ fn get_my_factions_response_decoder() -> Decoder(GetMyFactionsResponse) {
pub fn get_my_factions(
token: AgentToken,
page: Int,
limit: Int,
page: Option(Int),
limit: Option(Int),
) -> ApiResponse(PagedData(GetMyFactionsResponse)) {
let request = api.get_page(AgentAuth(token), "/my/factions", page, limit)
use response <- api.try_send(request)

View file

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

View file

@ -29,8 +29,8 @@ fn list_systems_response_decoder() -> Decoder(ListSystemsResponse) {
pub fn list_systems(
token: AgentToken,
page: Int,
limit: Int,
page: Option(Int),
limit: Option(Int),
) -> ApiResponse(PagedData(ListSystemsResponse)) {
let request = api.get_page(AgentAuth(token), "/systems", page, limit)
use response <- api.try_send(request)
@ -80,22 +80,18 @@ fn list_system_waypoints_response_decoder() -> Decoder(
pub fn list_system_waypoints(
token: AgentToken,
system_symbol: SystemSymbol,
page: Int,
limit: Int,
page: Option(Int),
limit: Option(Int),
type_: Option(WaypointType),
traits: List(WaypointTraitSymbol),
) -> ApiResponse(PagedData(ListSystemWaypointsResponse)) {
let traits_query =
traits
|> list.map(fn(trait) {
let query =
list.map(traits, fn(trait) {
#("traits", waypoint_trait_symbol.to_string(trait))
})
let query = case type_ {
option.Some(type_) -> [
#("type", waypoint_type.to_string(type_)),
..traits_query
]
option.None -> traits_query
option.Some(type_) -> [#("type", waypoint_type.to_string(type_)), ..query]
option.None -> query
}
let request =
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)
}
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)
|> request.set_query([
#("page", int.to_string(page)),
#("limit", int.to_string(limit)),
])
|> request.set_query(page_query_params([], page, limit))
}
pub fn get_page_query(
auth_method: AuthMethod,
path: String,
page: Int,
limit: Int,
page: Option(Int),
limit: Option(Int),
query: List(#(String, String)),
) {
make_request(http.Get, auth_method, path)
|> request.set_query([
#("page", int.to_string(page)),
#("limit", int.to_string(limit)),
..query
])
|> request.set_query(page_query_params(query, page, limit))
}
pub fn post(auth_method: AuthMethod, path: String) {