Allow to change API endpoint for bot

bot-api-6.1
zhuharev 2019-05-24 13:14:48 +03:00
parent 87e7035a90
commit 8d345836d6
1 changed files with 16 additions and 8 deletions

12
bot.go
View File

@ -28,6 +28,8 @@ type BotAPI struct {
Self User `json:"-"` Self User `json:"-"`
Client *http.Client `json:"-"` Client *http.Client `json:"-"`
shutdownChannel chan interface{} shutdownChannel chan interface{}
apiEndpoint string
} }
// NewBotAPI creates a new BotAPI instance. // NewBotAPI creates a new BotAPI instance.
@ -47,6 +49,8 @@ func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) {
Client: client, Client: client,
Buffer: 100, Buffer: 100,
shutdownChannel: make(chan interface{}), shutdownChannel: make(chan interface{}),
apiEndpoint: APIEndpoint,
} }
self, err := bot.GetMe() self, err := bot.GetMe()
@ -59,9 +63,13 @@ func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) {
return bot, nil return bot, nil
} }
func (b *BotAPI) SetAPIEndpoint(apiEndpoint string) {
b.apiEndpoint = apiEndpoint
}
// MakeRequest makes a request to a specific endpoint with our token. // MakeRequest makes a request to a specific endpoint with our token.
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(APIEndpoint, bot.Token, endpoint) method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint)
resp, err := bot.Client.PostForm(method, params) resp, err := bot.Client.PostForm(method, params)
if err != nil { if err != nil {
@ -186,7 +194,7 @@ func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldna
return APIResponse{}, errors.New(ErrBadFileType) return APIResponse{}, errors.New(ErrBadFileType)
} }
method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint) method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint)
req, err := http.NewRequest("POST", method, nil) req, err := http.NewRequest("POST", method, nil)
if err != nil { if err != nil {