(method, path string, body interface{})
| 51 | } |
| 52 | |
| 53 | func (s *RemoteDownloadService) doRequest(method, path string, body interface{}) (*http.Response, error) { |
| 54 | var bodyReader io.Reader |
| 55 | if body != nil { |
| 56 | jsonBody, err := json.Marshal(body) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | bodyReader = bytes.NewBuffer(jsonBody) |
| 61 | } |
| 62 | |
| 63 | req, err := http.NewRequestWithContext(s.ctx, method, s.BaseURL+path, bodyReader) |
| 64 | if err != nil { |
| 65 | return nil, err |
| 66 | } |
| 67 | |
| 68 | req.Header.Set("Authorization", "Bearer "+s.Token) |
| 69 | if body != nil { |
| 70 | req.Header.Set("Content-Type", "application/json") |
| 71 | } |
| 72 | |
| 73 | resp, err := s.Client.Do(req) |
| 74 | if err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 | |
| 78 | if resp.StatusCode >= 400 { |
| 79 | defer func() { _, _ = io.Copy(io.Discard, resp.Body); _ = resp.Body.Close() }() |
| 80 | // Limit error body read to 1KB to prevent DoS |
| 81 | bodyBytes, _ := io.ReadAll(io.LimitReader(resp.Body, types.KB)) |
| 82 | return nil, fmt.Errorf("API error %d: %s", resp.StatusCode, string(bodyBytes)) |
| 83 | } |
| 84 | |
| 85 | return resp, nil |
| 86 | } |
| 87 | |
| 88 | // List returns the status of all active and completed downloads. |
| 89 | func (s *RemoteDownloadService) List() ([]types.DownloadStatus, error) { |
no test coverage detected