@@ -17,17 +17,17 @@ type paginatedResponse[T any] struct {
17
17
Data []T `json:"data"`
18
18
}
19
19
20
- // getPaginatedResults aggregates results from the given
21
- // paginated endpoint using the provided ListOptions.
20
+ // handlePaginatedResults aggregates results from the given
21
+ // paginated endpoint using the provided ListOptions and HTTP method .
22
22
// nolint:funlen
23
- func getPaginatedResults [ T any ](
23
+ func handlePaginatedResults [ T any , O any ](
24
24
ctx context.Context ,
25
25
client * Client ,
26
26
endpoint string ,
27
27
opts * ListOptions ,
28
+ method string ,
29
+ options ... O ,
28
30
) ([]T , error ) {
29
- var resultType paginatedResponse [T ]
30
-
31
31
result := make ([]T , 0 )
32
32
33
33
if opts == nil {
@@ -38,38 +38,76 @@ func getPaginatedResults[T any](
38
38
opts .PageOptions = & PageOptions {Page : 0 }
39
39
}
40
40
41
- // Makes a request to a particular page and
42
- // appends the response to the result
41
+ // Validate options
42
+ numOpts := len (options )
43
+ if numOpts > 1 {
44
+ return nil , fmt .Errorf ("invalid number of options: expected 0 or 1, got %d" , numOpts )
45
+ }
46
+
47
+ // Prepare request body if options are provided
48
+ var reqBody string
49
+
50
+ if numOpts > 0 && ! isNil (options [0 ]) {
51
+ body , err := json .Marshal (options [0 ])
52
+ if err != nil {
53
+ return nil , fmt .Errorf ("failed to marshal request body: %w" , err )
54
+ }
55
+
56
+ reqBody = string (body )
57
+ }
58
+
59
+ // Makes a request to a particular page and appends the response to the result
43
60
handlePage := func (page int ) error {
61
+ var resultType paginatedResponse [T ]
62
+
44
63
// Override the page to be applied in applyListOptionsToRequest(...)
45
64
opts .Page = page
46
65
47
66
// This request object cannot be reused for each page request
48
67
// because it can lead to possible data corruption
49
- req := client .R (ctx ).SetResult (resultType )
68
+ req := client .R (ctx ).SetResult (& resultType )
50
69
51
70
// Apply all user-provided list options to the request
52
71
if err := applyListOptionsToRequest (opts , req ); err != nil {
53
72
return err
54
73
}
55
74
56
- res , err := coupleAPIErrors ( req . Get ( endpoint ))
57
- if err != nil {
58
- return err
75
+ // Set request body if provided
76
+ if reqBody != "" {
77
+ req . SetBody ( reqBody )
59
78
}
60
79
61
- response := res .Result ().(* paginatedResponse [T ])
80
+ var response * paginatedResponse [T ]
81
+ // Execute the appropriate HTTP method
82
+ switch method {
83
+ case "GET" :
84
+ res , err := coupleAPIErrors (req .Get (endpoint ))
85
+ if err != nil {
86
+ return err
87
+ }
88
+
89
+ response = res .Result ().(* paginatedResponse [T ])
90
+ case "PUT" :
91
+ res , err := coupleAPIErrors (req .Put (endpoint ))
92
+ if err != nil {
93
+ return err
94
+ }
95
+
96
+ response = res .Result ().(* paginatedResponse [T ])
97
+ default :
98
+ return fmt .Errorf ("unsupported HTTP method: %s" , method )
99
+ }
62
100
101
+ // Update pagination metadata
63
102
opts .Page = page
64
103
opts .Pages = response .Pages
65
104
opts .Results = response .Results
66
-
67
105
result = append (result , response .Data ... )
68
106
69
107
return nil
70
108
}
71
109
72
- // This helps simplify the logic below
110
+ // Determine starting page
73
111
startingPage := 1
74
112
pageDefined := opts .Page > 0
75
113
@@ -98,6 +136,29 @@ func getPaginatedResults[T any](
98
136
return result , nil
99
137
}
100
138
139
+ // getPaginatedResults aggregates results from the given
140
+ // paginated endpoint using the provided ListOptions.
141
+ func getPaginatedResults [T any ](
142
+ ctx context.Context ,
143
+ client * Client ,
144
+ endpoint string ,
145
+ opts * ListOptions ,
146
+ ) ([]T , error ) {
147
+ return handlePaginatedResults [T , any ](ctx , client , endpoint , opts , "GET" )
148
+ }
149
+
150
+ // putPaginatedResults sends a PUT request and aggregates the results from the given
151
+ // paginated endpoint using the provided ListOptions.
152
+ func putPaginatedResults [T , O any ](
153
+ ctx context.Context ,
154
+ client * Client ,
155
+ endpoint string ,
156
+ opts * ListOptions ,
157
+ options ... O ,
158
+ ) ([]T , error ) {
159
+ return handlePaginatedResults [T , O ](ctx , client , endpoint , opts , "PUT" , options ... )
160
+ }
161
+
101
162
// doGETRequest runs a GET request using the given client and API endpoint,
102
163
// and returns the result
103
164
func doGETRequest [T any ](
0 commit comments