(method, url string, payload io.Reader, headers map[string]string)
| 33 | } |
| 34 | |
| 35 | func (c *HttpClient) MakeRequest(method, url string, payload io.Reader, headers map[string]string) ([]byte, error) { |
| 36 | req, err := http.NewRequest(method, url, payload) |
| 37 | if err != nil { |
| 38 | return nil, fmt.Errorf("failed to initialize request: %s", err.Error()) |
| 39 | } |
| 40 | req.Header.Add("Content-Type", "application/json") |
| 41 | req.Header.Add("Accept", "application/json") |
| 42 | req.Header.Add("User-Agent", c.UserAgent) |
| 43 | |
| 44 | for key, value := range headers { |
| 45 | req.Header.Add(key, value) |
| 46 | } |
| 47 | |
| 48 | resp, err := c.Client.Do(req) |
| 49 | if err != nil { |
| 50 | return nil, fmt.Errorf("failed to make request: %s", err.Error()) |
| 51 | } |
| 52 | |
| 53 | defer func() { |
| 54 | if err := resp.Body.Close(); err != nil { |
| 55 | slog.Warn("response body close failed", "context", err.Error()) |
| 56 | } |
| 57 | }() |
| 58 | |
| 59 | body, err := io.ReadAll(resp.Body) |
| 60 | if err != nil { |
| 61 | return nil, fmt.Errorf("failed to read response body: %s", err.Error()) |
| 62 | } |
| 63 | |
| 64 | if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 65 | slog.Debug("response info", logging.RuntimeAttr(string(body))) |
| 66 | return nil, fmt.Errorf("got %d from %s", resp.StatusCode, url) |
| 67 | } |
| 68 | |
| 69 | return body, nil |
| 70 | } |
| 71 | |
| 72 | func ParseResp[T any](body []byte, target *T) error { |
| 73 |
no test coverage detected