-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
124 lines (106 loc) · 2.69 KB
/
Copy pathhttp.go
File metadata and controls
124 lines (106 loc) · 2.69 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
124
package devicebase
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
const defaultBaseURL = "https://api.devicebase.cn"
type httpClient struct {
baseURL string
apiKey string
httpClient *http.Client
}
func newHTTPClient(baseURL, apiKey string, hc *http.Client) *httpClient {
if hc == nil {
hc = &http.Client{}
}
return &httpClient{
baseURL: strings.TrimRight(baseURL, "/"),
apiKey: apiKey,
httpClient: hc,
}
}
func (c *httpClient) doJSON(method, path string, body any) (map[string]any, error) {
var bodyReader io.Reader
if body != nil {
data, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("marshal request body: %w", err)
}
bodyReader = bytes.NewReader(data)
}
req, err := http.NewRequest(method, c.baseURL+path, bodyReader)
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
c.setHeaders(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("execute request: %w", err)
}
defer resp.Body.Close()
if err := handleError(resp); err != nil {
return nil, err
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("read response: %w", err)
}
if len(bodyBytes) == 0 {
return map[string]any{}, nil
}
var result map[string]any
if err := json.Unmarshal(bodyBytes, &result); err != nil {
return nil, fmt.Errorf("decode response: %w", err)
}
return result, nil
}
func (c *httpClient) doRaw(method, path string) ([]byte, error) {
req, err := http.NewRequest(method, c.baseURL+path, nil)
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
c.setHeaders(req)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("execute request: %w", err)
}
defer resp.Body.Close()
if err := handleError(resp); err != nil {
return nil, err
}
return io.ReadAll(resp.Body)
}
func (c *httpClient) setHeaders(req *http.Request) {
req.Header.Set("Authorization", "Bearer "+c.apiKey)
req.Header.Set("Content-Type", "application/json")
}
func handleError(resp *http.Response) error {
if resp.StatusCode < 400 {
return nil
}
body, _ := io.ReadAll(resp.Body)
bodyStr := string(body)
switch resp.StatusCode {
case http.StatusUnauthorized:
return &AuthenticationError{
Message: "authentication failed - invalid API key",
StatusCode: resp.StatusCode,
}
case http.StatusNotFound:
return &DeviceNotFoundError{
Message: "device not found or not connected",
StatusCode: resp.StatusCode,
}
case http.StatusUnprocessableEntity:
return &ValidationError{
Message: fmt.Sprintf("validation error: %s", bodyStr),
StatusCode: resp.StatusCode,
}
default:
return newError(resp.StatusCode, bodyStr)
}
}