Skip to content

Commit 4e528c1

Browse files
committed
Add pagination support for specific commands.
Add pagination support for specific commands to allow for list ALL the resources they are associated with. They do NOT allow for the end user to specify a limit/page size at this time. This is meant as an interim fix to allow for users with large numbers of clusters, team, team members and networks to be able list them all at one time.
1 parent 834ba38 commit 4e528c1

File tree

7 files changed

+90
-13
lines changed

7 files changed

+90
-13
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Fixed
9+
- Pagination limit on `cb list`, `cb network list`, `cb team list` and
10+
`cb team_member list`.
811

912
## [3.6.1] - 2024-11-05
1013
### Added

src/client/client.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ require "json"
33
require "log"
44
require "promise"
55
require "../ext/stdlib_ext"
6+
require "./pagination"
67

78
module CB
89
class Client

src/client/cluster.cr

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,28 @@ module CB
2121
result
2222
end
2323

24+
struct ClusterListResponse
25+
include JSON::Serializable
26+
pagination_properties
27+
property clusters : Array(CB::Model::Cluster) = [] of CB::Model::Cluster
28+
end
29+
2430
def get_clusters(team_id)
25-
resp = get "clusters?order_field=name&team_id=#{team_id}&limit=200"
26-
Array(CB::Model::Cluster).from_json resp.body, root: "clusters"
31+
clusters : Array(CB::Model::Cluster) = [] of CB::Model::Cluster
32+
query_params = Hash(String, String | Array(String)).new.tap do |params|
33+
params["team_id"] = team_id.to_s
34+
params["order_field"] = "name"
35+
end
36+
37+
loop do
38+
resp = get "clusters?#{HTTP::Params.encode(query_params)}"
39+
data = ClusterListResponse.from_json resp.body
40+
clusters.concat(data.clusters)
41+
break unless data.has_more
42+
query_params["cursor"] = data.next_cursor.to_s
43+
end
44+
45+
clusters
2746
end
2847

2948
# Retrieve the cluster by id or by name.

src/client/network.cr

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,31 @@ module CB
1515
CB::Model::Network.from_json resp.body
1616
end
1717

18+
struct NetworkListResponse
19+
include JSON::Serializable
20+
pagination_properties
21+
property networks : Array(CB::Model::Network)
22+
end
23+
1824
# Get all networks for a team.
1925
#
2026
# https://crunchybridgeapi.docs.apiary.io/#reference/0/networks/list-networks
2127
def get_networks(team : Identifier?)
22-
resp = if team
23-
team_id = team.eid? ? team.to_s : get_team_by_name(team).id
24-
get "networks?team_id=#{team_id}"
25-
else
26-
get "networks"
27-
end
28-
Array(CB::Model::Network).from_json resp.body, root: "networks"
28+
networks = [] of CB::Model::Network
29+
query_params = Hash(String, String | Array(String)).new.tap do |params|
30+
params["order_field"] = "id"
31+
params["team_id"] = team.eid? ? team.to_s : get_team_by_name(team).id if team
32+
end
33+
34+
loop do
35+
resp = get "networks?#{HTTP::Params.encode(query_params)}"
36+
data = NetworkListResponse.from_json resp.body
37+
networks.concat(data.networks)
38+
break unless data.has_more
39+
query_params["cursor"] = data.next_cursor.to_s
40+
end
41+
42+
networks
2943
end
3044

3145
def get_networks(teams : Array(CB::Model::Team))

src/client/pagination.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
macro pagination_properties
2+
property has_more : Bool = false
3+
property next_cursor : String? = nil
4+
end

src/client/team.cr

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,30 @@ module CB
1010
CB::Model::Team.from_json resp.body
1111
end
1212

13+
struct TeamListResponse
14+
include JSON::Serializable
15+
pagination_properties
16+
property teams : Array(CB::Model::Team)
17+
end
18+
1319
# List available teams.
1420
#
1521
# https://crunchybridgeapi.docs.apiary.io/#reference/0/teams/list-teams
1622
def get_teams
17-
resp = get "teams?order_field=name"
18-
Array(CB::Model::Team).from_json resp.body, root: "teams"
23+
teams = [] of CB::Model::Team
24+
query_params = Hash(String, String | Array(String)).new.tap do |params|
25+
params["order_field"] = "name"
26+
end
27+
28+
loop do
29+
resp = get "teams?#{HTTP::Params.encode(query_params)}"
30+
data = TeamListResponse.from_json resp.body
31+
teams.concat(data.teams)
32+
break unless data.has_more
33+
query_params["cursor"] = data.next_cursor.to_s
34+
end
35+
36+
teams
1937
end
2038

2139
# Update a team.

src/client/team_member.cr

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,30 @@ module CB
1313
CB::Model::TeamMember.from_json resp.body
1414
end
1515

16+
struct TeamMemberListResponse
17+
include JSON::Serializable
18+
pagination_properties
19+
property team_members : Array(CB::Model::TeamMember)
20+
end
21+
1622
# List the memebers of a team.
1723
#
1824
# https://crunchybridgeapi.docs.apiary.io/#reference/0/teamsteamidmembers/list-team-members
1925
def list_team_members(team_id)
20-
resp = get "teams/#{team_id}/members"
21-
Array(CB::Model::TeamMember).from_json resp.body, root: "team_members"
26+
team_members = [] of CB::Model::TeamMember
27+
query_params = Hash(String, String | Array(String)).new.tap do |params|
28+
params["order_field"] = "email"
29+
end
30+
31+
loop do
32+
resp = get "teams/#{team_id}/members?#{HTTP::Params.encode(query_params)}"
33+
data = TeamMemberListResponse.from_json resp.body
34+
team_members.concat(data.team_members)
35+
break unless data.has_more
36+
query_params["cursor"] = data.next_cursor.to_s
37+
end
38+
39+
team_members
2240
end
2341

2442
# Retrieve details about a team member.

0 commit comments

Comments
 (0)