-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
133 lines (110 loc) · 2.64 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
package tg
import (
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/pkg/errors"
)
// Request represents RPC request to Telegram Bot API.
// It contains following info: method, token, parameters, files.
type Request struct {
token string
method string
args map[string]string
files map[string]*InputFile
attachmentIdx int
}
// NewRequest creates request with provided method.
func NewRequest(method string) *Request {
return &Request{
method: method,
args: map[string]string{},
files: map[string]*InputFile{},
}
}
// Token returns request token.
func (r *Request) Token() string {
return r.token
}
// Method returns request method.
func (r *Request) Method() string {
return r.method
}
// SetToken sets request token.
func (r *Request) SetToken(token string) {
r.token = token
}
// SetString sets request string argument.
func (r *Request) SetString(k, v string) {
r.args[k] = v
}
// SetOptString sets request string argument, if value is not zero.
func (r *Request) SetOptString(k, v string) {
if v != "" {
r.args[k] = v
}
}
// hasInputFiles returns true if request contains files for upload.
func (r *Request) hasInputFiles() bool {
return len(r.files) > 0
}
// SetInputFile add file to request
func (r *Request) SetInputFile(k string, file *InputFile) (addr string) {
name := fmt.Sprintf("attachment_%d", r.attachmentIdx)
addr = "attach://" + name
if k != "" {
r.SetString(k, addr)
}
r.files[name] = file
r.attachmentIdx++
return addr
}
// SetJSON value k.
func (r *Request) SetJSON(k string, v interface{}) error {
data, err := json.Marshal(v)
if err != nil {
return errors.Wrap(err, "marshal v")
}
r.args[k] = string(data)
return nil
}
// SetOptJSON value k.
func (r *Request) SetOptJSON(k string, v interface{}) error {
if v != nil {
return r.SetJSON(k, v)
}
return nil
}
// SetInt sets request int argument k to value v.
func (r *Request) SetInt(k string, v int) {
r.args[k] = strconv.Itoa(v)
}
// SetFloat sets request float argument k to value v.
func (r *Request) SetFloat64(k string, v float64) {
r.args[k] = strconv.FormatFloat(v, 'f', -1, 64)
}
// SetOptInt sets request int argument k to value v, if v is not zero.
func (r *Request) SetOptInt(k string, v int) {
if v != 0 {
r.args[k] = strconv.Itoa(v)
}
}
func (r *Request) SetOptInt64(k string, v int64) {
if v != 0 {
r.args[k] = strconv.FormatInt(v, 10)
}
}
func (r *Request) SetOptDuration(k string, v time.Duration) {
if v != 0 {
r.args[k] = strconv.Itoa(int(v.Seconds()))
}
}
func (r *Request) SetPeer(k string, v Peer) {
r.args[k] = v.Peer()
}
func (r *Request) SetOptBool(k string, v bool) {
if v {
r.args[k] = "true"
}
}