From 6792fab6bb9bbeda26f6ef0573946e15fc4b33af Mon Sep 17 00:00:00 2001 From: Andrii Soluk Date: Sun, 24 May 2020 12:54:43 +0300 Subject: [PATCH] feat: Replaces *http.Client with an interface Signed-off-by: Andrii Soluk --- bot.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/bot.go b/bot.go index 5f25979..e6c2cc8 100644 --- a/bot.go +++ b/bot.go @@ -19,14 +19,18 @@ import ( "github.com/technoweenie/multipartstreamer" ) +type HttpClient interface { + Do(req *http.Request) (*http.Response, error) +} + // BotAPI allows you to interact with the Telegram Bot API. type BotAPI struct { Token string `json:"token"` Debug bool `json:"debug"` Buffer int `json:"buffer"` - Self User `json:"-"` - Client *http.Client `json:"-"` + Self User `json:"-"` + Client HttpClient `json:"-"` shutdownChannel chan interface{} apiEndpoint string @@ -51,7 +55,7 @@ func NewBotAPIWithAPIEndpoint(token, apiEndpoint string) (*BotAPI, error) { // and allows you to pass a http.Client. // // It requires a token, provided by @BotFather on Telegram and API endpoint. -func NewBotAPIWithClient(token, apiEndpoint string, client *http.Client) (*BotAPI, error) { +func NewBotAPIWithClient(token, apiEndpoint string, client HttpClient) (*BotAPI, error) { bot := &BotAPI{ Token: token, Client: client, @@ -79,7 +83,13 @@ func (b *BotAPI) SetAPIEndpoint(apiEndpoint string) { func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) { method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint) - resp, err := bot.Client.PostForm(method, params) + req, err := http.NewRequest("POST", method, strings.NewReader(params.Encode())) + if err != nil { + return APIResponse{}, err + } + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + resp, err := bot.Client.Do(req) if err != nil { return APIResponse{}, err }