-
Notifications
You must be signed in to change notification settings - Fork 1
/
tgstat.go
238 lines (187 loc) · 4.46 KB
/
tgstat.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
package tgstat_go
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
)
const (
APIURL string = "https://api.tgstat.ru"
)
// APIs are the currently supported endpoints.
type APIs struct {
Api API
mu sync.RWMutex
}
var Token string
var apis APIs
type API interface {
NewRestRequest(ctx context.Context, token, method, urlPath string, data map[string]string) (*http.Request, error)
Do(r *http.Request, v interface{}) (*http.Response, error)
}
// Client is a client to TG Stat API
type Client struct {
Url string
httpClient *http.Client
}
var TGStatClient Client
// ClientOption is used to configure a Client.
type ClientOption func(*Client)
// WithEndpoint configures a Client to use the specified API endpoint.
func WithEndpoint(endpoint string) {
TGStatClient.Url = strings.TrimRight(endpoint, "/")
}
func (c *Client) NewRestRequest(ctx context.Context, token, method, urlPath string, data map[string]string) (*http.Request, error) {
return NewRestRequest(c, ctx, token, method, urlPath, data)
}
var NewRestRequest = func(c *Client, ctx context.Context, token, method, urlPath string, data map[string]string) (*http.Request, error) {
uri := APIURL + urlPath
if c == nil {
return nil, errors.New("client not configured")
}
if c.Url != "" {
uri = c.Url + urlPath
}
if data == nil {
return nil, errors.New("data is not initialised")
}
if token == "" {
return nil, errors.New("token not found")
}
data["token"] = token
reqBodyData, _ := json.Marshal(data)
// On `GET`, move the payload into the URL
if method == http.MethodGet {
//var body string
body := url.Values{}
for key, value := range data {
body.Add(key, value)
}
body.Add("token", token)
uri += "?" + body.Encode()
reqBodyData = nil
}
req, err := http.NewRequest(method, uri, bytes.NewReader(reqBodyData))
if err != nil {
return nil, err
}
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Content-Type", "application/json")
req = req.WithContext(ctx)
return req, nil
}
var reader = func(r io.Reader) ([]byte, error) {
return io.ReadAll(r)
}
// Do perform an HTTP request against the API.
func (c *Client) Do(r *http.Request, v interface{}) (*http.Response, error) {
resp, err := c.httpClient.Do(r)
if err != nil {
return nil, err
}
body, err := reader(resp.Body)
if err != nil {
resp.Body.Close()
return resp, err
}
resp.Body.Close()
resp.Body = io.NopCloser(bytes.NewReader(body))
err = errorFromResponse(resp, body)
if err != nil {
return resp, err
}
if resp.StatusCode >= 400 && resp.StatusCode <= 599 {
err = fmt.Errorf("tgstat server responded with status code %d", resp.StatusCode)
return resp, err
}
if v != nil {
if w, ok := v.(io.Writer); ok {
_, err = io.Copy(w, bytes.NewReader(body))
} else {
err = json.Unmarshal(body, v)
}
}
return resp, err
}
func errorFromResponse(resp *http.Response, body []byte) error {
if !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
return nil
}
var respBody ErrorResult
if err := json.Unmarshal(body, &respBody); err != nil {
return nil
}
if respBody.Error == "" || respBody.VerifyCode != "" {
return nil
}
return fmt.Errorf("%s",respBody.Error)
}
// NewClient creates a new client.
func newClient(uri string, options ...ClientOption) (*Client, error) {
if uri == "" {
return nil, errors.New("URL is empty")
}
if _, err := url.ParseRequestURI(uri); err != nil {
return nil, fmt.Errorf("unable to parse URL: %v", err)
}
client := &Client{
Url: uri,
httpClient: &http.Client{},
}
for _, option := range options {
option(client)
}
return client, nil
}
// newAPI creates a new client.
func newAPI(url string, options ...ClientOption) *Client {
client := &Client{
Url: url,
httpClient: &http.Client{},
}
for _, option := range options {
option(client)
}
return client
}
func GetAPI(options ...ClientOption) API {
var api API
apis.mu.RLock()
api = apis.Api
apis.mu.RUnlock()
if api != nil {
return api
}
if TGStatClient.Url == "" {
TGStatClient.Url = APIURL
}
return newAPI(TGStatClient.Url, options...)
}
func String(v string) *string {
if v == "" {
return nil
}
return &v
}
func Bool(b bool) *bool {
return &b
}
func Int(v int) *int {
return &v
}
func ValidateDate(date *string) error {
if date == nil {
return nil
}
if _, err := strconv.Atoi(*date); err != nil {
return fmt.Errorf("date: must be numeric")
}
return nil
}