forked from yanzay/tbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
115 lines (102 loc) · 2.67 KB
/
api.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
package tbot
import (
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
)
type responseParameters struct {
MigrateToChatID int `json:"migrate_to_chat_id"`
ReplyAfter int `json:"retry_after"`
}
type apiResponse struct {
OK bool `json:"ok"`
Result json.RawMessage `json:"result"`
Description string `json:"description"`
ErrorCode int `json:"error_code"`
Parameters *responseParameters `json:"parameters"`
}
func (c *Client) doRequest(method string, request url.Values, response interface{}) error {
endpoint := fmt.Sprintf(c.url, method)
var resp *http.Response
var err error
if request == nil {
resp, err = c.httpClient.Post(endpoint, "application/x-www-form-urlencoded", nil)
} else {
resp, err = c.httpClient.PostForm(endpoint, request)
}
if err != nil {
return fmt.Errorf("unable to send message: %v", err)
}
apiResp := &apiResponse{}
err = json.NewDecoder(resp.Body).Decode(&apiResp)
if err != nil {
return fmt.Errorf("unable to decode sendMessage response: %v", err)
}
err = resp.Body.Close()
if err != nil {
c.logger.Errorf("unable to close response body: %v", err)
}
if !apiResp.OK {
return fmt.Errorf(apiResp.Description)
}
return json.Unmarshal(apiResp.Result, response)
}
func (c *Client) doRequestWithFiles(method string, request url.Values, response interface{}, files ...inputFile) error {
endpoint := fmt.Sprintf(c.url, method)
r, w := io.Pipe()
done := make(chan struct{})
var resp *http.Response
var err error
mw := multipart.NewWriter(w)
go func() {
defer close(done)
req, err := http.NewRequest(http.MethodPost, endpoint, r)
if err != nil {
c.logger.Error(err)
return
}
req.Header.Set("Content-Type", mw.FormDataContentType())
resp, err = c.httpClient.Do(req)
}()
for k := range request {
mw.WriteField(k, request.Get(k))
}
for _, file := range files {
f, err := os.Open(file.name)
if err != nil {
return err
}
fileWriter, err := mw.CreateFormFile(file.field, file.name)
if err != nil {
return err
}
io.Copy(fileWriter, f)
f.Close()
}
mw.Close()
w.Close()
<-done // post request is done
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %s", resp.Status)
}
apiResp := &apiResponse{}
err = json.NewDecoder(resp.Body).Decode(&apiResp)
if err != nil {
return fmt.Errorf("unable to decode sendMessage response: %v", err)
}
err = resp.Body.Close()
if err != nil {
c.logger.Errorf("unable to close response body: %v", err)
}
if !apiResp.OK {
return fmt.Errorf(apiResp.Description)
}
return json.Unmarshal(apiResp.Result, response)
}