summaryrefslogtreecommitdiffstats
path: root/internal/client/client.go
blob: 098479f6aa115ec0b343ba03ae6d9ed13c2e36a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package client

import (
	"bytes"
	"dborg/internal/config"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"time"
)

type Client struct {
	config     *config.Config
	httpClient *http.Client
}

func New(cfg *config.Config) (*Client, error) {
	if err := cfg.Validate(); err != nil {
		return nil, err
	}

	return &Client{
		config: cfg,
		httpClient: &http.Client{
			Timeout: cfg.Timeout,
		},
	}, nil
}

func (c *Client) doRequest(method, path string, params url.Values, body interface{}) ([]byte, error) {
	fullURL := c.config.BaseURL + path
	if params != nil && len(params) > 0 {
		fullURL += "?" + params.Encode()
	}

	var reqBody io.Reader
	if body != nil {
		jsonData, err := json.Marshal(body)
		if err != nil {
			return nil, fmt.Errorf("failed to marshal request body: %w", err)
		}
		reqBody = bytes.NewBuffer(jsonData)
	}

	req, err := http.NewRequest(method, fullURL, reqBody)
	if err != nil {
		return nil, fmt.Errorf("failed to create request: %w", err)
	}

	req.Header.Set("X-API-Key", c.config.APIKey)
	req.Header.Set("User-Agent", c.config.UserAgent)
	if body != nil {
		req.Header.Set("Content-Type", "application/json")
	}

	var resp *http.Response
	var lastErr error

	for attempt := 0; attempt <= c.config.MaxRetries; attempt++ {
		if attempt > 0 {
			time.Sleep(time.Duration(attempt) * time.Second)
		}

		resp, err = c.httpClient.Do(req)
		if err != nil {
			lastErr = err
			continue
		}

		defer resp.Body.Close()

		if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusCreated {
			return io.ReadAll(resp.Body)
		}

		bodyBytes, _ := io.ReadAll(resp.Body)

		switch resp.StatusCode {
		case http.StatusForbidden:
			lastErr = fmt.Errorf("access denied (403): %s - This endpoint requires premium access", string(bodyBytes))
		case http.StatusUnauthorized:
			lastErr = fmt.Errorf("unauthorized (401): %s - Check your API key", string(bodyBytes))
		case http.StatusTooManyRequests:
			lastErr = fmt.Errorf("rate limit exceeded (429): %s", string(bodyBytes))
		case http.StatusBadRequest:
			lastErr = fmt.Errorf("bad request (400): %s", string(bodyBytes))
		default:
			lastErr = fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(bodyBytes))
		}

		if resp.StatusCode != http.StatusTooManyRequests && resp.StatusCode < 500 {
			break
		}
	}

	return nil, lastErr
}

func (c *Client) Get(path string, params url.Values) ([]byte, error) {
	return c.doRequest(http.MethodGet, path, params, nil)
}

func (c *Client) Post(path string, body interface{}) ([]byte, error) {
	return c.doRequest(http.MethodPost, path, nil, body)
}

func (c *Client) Delete(path string) ([]byte, error) {
	return c.doRequest(http.MethodDelete, path, nil, nil)
}

func (c *Client) Patch(path string, body interface{}) ([]byte, error) {
	return c.doRequest(http.MethodPatch, path, nil, body)
}

func (c *Client) Put(path string, body interface{}) ([]byte, error) {
	return c.doRequest(http.MethodPut, path, nil, body)
}