Skip to content

Commit

Permalink
feat(gateway): add paging support
Browse files Browse the repository at this point in the history
  • Loading branch information
kangasta committed Mar 11, 2024
1 parent 208f3fd commit 36b7884
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]

### Added

- Gateway: add paging support to `GetGateways` method.

## [8.2.0]

### Added
Expand Down
11 changes: 9 additions & 2 deletions upcloud/request/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ const gatewayBaseURL string = "/gateway"

type GetGatewaysRequest struct {
Filters []QueryFilter
Page *Page
}

func (r *GetGatewaysRequest) RequestURL() string {
if len(r.Filters) == 0 {
f := make([]QueryFilter, len(r.Filters))
copy(f, r.Filters)
if r.Page != nil {
f = append(f, r.Page)
}

if len(f) == 0 {
return gatewayBaseURL
}

return fmt.Sprintf("%s?%s", gatewayBaseURL, encodeQueryFilters(r.Filters))
return fmt.Sprintf("%s?%s", gatewayBaseURL, encodeQueryFilters(f))
}

type GetGatewayRequest struct {
Expand Down
33 changes: 25 additions & 8 deletions upcloud/request/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,35 @@ func TestGetGatewaysRequest(t *testing.T) {
r := GetGatewaysRequest{}
assert.Equal(t, "/gateway", r.RequestURL())

r = GetGatewaysRequest{
Filters: []QueryFilter{
FilterLabel{
Label: upcloud.Label{
Key: "color",
Value: "green",
},
filters := []QueryFilter{
FilterLabel{
Label: upcloud.Label{
Key: "color",
Value: "green",
},
FilterLabelKey{Key: "size"},
},
FilterLabelKey{Key: "size"},
}

r = GetGatewaysRequest{
Filters: filters,
}
assert.Equal(t, "/gateway?label=color%3Dgreen&label=size", r.RequestURL())

page := &Page{
Size: 15,
Number: 3,
}
r = GetGatewaysRequest{
Page: page,
}
assert.Equal(t, "/gateway?limit=15&offset=30", r.RequestURL())

r = GetGatewaysRequest{
Filters: filters,
Page: page,
}
assert.Equal(t, "/gateway?label=color%3Dgreen&label=size&limit=15&offset=30", r.RequestURL())
}

func TestGetGatewayRequest(t *testing.T) {
Expand Down

0 comments on commit 36b7884

Please sign in to comment.