-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
63 lines (52 loc) · 1.57 KB
/
client.go
File metadata and controls
63 lines (52 loc) · 1.57 KB
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
package katapult
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"time"
)
var baseURL = "https://api.katapult.io/core/v1"
var errUnexpectedStatusCode = errors.New("unexpected status code")
// NewHTTPClient returns a configured HTTP client.
func NewHTTPClient() *http.Client {
return &http.Client{Timeout: 10 * time.Second}
}
// DoRequest performs an HTTP request and decodes the response.
func (p *Provider) DoRequest(ctx context.Context, method, url string, body interface{}, response interface{}) error {
var reqBody []byte
var err error
if body != nil {
reqBody, err = json.Marshal(body)
if err != nil {
return fmt.Errorf("failed to marshal request body: %w", err)
}
}
apiURL := baseURL + url
req, err := http.NewRequestWithContext(ctx, method, apiURL, bytes.NewBuffer(reqBody))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+p.APIToken)
req.Header.Set("Content-Type", "application/json")
client := NewHTTPClient()
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to execute request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %w", err)
}
return fmt.Errorf("%w: %d, response body: %s", errUnexpectedStatusCode, resp.StatusCode, string(respBody))
}
if err := json.NewDecoder(resp.Body).Decode(response); err != nil {
return fmt.Errorf("failed to decode response: %w", err)
}
return nil
}