package client import ( "bytes" "context" "encoding/json" "fmt" "io" "net/http" ) type Client struct { baseURL string httpClient *http.Client } func New(baseURL string) *Client { return &Client{ baseURL: baseURL, httpClient: http.DefaultClient, } } func (c *Client) get(ctx context.Context, path string, out any) error { return c.do(ctx, http.MethodGet, path, nil, out) } func (c *Client) post(ctx context.Context, path string, body any, out any) error { return c.do(ctx, http.MethodPost, path, body, out) } func (c *Client) put(ctx context.Context, path string, body any, out any) error { return c.do(ctx, http.MethodPut, path, body, out) } func (c *Client) delete(ctx context.Context, path string) error { return c.do(ctx, http.MethodDelete, path, nil, nil) } func (c *Client) do(ctx context.Context, method, path string, body any, out any) error { var bodyReader io.Reader if body != nil { b, err := json.Marshal(body) if err != nil { return fmt.Errorf("marshal: %w", err) } bodyReader = bytes.NewReader(b) } req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, bodyReader) if err != nil { return fmt.Errorf("request: %w", err) } if body != nil { req.Header.Set("Content-Type", "application/json") } resp, err := c.httpClient.Do(req) if err != nil { return fmt.Errorf("do: %w", err) } defer resp.Body.Close() if resp.StatusCode >= 400 { b, _ := io.ReadAll(resp.Body) return fmt.Errorf("api error %d: %s", resp.StatusCode, b) } if out != nil && resp.StatusCode != http.StatusNoContent { if err := json.NewDecoder(resp.Body).Decode(out); err != nil { return fmt.Errorf("decode: %w", err) } } return nil }