-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
123 lines (106 loc) · 2.86 KB
/
Copy pathhttp.go
File metadata and controls
123 lines (106 loc) · 2.86 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
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
120
121
122
123
package kricket
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
// apiResponse is the standard envelope returned by Kricket API endpoints.
type apiResponse struct {
Success bool `json:"success"`
Data json.RawMessage `json:"data,omitempty"`
Error *apiError `json:"error,omitempty"`
}
type apiError struct {
Code string `json:"code"`
Message string `json:"message"`
}
// Error represents an API error from Kricket.
type Error struct {
Code string
Message string
Status int
}
func (e *Error) Error() string {
return fmt.Sprintf("kricket: %s: %s (HTTP %d)", e.Code, e.Message, e.Status)
}
// httpClient is the internal HTTP transport used by all sub-clients.
type httpClient struct {
baseURL string
apiKey string
client *http.Client
}
func newHTTPClient(baseURL, apiKey string, timeout time.Duration) *httpClient {
return &httpClient{
baseURL: strings.TrimRight(baseURL, "/"),
apiKey: apiKey,
client: &http.Client{Timeout: timeout},
}
}
func (h *httpClient) get(ctx context.Context, path string, result any) error {
return h.do(ctx, http.MethodGet, path, nil, result)
}
func (h *httpClient) post(ctx context.Context, path string, body, result any) error {
return h.do(ctx, http.MethodPost, path, body, result)
}
func (h *httpClient) delete(ctx context.Context, path string) error {
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, h.baseURL+path, nil)
if err != nil {
return err
}
h.setHeaders(req)
resp, err := h.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
return &Error{Code: "REQUEST_FAILED", Message: fmt.Sprintf("HTTP %d", resp.StatusCode), Status: resp.StatusCode}
}
return nil
}
func (h *httpClient) do(ctx context.Context, method, path string, body, result any) error {
var bodyReader io.Reader
if body != nil {
b, err := json.Marshal(body)
if err != nil {
return err
}
bodyReader = bytes.NewReader(b)
}
req, err := http.NewRequestWithContext(ctx, method, h.baseURL+path, bodyReader)
if err != nil {
return err
}
h.setHeaders(req)
resp, err := h.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
var envelope apiResponse
if err := json.NewDecoder(resp.Body).Decode(&envelope); err != nil {
return err
}
if !envelope.Success || envelope.Error != nil {
code := "UNKNOWN_ERROR"
msg := "unknown error"
if envelope.Error != nil {
code = envelope.Error.Code
msg = envelope.Error.Message
}
return &Error{Code: code, Message: msg, Status: resp.StatusCode}
}
if result != nil && envelope.Data != nil {
return json.Unmarshal(envelope.Data, result)
}
return nil
}
func (h *httpClient) setHeaders(req *http.Request) {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-API-Key", h.apiKey)
}