forked from max-messenger/max-bot-api-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
152 lines (127 loc) · 3.15 KB
/
client.go
File metadata and controls
152 lines (127 loc) · 3.15 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
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
package maxbot
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
jsoniter "github.com/json-iterator/go"
"github.com/max-messenger/max-bot-api-client-go/schemes"
)
type HttpClient interface {
Do(req *http.Request) (*http.Response, error)
}
type client struct {
key string
version string
baseURL *url.URL
httpClient HttpClient
errors chan error
}
func newClient(key string, version string, baseURL *url.URL, httpClient HttpClient) *client {
if httpClient == nil {
httpClient = &http.Client{
Timeout: defaultTimeout,
}
}
return &client{
key: key,
version: version,
baseURL: baseURL,
httpClient: httpClient,
errors: make(chan error, 1),
}
}
func (cl *client) notifyError(err error) {
if err == nil {
return
}
select {
case cl.errors <- err:
default:
log.Println(err)
}
}
func (cl *client) closer(name string, c io.Closer) {
if c == nil {
return
}
if err := c.Close(); err != nil {
cl.notifyError(fmt.Errorf("failed to close %s: %w", name, err))
}
}
func (cl *client) createTimeoutError(op string, reason string) *TimeoutError {
return &TimeoutError{
Op: op,
Reason: reason,
}
}
func (cl *client) request(ctx context.Context, method, path string, query url.Values, reset bool, body interface{}) (io.ReadCloser, error) {
if body == nil {
return cl.requestReader(ctx, method, path, query, reset, nil)
}
data, err := jsoniter.Marshal(body)
if err != nil {
return nil, &SerializationError{
Op: "marshal",
Type: "request body",
Err: err,
}
}
return cl.requestReader(ctx, method, path, query, reset, bytes.NewReader(data))
}
func (cl *client) requestReader(ctx context.Context, method, path string, query url.Values, reset bool, body io.Reader) (io.ReadCloser, error) {
if query == nil {
query = url.Values{}
}
u := *cl.baseURL
u.Path = path
query.Set(paramVersion, cl.version)
u.RawQuery = query.Encode()
req, err := http.NewRequestWithContext(ctx, method, u.String(), body)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("User-Agent", fmt.Sprintf("max-bot-api-client-go/%s", cl.version))
if !reset {
req.Header.Set("Authorization", cl.key)
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
resp, err := cl.do(req)
if err != nil {
var urlErr *url.Error
if errors.As(err, &urlErr) {
if urlErr.Timeout() {
return nil, cl.createTimeoutError(
fmt.Sprintf("%s %s", method, path),
"request timeout exceeded",
)
}
}
return nil, &NetworkError{
Op: fmt.Sprintf("%s %s", method, path),
Err: err,
}
}
if resp.StatusCode != http.StatusOK {
defer cl.closer("requestReader body", resp.Body)
apiErr := &schemes.Error{}
if decodeErr := jsoniter.NewDecoder(resp.Body).Decode(apiErr); decodeErr != nil {
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
return nil, &APIError{
Code: resp.StatusCode,
Message: apiErr.Code,
Details: apiErr.Message,
}
}
return resp.Body, nil
}
func (cl *client) do(req *http.Request) (*http.Response, error) {
return cl.httpClient.Do(req)
}