getResponse performs the API call and returns the response's link header values. If the "Link" header is missing, the returned slice will be nil.
(client *http.Client, url safeurl.SafeURL, data interface{})
| 81 | // getResponse performs the API call and returns the response's link header values. |
| 82 | // If the "Link" header is missing, the returned slice will be nil. |
| 83 | func getResponse(client *http.Client, url safeurl.SafeURL, data interface{}) ([]string, error) { |
| 84 | req, err := http.NewRequest("GET", url.String(), nil) |
| 85 | if err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | |
| 89 | req.Header.Set("Content-Type", "application/json; charset=utf-8") |
| 90 | resp, err := client.Do(req) |
| 91 | if err != nil { |
| 92 | return nil, err |
| 93 | } |
| 94 | defer resp.Body.Close() |
| 95 | |
| 96 | success := resp.StatusCode >= 200 && resp.StatusCode < 300 |
| 97 | if !success { |
| 98 | return nil, errors.New("api call failed") |
| 99 | } |
| 100 | |
| 101 | links := resp.Header["Link"] |
| 102 | |
| 103 | if resp.StatusCode == http.StatusNoContent { |
| 104 | return links, nil |
| 105 | } |
| 106 | |
| 107 | b, err := io.ReadAll(resp.Body) |
| 108 | if err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | |
| 112 | err = json.Unmarshal(b, &data) |
| 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | |
| 117 | return links, nil |
| 118 | } |
no test coverage detected