-
Notifications
You must be signed in to change notification settings - Fork 3
/
making-requests.go
134 lines (109 loc) · 2.7 KB
/
making-requests.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
package telegram
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
)
// Response general interface
type Response interface {
IsOK() bool
GetErrorCode() int
GetDescription() string
GetParameters() *ResponseParameters
}
type response struct {
OK bool `json:"ok"`
Description *string `json:"description,omitempty"`
ErrorCode *int `json:"error_code,omitempty"`
Parameters *ResponseParameters `json:"parameters,omitempty"`
}
func (r *response) IsOK() bool {
return r.OK
}
func (r *response) GetErrorCode() int {
if r.ErrorCode != nil {
return *r.ErrorCode
}
return 0
}
func (r *response) GetDescription() string {
if r.Description != nil {
return *r.Description
}
return ""
}
func (r *response) GetParameters() *ResponseParameters {
return r.Parameters
}
type request struct {
params map[string]interface{}
}
func (b *bot) doRequest(ctx context.Context, methodName string, res interface{}, options ...MethodOption) error {
r := request{
params: map[string]interface{}{},
}
for i := range options {
options[i](&r)
}
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for k, v := range r.params {
switch t := v.(type) {
case io.Reader:
w, err := writer.CreateFormFile(k, k)
if err != nil {
return fmt.Errorf("error on create field from file: %w", err)
}
b, err := ioutil.ReadAll(t)
if err != nil {
return fmt.Errorf("error on read: %w", err)
}
_, err = w.Write(b)
if err != nil {
return fmt.Errorf("error on write data: %w", err)
}
case string:
err := writer.WriteField(k, t)
if err != nil {
return fmt.Errorf("error on write field: %w", err)
}
default:
b, err := json.Marshal(v)
if err != nil {
return fmt.Errorf("error on marshal json: %w", err)
}
err = writer.WriteField(k, string(b))
if err != nil {
return fmt.Errorf("error on write field: %w", err)
}
}
}
err := writer.Close()
if err != nil {
return fmt.Errorf("error on close writer: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/bot%s/%s", b.baseURL, b.token, methodName), body)
if err != nil {
return fmt.Errorf("error on new http request: %w", err)
}
// don't send content type on empty body
if len(r.params) > 0 {
req.Header.Set("Content-Type", writer.FormDataContentType())
}
client := new(http.Client)
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error on do http request: %w", err)
}
defer func() { _ = resp.Body.Close() }()
err = json.NewDecoder(resp.Body).Decode(res)
if err != nil {
return fmt.Errorf("error decode json response: %w", err)
}
return nil
}