Commit 2027c978 authored by Faizal Aziz's avatar Faizal Aziz

adding get with proxy to resty client

parent 4da79a81
......@@ -3,6 +3,8 @@ package http_client
import (
"bytes"
"crypto/tls"
"encoding/base64"
"fmt"
"github.com/go-resty/resty/v2"
"gitlab.ursabyte.com/faizal.aziz/ulfssar-go/pkg/context"
"gitlab.ursabyte.com/faizal.aziz/ulfssar-go/pkg/logger"
......@@ -46,6 +48,7 @@ type (
PostMultipartFormFilesAndData(ctx *context.UlfsaarContext, path string, headers http.Header, formData []*MultipartField, formFiles []MultipartFileRequest) (respHeader http.Header, statusCode int, body []byte, err error)
Put(ctx *context.UlfsaarContext, path string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error)
Get(ctx *context.UlfsaarContext, path string, headers http.Header) (respHeader http.Header, statusCode int, body []byte, err error)
GetWithProxy(ctx *context.UlfsaarContext, path, ip, port, username, password string, headers http.Header) (respHeader http.Header, statusCode int, body []byte, err error)
GetWithQueryParam(ctx *context.UlfsaarContext, path string, headers http.Header, queryParam map[string]string) (respHeader http.Header, statusCode int, body []byte, err error)
Delete(ctx *context.UlfsaarContext, path string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error)
Patch(ctx *context.UlfsaarContext, path string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error)
......@@ -57,6 +60,40 @@ type (
}
)
func (c *client) GetWithProxy(ctx *context.UlfsaarContext, path, ip, port, username, password string, headers http.Header) (respHeader http.Header, statusCode int, body []byte, err error) {
url := c.options.Address + path
startTime := time.Now()
request := c.httpClient.SetProxy(fmt.Sprintf("%s:%s", ip, port)).R()
for h, val := range headers {
request.Header[h] = val
}
request.Header.Set(UserAgent, UserAgentValue)
request.Header.Set("Proxy-Authorization", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password))))
httpResp, httpErr := request.Get(url)
if httpResp != nil {
body = httpResp.Body()
respHeader = httpResp.Header()
statusCode = httpResp.StatusCode()
}
ctx.Info("Get",
logger.ToField(urlKey, url),
logger.ToField(requestKey, toRequest(request.Header, nil)),
logger.ToField(responseKey, toResponse(statusCode, respHeader, body)),
logger.ToField(startProcessingTimeKey, startProcessingTime(startTime)),
logger.ToField(processingTimeKey, processingTime(startTime)),
)
if statusCode == http.StatusOK {
return respHeader, statusCode, body, nil
}
return respHeader, statusCode, body, httpErr
}
func (c *client) SetTimeout(timeout time.Duration) {
c.httpClient.SetTimeout(timeout)
return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment