1
0
telegram/request.go

56 lines
1.1 KiB
Go
Raw Normal View History

package telegram
import (
"errors"
2018-08-21 11:05:04 +00:00
"path"
"strconv"
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) {
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)
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")
}
req.Header.SetRequestURI(requestURI.String())
2018-08-21 11:05:04 +00:00
req.Header.SetUserAgent(path.Join("telegram", strconv.FormatInt(Version, 10)))
req.Header.SetHost(requestURI.Hostname())
req.SetBody(dst)
resp := http.AcquireResponse()
defer http.ReleaseResponse(resp)
2018-08-21 11:05:04 +00:00
err = bot.Client.Do(req, resp)
log.Ln("Request:")
log.D(req)
log.Ln("Response:")
log.D(resp)
if err != nil {
2018-08-21 11:05:04 +00:00
return
}
2018-08-21 11:05:04 +00:00
response = new(Response)
if err = json.Unmarshal(resp.Body(), response); err != nil {
return
}
2018-08-21 11:05:04 +00:00
if !response.Ok {
err = errors.New(response.Description)
}
2018-08-21 11:05:04 +00:00
return
}