From 7073b3d47ac2427c0b5831c09041ca2cbd263a5c Mon Sep 17 00:00:00 2001 From: min4er <36953773+min4er@users.noreply.github.com> Date: Wed, 4 Jul 2018 18:05:43 +0300 Subject: [PATCH] Update bot.go + fix error in http.Header adding --- bot.go | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/bot.go b/bot.go index 572f240..7c782ff 100644 --- a/bot.go +++ b/bot.go @@ -60,35 +60,31 @@ func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) { // MakeRequest makes a request to a specific endpoint with our token. func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) { - if bot.Debug { - log.Printf("Endpoint: %s, values: %v\n", endpoint, params) - } - - method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint) - // Uncomment for adding ability to connect via proxy - /*proxyStr := "http://proxy.com:3128" - proxyURL, _ := url.Parse(proxyStr) + method := fmt.Sprintf(APIEndpoint, bot.Token, endpoint) + // Uncomment to use proxy with auth + /* + proxyStr := "http://proxy.com:3128" + proxyURL, _ := url.Parse(proxyStr) + + auth := "username:password" + basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) + + hdr := http.Header{} + hdr.Add("Proxy-Authorization", basicAuth) transport := &http.Transport{ Proxy: http.ProxyURL(proxyURL), + ProxyConnectHeader: hdr, } + bot.Client = &http.Client{ Transport: transport, } */ - //================== - - resp, err := bot.Client.PostForm(method, params) - - // Uncomment for adding auth params to header - /* - auth := "username:password" - basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth)) - resp.Header.Add("Proxy-Authorization", basicAuth) - */ - //================== + resp, err := bot.Client.PostForm(method, params) + if err != nil { return APIResponse{}, err } @@ -101,20 +97,15 @@ func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, } if bot.Debug { - log.Printf("Endpoint: %s, response: %s\n", endpoint, string(bytes)) + log.Printf("%s resp: %s", endpoint, bytes) } if !apiResp.Ok { - var parameters ResponseParameters - + parameters := ResponseParameters{} if apiResp.Parameters != nil { parameters = *apiResp.Parameters } - - return apiResp, Error{ - Message: apiResp.Description, - ResponseParameters: parameters, - } + return apiResp, Error{apiResp.Description, parameters} } return apiResp, nil