| 295 | } |
| 296 | |
| 297 | func sendRequest(client httpDoer, req *http.Request) ([]byte, error) { |
| 298 | // http.Client returns a non-nil error only if it encounters an error |
| 299 | // caused by client policy (such as CheckRedirect), or failure to speak |
| 300 | // HTTP (such as a network connectivity problem). A non-2xx status code |
| 301 | // doesn't cause an error. |
| 302 | resp, err := client.Do(req) |
| 303 | if err != nil { |
| 304 | return nil, err |
| 305 | } |
| 306 | |
| 307 | // When the http.Client returns a non-nil error, it is the |
| 308 | // responsibility of the caller to read the response body till an EOF is |
| 309 | // encountered and to close it. |
| 310 | body, err := io.ReadAll(resp.Body) |
| 311 | resp.Body.Close() |
| 312 | if err != nil { |
| 313 | return nil, err |
| 314 | } |
| 315 | |
| 316 | if resp.StatusCode == http.StatusOK { |
| 317 | return body, nil |
| 318 | } |
| 319 | logger.Warningf("http status %d, body: %s", resp.StatusCode, string(body)) |
| 320 | return nil, fmt.Errorf("http status %d, body: %s", resp.StatusCode, string(body)) |
| 321 | } |
| 322 | |
| 323 | func tokenInfoFromResponse(respBody []byte) (*tokenInfo, error) { |
| 324 | respData := &responseParameters{} |