Commit 0cea26ba authored by Faizal Aziz's avatar Faizal Aziz

adding get with proxy with auth and without and making sure url valid in client

parent 0acac592
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"gitlab.ursabyte.com/faizal.aziz/ulfssar-go/pkg/context" "gitlab.ursabyte.com/faizal.aziz/ulfssar-go/pkg/context"
"gitlab.ursabyte.com/faizal.aziz/ulfssar-go/pkg/logger" "gitlab.ursabyte.com/faizal.aziz/ulfssar-go/pkg/logger"
"net/http" "net/http"
"net/url"
"time" "time"
) )
...@@ -48,7 +49,8 @@ type ( ...@@ -48,7 +49,8 @@ type (
PostMultipartFormFilesAndData(ctx *context.UlfsaarContext, path string, headers http.Header, formData []*MultipartField, formFiles []MultipartFileRequest) (respHeader http.Header, statusCode int, body []byte, err error) 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) 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) 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) GetWithProxy(ctx *context.UlfsaarContext, path, ip, port string, headers http.Header) (respHeader http.Header, statusCode int, body []byte, err error)
GetWithProxyAuth(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) 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) 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) Patch(ctx *context.UlfsaarContext, path string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error)
...@@ -60,18 +62,63 @@ type ( ...@@ -60,18 +62,63 @@ 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) { func (c *client) GetWithProxy(ctx *context.UlfsaarContext, path, ip, port string, headers http.Header) (respHeader http.Header, statusCode int, body []byte, err error) {
url := c.options.Address + path urls := c.options.Address + path
startTime := time.Now() startTime := time.Now()
var stringProxUrl string
proxUrl, err := url.Parse(fmt.Sprintf("%s:%s", ip, port))
stringProxUrl = proxUrl.String()
if err != nil {
stringProxUrl = fmt.Sprintf("%s:%s", ip, port)
}
request := c.httpClient.SetProxy(fmt.Sprintf("%s:%s", ip, port)).R() request := c.httpClient.SetProxy(stringProxUrl).R()
for h, val := range headers {
request.Header[h] = val
}
request.Header.Set(UserAgent, UserAgentValue)
httpResp, httpErr := request.Get(urls)
if httpResp != nil {
body = httpResp.Body()
respHeader = httpResp.Header()
statusCode = httpResp.StatusCode()
}
ctx.Info("Get",
logger.ToField(urlKey, urls),
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) GetWithProxyAuth(ctx *context.UlfsaarContext, path, ip, port, username, password string, headers http.Header) (respHeader http.Header, statusCode int, body []byte, err error) {
urls := c.options.Address + path
startTime := time.Now()
var stringProxUrl string
proxUrl, err := url.Parse(fmt.Sprintf("%s:%s", ip, port))
stringProxUrl = proxUrl.String()
if err != nil {
stringProxUrl = fmt.Sprintf("%s:%s", ip, port)
}
request := c.httpClient.SetProxy(stringProxUrl).R()
for h, val := range headers { for h, val := range headers {
request.Header[h] = val request.Header[h] = val
} }
request.Header.Set(UserAgent, UserAgentValue) request.Header.Set(UserAgent, UserAgentValue)
request.Header.Set("Proxy-Authorization", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))) request.Header.Set("Proxy-Authorization", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password))))
httpResp, httpErr := request.Get(url) httpResp, httpErr := request.Get(urls)
if httpResp != nil { if httpResp != nil {
body = httpResp.Body() body = httpResp.Body()
...@@ -80,7 +127,7 @@ func (c *client) GetWithProxy(ctx *context.UlfsaarContext, path, ip, port, usern ...@@ -80,7 +127,7 @@ func (c *client) GetWithProxy(ctx *context.UlfsaarContext, path, ip, port, usern
} }
ctx.Info("Get", ctx.Info("Get",
logger.ToField(urlKey, url), logger.ToField(urlKey, urls),
logger.ToField(requestKey, toRequest(request.Header, nil)), logger.ToField(requestKey, toRequest(request.Header, nil)),
logger.ToField(responseKey, toResponse(statusCode, respHeader, body)), logger.ToField(responseKey, toResponse(statusCode, respHeader, body)),
logger.ToField(startProcessingTimeKey, startProcessingTime(startTime)), logger.ToField(startProcessingTimeKey, startProcessingTime(startTime)),
......
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