package monochrome import ( "fmt" "io" "net/http" "net/url" ) type ClientConfig struct { ApiURL string } type Client struct { config ClientConfig } func NewClient(config ClientConfig) *Client { return &Client{ config: config, } } func (c *Client) makeRequest(method string, path string, body io.Reader, params map[string]string) (*http.Request, error) { req, err := http.NewRequest(method, c.config.ApiURL+path, body) if err != nil { return nil, fmt.Errorf("failed to create request: %v", err) } p := url.Values{} for key, value := range params { p.Add(key, value) } req.URL.RawQuery = p.Encode() return req, nil } func (c *Client) do(req *http.Request) ([]byte, error) { resp, err := http.DefaultClient.Do(req) if err != nil { return nil, fmt.Errorf("failed to send request: %v", err) } defer resp.Body.Close() if resp.StatusCode == http.StatusTooManyRequests { return nil, fmt.Errorf("rate limited") } data, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("failed to read response: %v", err) } if resp.StatusCode >= 400 { return data, fmt.Errorf("got error from api, data is filled with response") } return data, nil }