-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
269 lines (230 loc) · 5.88 KB
/
client.go
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package foxesscloud
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
const (
baseURL = "https://www.foxesscloud.com"
)
type Config struct {
Client *http.Client
Token string
UserAgent string
}
func NewClient(cfg Config) (*Client, error) {
client := http.DefaultClient
if cfg.Client != nil {
client = cfg.Client
}
c := &Client{
client: client,
token: cfg.Token,
userAgent: cfg.UserAgent,
}
c.PowerStations = &PowerStationService{client: c}
c.Inverters = &InverterService{client: c}
return c, nil
}
type Client struct {
client *http.Client
token string
userAgent string
PowerStations *PowerStationService
Inverters *InverterService
}
const (
errNoNoError = 0
errNoTokenExpired = 41808
errNoTokenInvalid = 41809
errHeadersMissing = 40256
errBodyInvalid = 40257
errRequestsTooFrequent = 40400
errRateLimitExceeded = 40402
)
type RateLimitExceededError struct {
msg string
}
func (e *RateLimitExceededError) Error() string {
return fmt.Sprintf("rate limit exceeded: %s", e.msg)
}
type errorResponse struct {
ErrNo int `json:"errno"`
Msg string `json:"msg"`
}
func (c *Client) do(req *http.Request, res any) (*http.Response, error) {
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not read response body: %w", err)
}
var errResp errorResponse
if err := json.Unmarshal(data, &errResp); err != nil {
return nil, fmt.Errorf("could not unmarshal json response: %w", err)
}
switch errResp.ErrNo {
case errNoNoError:
break
case errNoTokenInvalid, errNoTokenExpired:
return nil, fmt.Errorf("invalid token: %v", errResp.Msg)
case errHeadersMissing:
return nil, fmt.Errorf("missing headers: %v", errResp.Msg)
case errBodyInvalid:
return nil, fmt.Errorf("invalid body: %v", errResp.Msg)
case errRequestsTooFrequent:
return nil, fmt.Errorf("rate limit exceeded: %v", errResp.Msg)
case errRateLimitExceeded:
return nil, &RateLimitExceededError{msg: errResp.Msg}
default:
return nil, fmt.Errorf("invalid response, got error code: %v, message: %v", errResp.ErrNo, errResp.Msg)
}
if err := json.Unmarshal(data, res); err != nil {
return nil, fmt.Errorf("could not unmarshal json response: %w", err)
}
return resp, nil
}
func (c *Client) newGetRequest(ctx context.Context, path string, qry url.Values) (*http.Request, error) {
return c.newRequest(ctx, http.MethodGet, path, qry, nil)
}
func (c *Client) newPostRequest(ctx context.Context, path string, pld any) (*http.Request, error) {
return c.newRequest(ctx, http.MethodPost, path, nil, pld)
}
func (c *Client) newRequest(ctx context.Context, method, path string, qry url.Values, pld any) (*http.Request, error) {
var body io.Reader
if pld != nil {
data, err := json.Marshal(pld)
if err != nil {
return nil, fmt.Errorf("could not marshal request body: %w", err)
}
body = bytes.NewReader(data)
}
req, err := http.NewRequestWithContext(ctx, method, buildPath(path, qry), body)
if err != nil {
return nil, err
}
now := time.Now()
for k, v := range c.buildHeaders(path, now) {
req.Header.Set(k, v)
}
return req, nil
}
func (c *Client) buildHeaders(path string, date time.Time) map[string]string {
timestamp := date.UnixNano() / int64(time.Millisecond)
headers := map[string]string{
"content-type": "application/json",
"lang": "en",
"timestamp": strconv.FormatInt(timestamp, 10),
"user-agent": c.userAgent,
"token": c.token,
"signature": buildSignature(c.token, path, timestamp),
}
return headers
}
func buildPath(path string, qry url.Values) string {
if len(qry) > 0 {
return baseURL + path + "?" + qry.Encode()
}
return baseURL + path
}
func buildSignature(token string, path string, timestamp int64) string {
var b strings.Builder
b.WriteString(path)
b.WriteString("\\r\\n")
b.WriteString(token)
b.WriteString("\\r\\n")
b.WriteString(strconv.FormatInt(timestamp, 10))
return hashMD5(b.String())
}
func hashMD5(text string) string {
hash := md5.New()
hash.Write([]byte(text))
return hex.EncodeToString(hash.Sum(nil))
}
type Pagination struct {
CurrentPage int `json:"currentPage"`
Pagesize int `json:"pageSize"`
}
type QueryTimestamp struct {
time.Time
}
func (t *QueryTimestamp) MarshalJSON() ([]byte, error) {
return []byte(strconv.FormatInt(t.UnixMilli(), 10)), nil
}
type DataTimestamp struct {
time.Time
}
func (t *DataTimestamp) UnmarshalJSON(b []byte) error {
d, err := time.Parse("2006-01-02 15:04:05 MST-0700", strings.Trim(string(b), "\""))
if err != nil {
return err
}
t.Time = d
return nil
}
type DataFloat struct {
Value float64
}
func (f *DataFloat) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
if s == "" {
return nil
}
v, err := strconv.ParseFloat(s, 64)
if err != nil {
return err
}
f.Value = v
return nil
}
type DataListResponse[T any] struct {
Items []T
}
type wrappedDataListResponse[T any] struct {
Result []T `json:"result"`
}
func (l *wrappedDataListResponse[T]) unwrap() *DataListResponse[T] {
return &DataListResponse[T]{
Items: l.Result,
}
}
type ListResponse[T any] struct {
Items []T
CurrentPage int
Pagesize int
Total int
}
type wrappedListResponse[T any] struct {
Result struct {
Data []T `json:"data"`
CurrentPage int `json:"currentPage"`
Pagesize int `json:"pageSize"`
Total int `json:"total"`
} `json:"result"`
}
func (l *wrappedListResponse[T]) unwrap() *ListResponse[T] {
return &ListResponse[T]{
Items: l.Result.Data,
CurrentPage: l.Result.CurrentPage,
Pagesize: l.Result.Pagesize,
Total: l.Result.Total,
}
}
type wrappedDetailResponse[T any] struct {
Result *T `json:"result"`
}
func (l *wrappedDetailResponse[T]) unwrap() *T {
return l.Result
}