@@ -5,15 +5,17 @@ import (
5
5
"encoding/json"
6
6
"net/http"
7
7
"net/url"
8
+ "regexp"
8
9
"strconv"
10
+ "strings"
9
11
)
10
12
11
13
// ListEntitiesRequest is the request to the [Client.ListEntities] method.
12
14
type ListEntitiesRequest struct {
13
- // Filter for selecting only a subset of all entities.
14
- Filter string
15
+ // Filters for selecting only a subset of all entities.
16
+ Filters [] string
15
17
// Fields for selecting only parts of the full data structure of each entity.
16
- Fields string
18
+ Fields [] string
17
19
// Offset for pagination.
18
20
Offset int64
19
21
// Limit for pagination.
@@ -26,8 +28,12 @@ type ListEntitiesRequest struct {
26
28
type ListEntitiesResponse struct {
27
29
// Entities in the response.
28
30
Entities []* Entity
31
+ // NextPageToken contains the next page token.
32
+ NextPageToken string
29
33
}
30
34
35
+ var linkURLRegexp = regexp .MustCompile (`<(.*)>; *rel="next"` )
36
+
31
37
// ListEntities lists entities in the catalog.
32
38
//
33
39
// See: https://backstage.io/docs/features/software-catalog/software-catalog-api/#get-entities
@@ -40,23 +46,34 @@ func (c *Client) ListEntities(ctx context.Context, request *ListEntitiesRequest)
40
46
if request .Limit > 0 {
41
47
query .Set ("limit" , strconv .FormatInt (request .Limit , 10 ))
42
48
}
43
- if request . Filter != "" {
44
- query .Set ("filter" , request . Filter )
49
+ for _ , filter := range request . Filters {
50
+ query .Add ("filter" , filter )
45
51
}
46
- if request .Fields != "" {
47
- query .Set ("fields" , request .Fields )
52
+ if len ( request .Fields ) > 0 {
53
+ query .Set ("fields" , strings . Join ( request .Fields , "," ) )
48
54
}
49
55
if request .After != "" {
50
56
query .Set ("after" , request .After )
51
57
}
52
58
var rawEntities []json.RawMessage
59
+ var nextPageToken string
53
60
if err := c .get (ctx , path , query , func (response * http.Response ) error {
61
+ for _ , link := range response .Header .Values ("link" ) {
62
+ if matches := linkURLRegexp .FindStringSubmatch (link ); len (matches ) > 1 {
63
+ linkURL , err := url .ParseRequestURI (matches [1 ])
64
+ if err != nil {
65
+ return err
66
+ }
67
+ nextPageToken = linkURL .Query ().Get ("after" )
68
+ }
69
+ }
54
70
return json .NewDecoder (response .Body ).Decode (& rawEntities )
55
71
}); err != nil {
56
72
return nil , err
57
73
}
58
74
response := ListEntitiesResponse {
59
- Entities : make ([]* Entity , 0 , len (rawEntities )),
75
+ Entities : make ([]* Entity , 0 , len (rawEntities )),
76
+ NextPageToken : nextPageToken ,
60
77
}
61
78
for _ , rawEntity := range rawEntities {
62
79
entity := & Entity {
0 commit comments