-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpaginator.go
90 lines (73 loc) · 2.79 KB
/
paginator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package sailpoint
import (
"context"
"errors"
"net/http"
"reflect"
"strings"
v3 "github.com/sailpoint-oss/golang-sdk/v2/api_v3"
)
func PaginateWithDefaults[T any](f interface{}) ([]T, *http.Response, error) {
return Paginate[T](f, 0, 250, 10000)
}
func Paginate[T any](f interface{}, initialOffset int32, increment int32, limit int32) ([]T, *http.Response, error) {
var offset int32 = initialOffset
var returnObject []T
var latestResponse *http.Response
incrementResp := Invoke(f, "Limit", increment)
for offset < limit {
// first invoke the Offset command to set the new offset
offsetResp := Invoke(incrementResp[0].Interface(), "Offset", offset)
// invoke the Execute function to get the response
resp := Invoke(offsetResp[0].Interface(), "Execute")
// convert the expected return values to their respective types
actualValue := resp[0].Interface().([]T)
latestResponse = resp[1].Interface().(*http.Response)
err := resp[2].Interface()
if err != nil {
return returnObject, latestResponse, err.(error)
}
// append the results to the main return object
returnObject = append(returnObject, actualValue...)
// check if this is the last set in the response. This could be enhanced by inspecting the header for the max results
if int32(len(actualValue)) < increment {
break
}
offset += increment
}
return returnObject, latestResponse, nil
}
func PaginateSearchApi(ctx context.Context, apiClient *APIClient, search v3.Search, initialOffset int32, increment int32, limit int32) ([]map[string]interface{}, *http.Response, error) {
var offset int32 = initialOffset
var returnObject []map[string]interface{}
var r *http.Response
if len(search.Sort) != 1 {
return nil, nil, errors.New("search must include exactly one sort parameter to paginate properly")
}
for offset < limit {
if len(returnObject) > 0 {
search.SearchAfter = []string{returnObject[len(returnObject)-1][strings.Trim(search.Sort[0], "-")].(string)}
}
// convert the expected return values to their respective types
actualValue, latestResponse, err := apiClient.V3.SearchAPI.SearchPost(ctx).Limit(increment).Search(search).Execute()
if err != nil {
return returnObject, latestResponse, err.(error)
}
// append the results to the main return object
returnObject = append(returnObject, actualValue...)
r = latestResponse
// check if this is the last set in the response. This could be enhanced by inspecting the header for the max results
if int32(len(actualValue)) < increment {
break
}
offset += increment
}
return returnObject, r, nil
}
func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value {
inputs := make([]reflect.Value, len(args))
for i, v := range args {
inputs[i] = reflect.ValueOf(v)
}
return reflect.ValueOf(any).MethodByName(name).Call(inputs)
}