feat: Replaces *http.Client with an interface
Signed-off-by: Andrii Soluk <isoluchok@gmail.com>bot-api-6.1
parent
5e339ed016
commit
6792fab6bb
16
bot.go
16
bot.go
|
@ -19,6 +19,10 @@ import (
|
||||||
"github.com/technoweenie/multipartstreamer"
|
"github.com/technoweenie/multipartstreamer"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type HttpClient interface {
|
||||||
|
Do(req *http.Request) (*http.Response, error)
|
||||||
|
}
|
||||||
|
|
||||||
// BotAPI allows you to interact with the Telegram Bot API.
|
// BotAPI allows you to interact with the Telegram Bot API.
|
||||||
type BotAPI struct {
|
type BotAPI struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
|
@ -26,7 +30,7 @@ type BotAPI struct {
|
||||||
Buffer int `json:"buffer"`
|
Buffer int `json:"buffer"`
|
||||||
|
|
||||||
Self User `json:"-"`
|
Self User `json:"-"`
|
||||||
Client *http.Client `json:"-"`
|
Client HttpClient `json:"-"`
|
||||||
shutdownChannel chan interface{}
|
shutdownChannel chan interface{}
|
||||||
|
|
||||||
apiEndpoint string
|
apiEndpoint string
|
||||||
|
@ -51,7 +55,7 @@ func NewBotAPIWithAPIEndpoint(token, apiEndpoint string) (*BotAPI, error) {
|
||||||
// and allows you to pass a http.Client.
|
// and allows you to pass a http.Client.
|
||||||
//
|
//
|
||||||
// It requires a token, provided by @BotFather on Telegram and API endpoint.
|
// 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{
|
bot := &BotAPI{
|
||||||
Token: token,
|
Token: token,
|
||||||
Client: client,
|
Client: client,
|
||||||
|
@ -79,7 +83,13 @@ func (b *BotAPI) SetAPIEndpoint(apiEndpoint string) {
|
||||||
func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) {
|
func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) {
|
||||||
method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint)
|
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 {
|
if err != nil {
|
||||||
return APIResponse{}, err
|
return APIResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue