@@ -17,17 +17,17 @@ type paginatedResponse[T any] struct {
1717 Data []T `json:"data"`
1818}
1919
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 .
2222// nolint:funlen
23- func getPaginatedResults [ T any ](
23+ func handlePaginatedResults [ T any , O any ](
2424 ctx context.Context ,
2525 client * Client ,
2626 endpoint string ,
2727 opts * ListOptions ,
28+ method string ,
29+ options ... O ,
2830) ([]T , error ) {
29- var resultType paginatedResponse [T ]
30-
3131 result := make ([]T , 0 )
3232
3333 if opts == nil {
@@ -38,38 +38,76 @@ func getPaginatedResults[T any](
3838 opts .PageOptions = & PageOptions {Page : 0 }
3939 }
4040
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
4360 handlePage := func (page int ) error {
61+ var resultType paginatedResponse [T ]
62+
4463 // Override the page to be applied in applyListOptionsToRequest(...)
4564 opts .Page = page
4665
4766 // This request object cannot be reused for each page request
4867 // because it can lead to possible data corruption
49- req := client .R (ctx ).SetResult (resultType )
68+ req := client .R (ctx ).SetResult (& resultType )
5069
5170 // Apply all user-provided list options to the request
5271 if err := applyListOptionsToRequest (opts , req ); err != nil {
5372 return err
5473 }
5574
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 )
5978 }
6079
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+ }
62100
101+ // Update pagination metadata
63102 opts .Page = page
64103 opts .Pages = response .Pages
65104 opts .Results = response .Results
66-
67105 result = append (result , response .Data ... )
68106
69107 return nil
70108 }
71109
72- // This helps simplify the logic below
110+ // Determine starting page
73111 startingPage := 1
74112 pageDefined := opts .Page > 0
75113
@@ -98,6 +136,29 @@ func getPaginatedResults[T any](
98136 return result , nil
99137}
100138
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+
101162// doGETRequest runs a GET request using the given client and API endpoint,
102163// and returns the result
103164func doGETRequest [T any ](
0 commit comments