Commit 4c6bbdac authored by Faizal Aziz's avatar Faizal Aziz

adding post with proxy

parent 6d822213
......@@ -46,6 +46,7 @@ type (
Post(ctx *context.UlfsaarContext, path string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error)
PostFormData(ctx *context.UlfsaarContext, path string, headers http.Header, payload map[string]string) (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)
PostWithProxy(ctx *context.UlfsaarContext, path, ip, port, username, password 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)
GetWithProxy(ctx *context.UlfsaarContext, path, ip, port string, headers http.Header) (respHeader http.Header, statusCode int, body []byte, err error)
......@@ -148,6 +149,52 @@ func (c *client) SetTimeout(timeout time.Duration) {
return
}
func (c *client) PostWithProxy(ctx *context.UlfsaarContext, path, ip, port, username, password string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error) {
uri := c.options.Address + path
startTime := time.Now()
var stringProxUrl string
proxUrl, err := url.Parse(fmt.Sprintf("http://%s:%s@%s:%s", username, password, ip, port))
if err != nil {
stringProxUrl = fmt.Sprintf("http://%s:%s@%s:%s", username, password, ip, port)
} else {
stringProxUrl = proxUrl.String()
}
request := c.httpClient.SetProxy(stringProxUrl).R()
request = request.SetBody(payload)
for h, val := range headers {
request.Header[h] = val
}
if headers[ContentType] == nil {
request.Header.Set(ContentType, ApplicationJSON)
}
request.Header.Set(UserAgent, UserAgentValue)
httpResp, httpErr := request.Post(uri)
if httpResp != nil {
body = httpResp.Body()
respHeader = httpResp.Header()
statusCode = httpResp.StatusCode()
}
ctx.Info("Post",
logger.ToField(urlKey, uri),
logger.ToField(requestKey, toRequest(request.Header, payload)),
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) Post(ctx *context.UlfsaarContext, path string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error) {
url := c.options.Address + path
startTime := time.Now()
......
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