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, hostname string, path safeurl.SafeURL, data any)
| 80 | // getResponse performs the API call and returns the response's link header values. |
| 81 | // If the "Link" header is missing, the returned slice will be nil. |
| 82 | func getResponse(client *http.Client, hostname string, path safeurl.SafeURL, data any) ([]string, error) { |
| 83 | // The Content-Type this site used to set explicitly is the one the client already sends, |
| 84 | // so it is no longer stated here. |
| 85 | // TODO(api-client-rollout) |
| 86 | // This line of code is part of a mechanical roll out of the api client. |
| 87 | // As a follow up, consider whether the api client can be injected to this call site, rather than constructed |
| 88 | resp, err := api.NewClientFromHTTP(client).Request(hostname, http.MethodGet, path.String(), nil) |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | defer resp.Body.Close() |
| 93 | |
| 94 | links := resp.Header["Link"] |
| 95 | |
| 96 | if resp.StatusCode == http.StatusNoContent { |
| 97 | return links, nil |
| 98 | } |
| 99 | |
| 100 | b, err := io.ReadAll(resp.Body) |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | |
| 105 | err = json.Unmarshal(b, &data) |
| 106 | if err != nil { |
| 107 | return nil, err |
| 108 | } |
| 109 | |
| 110 | return links, nil |
| 111 | } |
no test coverage detected