-
Notifications
You must be signed in to change notification settings - Fork 11
/
request.go
189 lines (146 loc) · 3.81 KB
/
request.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
package tg
import (
"encoding/json"
"fmt"
"strconv"
"golang.org/x/exp/maps"
)
// Request is Telegram Bot API request structure.
type Request struct {
Method string
json map[string]any
args map[string]string
files map[string]InputFile
attachmentIdx int
}
func NewRequest(method string) *Request {
return &Request{
Method: method,
json: make(map[string]any),
args: make(map[string]string),
files: make(map[string]InputFile),
}
}
func (r *Request) JSON(name string, v any) *Request {
r.json[name] = v
return r
}
func (r *Request) InputFile(name string, file InputFile) *Request {
r.files[name] = file
return r
}
func (r *Request) PeerID(name string, v PeerID) *Request {
return r.String(name, v.PeerID())
}
func (r *Request) UserID(name string, v UserID) *Request {
return r.PeerID(name, v)
}
func (r *Request) String(name, value string) *Request {
r.args[name] = value
return r
}
func (r *Request) Bool(name string, value bool) *Request {
return r.String(name, strconv.FormatBool(value))
}
func (r *Request) Int(name string, value int) *Request {
return r.String(name, strconv.Itoa(value))
}
func (r *Request) Int64(name string, value int64) *Request {
return r.String(name, strconv.FormatInt(value, 10))
}
func (r *Request) Float64(name string, value float64) *Request {
return r.String(name, strconv.FormatFloat(value, 'f', -1, 64))
}
func (r *Request) ChatID(name string, v ChatID) *Request {
return r.Int64(name, int64(v))
}
func (r *Request) FileID(name string, v FileID) *Request {
return r.String(name, string(v))
}
func (r *Request) File(name string, arg FileArg) *Request {
if arg.isRef() {
return r.String(name, arg.getRef())
}
return r.InputFile(name, arg.Upload)
}
func (r *Request) Has(name string) bool {
_, inJSON := r.json[name]
_, inArgs := r.args[name]
_, inFiles := r.files[name]
return inJSON || inArgs || inFiles
}
func (r *Request) GetArg(name string) (string, bool) {
v, ok := r.args[name]
return v, ok
}
func (r *Request) GetJSON(name string) (any, bool) {
v, ok := r.json[name]
return v, ok
}
func (r *Request) InputMediaSlice(name string, im []InputMedia) *Request {
for _, v := range im {
r.InputMedia(v)
}
r.JSON(name, im)
return r
}
func (r *Request) InputMedia(im InputMedia) *Request {
media, thumb := im.getMedia()
id := fmt.Sprintf("attachment_%d", r.attachmentIdx)
addr := fmt.Sprintf("attach://%s", id)
if media.getRef() == "" {
r.InputFile(id, media.Upload)
media.addr = addr
r.attachmentIdx++
}
if thumb != nil {
thumbID := id + "_thumb"
r.InputFile(thumbID, *thumb)
thumb.addr = fmt.Sprintf("attach://%s", thumbID)
}
return r
}
func (r *Request) Stringer(name string, v fmt.Stringer) *Request {
return r.String(name, v.String())
}
func (r *Request) jsonToArgs() error {
for k, jn := range r.json {
v, err := json.Marshal(jn)
if err != nil {
return fmt.Errorf("failed to marshal %s: %w", k, err)
}
r.args[k] = string(v)
}
return nil
}
// Encode request using encoder.
func (r *Request) Encode(encoder Encoder) error {
if err := r.jsonToArgs(); err != nil {
return fmt.Errorf("encode json to args: %w", err)
}
// add files
for k, v := range r.files {
if err := encoder.WriteFile(k, v); err != nil {
return fmt.Errorf("encode file %s: %w", k, err)
}
}
// add arguments
for k, v := range r.args {
if err := encoder.WriteString(k, v); err != nil {
return fmt.Errorf("encode argument %s: %w", k, err)
}
}
return nil
}
func (req *Request) MarshalJSON() ([]byte, error) {
if err := req.jsonToArgs(); err != nil {
return nil, fmt.Errorf("marshal json to args: %w", err)
}
if len(req.files) > 0 {
return nil, fmt.Errorf("files are not supported in JSON requests")
}
args := make(map[string]string, len(req.args)+1)
args["method"] = req.Method
maps.Copy(args, req.args)
return json.Marshal(args)
}