Skip to content

Commit

Permalink
gateway: add client cursor handling
Browse files Browse the repository at this point in the history
Add ListOptions to request list limit, sort direction and provide a
cursor to start the fetch from.

Add Cursor to Response fetched from an http header.
  • Loading branch information
sgotti committed Sep 25, 2023
1 parent 12a9d49 commit 7c5eb94
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions services/gateway/api/types/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package types

type SortDirection string

const (
SortDirectionAsc SortDirection = "asc"
SortDirectionDesc SortDirection = "desc"
)
36 changes: 36 additions & 0 deletions services/gateway/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,42 @@ import (

var jsonContent = http.Header{"Content-Type": []string{"application/json"}}

const (
agolaCursorHeader = "X-Agola-Cursor"
)

type Response struct {
*http.Response

Cursor string
}

type ListOptions struct {
Cursor string

Limit int
SortDirection gwapitypes.SortDirection
}

func (o *ListOptions) Add(q url.Values) {
if o == nil {
return
}

if o.Cursor != "" {
q.Add("cursor", o.Cursor)
}

if o.Limit != 0 {
q.Add("limit", strconv.Itoa(o.Limit))
}

switch o.SortDirection {
case gwapitypes.SortDirectionDesc:
q.Add("sortdirection", "desc")
case gwapitypes.SortDirectionAsc:
q.Add("sortdirection", "asc")
}
}

type Client struct {
Expand Down Expand Up @@ -92,6 +126,8 @@ func (c *Client) getResponse(ctx context.Context, method, path string, query url
return resp, errors.WithStack(err)
}

resp.Cursor = resp.Response.Header.Get(agolaCursorHeader)

return resp, nil
}

Expand Down

0 comments on commit 7c5eb94

Please sign in to comment.