Archived
1
This repository has been archived on 2022-11-04. You can view files and clone it, but cannot push or open issues or pull requests.
fwzookeeper/vendor/gitlab.com/toby3d/telegram/request.go

60 lines
1.3 KiB
Go
Raw Normal View History

2018-11-29 20:32:51 +04:00
package telegram
import (
"errors"
"path"
"strconv"
"github.com/kirillDanshin/dlog"
json "github.com/pquerna/ffjson/ffjson"
http "github.com/valyala/fasthttp"
)
var defaultClient = new(http.Client)
func (bot *Bot) request(dst []byte, method string) (response *Response, err error) {
if bot.Client == nil {
bot.SetClient(defaultClient)
}
requestURI := http.AcquireURI()
requestURI.SetScheme("https")
requestURI.SetHost("api.telegram.org")
requestURI.SetPath(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")
if dst == nil {
req.Header.SetMethod("GET")
}
req.Header.SetRequestURI(requestURI.String())
req.Header.SetUserAgent(path.Join("telegram", strconv.FormatInt(Version, 10)))
req.Header.SetHostBytes(requestURI.Host())
req.SetBody(dst)
resp := http.AcquireResponse()
defer http.ReleaseResponse(resp)
err = bot.Client.Do(req, resp)
dlog.Ln("Request:")
dlog.D(req)
dlog.Ln("Response:")
dlog.D(resp)
if err != nil {
return
}
response = new(Response)
if err = json.Unmarshal(resp.Body(), response); err != nil {
return
}
if !response.Ok {
err = errors.New(response.Description)
}
return
}