-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
53 lines (42 loc) · 1.02 KB
/
api.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
package main
import (
"encoding/json"
"net/http"
)
type Response struct {
Field1 string `json:"id"`
Field2 string `json:"payment_no"`
Field3 string `json:"transaction_date"`
Field4 string `json:"total_collected"`
}
func GetApiData(apiUrl string, bearerToken string) ([]Response, error) {
var result []Response
for apiUrl != "" {
// Make API request
client := &http.Client{}
req, err := http.NewRequest("GET", apiUrl, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+bearerToken)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Decode response body
var response struct {
Results []Response `json:"results"`
NextUrl string `json:"nextUrl"`
}
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return nil, err
}
// Append results to the overall result
result = append(result, response.Results...)
// Update apiUrl for the next page
apiUrl = response.NextUrl
}
return result, nil
}