2017-12-13 14:02:07 +00:00
|
|
|
package telegram
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-08-21 11:05:04 +00:00
|
|
|
"path"
|
|
|
|
"strconv"
|
2017-12-13 14:02:07 +00:00
|
|
|
|
|
|
|
log "github.com/kirillDanshin/dlog"
|
|
|
|
json "github.com/pquerna/ffjson/ffjson"
|
|
|
|
http "github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
2018-08-21 11:05:04 +00:00
|
|
|
func (bot *Bot) request(dst []byte, method string) (response *Response, err error) {
|
2018-05-31 13:16:20 +00:00
|
|
|
if bot.Client == nil {
|
|
|
|
bot.SetClient(defaultClient)
|
|
|
|
}
|
|
|
|
|
2018-04-19 10:56:25 +00:00
|
|
|
requestURI := defaultURI
|
2018-08-21 11:05:04 +00:00
|
|
|
requestURI.Path = path.Join("bot"+bot.AccessToken, method)
|
2017-12-13 14:02:07 +00:00
|
|
|
|
|
|
|
req := http.AcquireRequest()
|
|
|
|
defer http.ReleaseRequest(req)
|
|
|
|
req.Header.SetContentType("application/json; charset=utf-8")
|
|
|
|
req.Header.SetMethod("POST")
|
2018-04-19 10:56:25 +00:00
|
|
|
if dst == nil {
|
|
|
|
req.Header.SetMethod("GET")
|
|
|
|
}
|
2017-12-13 14:02:07 +00:00
|
|
|
req.Header.SetRequestURI(requestURI.String())
|
2018-08-21 11:05:04 +00:00
|
|
|
req.Header.SetUserAgent(path.Join("telegram", strconv.FormatInt(Version, 10)))
|
2018-02-15 23:28:49 +00:00
|
|
|
req.Header.SetHost(requestURI.Hostname())
|
2017-12-13 14:02:07 +00:00
|
|
|
req.SetBody(dst)
|
|
|
|
|
|
|
|
resp := http.AcquireResponse()
|
|
|
|
defer http.ReleaseResponse(resp)
|
2018-05-31 13:16:20 +00:00
|
|
|
|
2018-08-21 11:05:04 +00:00
|
|
|
err = bot.Client.Do(req, resp)
|
2018-02-15 12:32:47 +00:00
|
|
|
log.Ln("Request:")
|
|
|
|
log.D(req)
|
2018-01-29 09:52:33 +00:00
|
|
|
log.Ln("Response:")
|
|
|
|
log.D(resp)
|
|
|
|
if err != nil {
|
2018-08-21 11:05:04 +00:00
|
|
|
return
|
2017-12-13 14:02:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 11:05:04 +00:00
|
|
|
response = new(Response)
|
|
|
|
if err = json.Unmarshal(resp.Body(), response); err != nil {
|
|
|
|
return
|
2017-12-13 14:02:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 11:05:04 +00:00
|
|
|
if !response.Ok {
|
|
|
|
err = errors.New(response.Description)
|
2017-12-13 14:02:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-21 11:05:04 +00:00
|
|
|
return
|
2017-12-13 14:02:07 +00:00
|
|
|
}
|