Commit 3f8fd14e authored by Administrator's avatar Administrator

Update client.go

parent 8c00a559
...@@ -45,7 +45,7 @@ type ( ...@@ -45,7 +45,7 @@ type (
SetTimeout(timeout time.Duration) SetTimeout(timeout time.Duration)
Post(ctx *context.UlfsaarContext, path string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error) 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) PostFormData(ctx *context.UlfsaarContext, path string, headers http.Header, payload map[string]string) (respHeader http.Header, statusCode int, body []byte, err error)
PostFormDataWithProxy(ctx *context.UlfsaarContext, path, ip, port, username, password string, headers http.Header, payload map[string]string) (respHeader http.Header, statusCode int, body []byte, err error) PostFormDataWithProxy(ctx *context.UlfsaarContext, path, ip, port, username, password 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) 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) 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) Put(ctx *context.UlfsaarContext, path string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error)
...@@ -63,8 +63,8 @@ type ( ...@@ -63,8 +63,8 @@ type (
} }
) )
func (c *client) GetWithProxy(ctx *context.UlfsaarContext, path, ip, port string, headers http.Header) (respHeader http.Header, statusCode int, body []byte, err error) { func (c *client) GetWithProxy(ctx *context.UlfsaarContext, urls, ip, port string, headers http.Header) (respHeader http.Header, statusCode int, body []byte, err error) {
urls := c.options.Address + path //urls := c.options.Address + path
startTime := time.Now() startTime := time.Now()
var stringProxUrl string var stringProxUrl string
proxUrl, err := url.Parse(fmt.Sprintf("http://%s:%s", ip, port)) proxUrl, err := url.Parse(fmt.Sprintf("http://%s:%s", ip, port))
...@@ -102,8 +102,8 @@ func (c *client) GetWithProxy(ctx *context.UlfsaarContext, path, ip, port string ...@@ -102,8 +102,8 @@ func (c *client) GetWithProxy(ctx *context.UlfsaarContext, path, ip, port string
return respHeader, statusCode, body, httpErr 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) { func (c *client) GetWithProxyAuth(ctx *context.UlfsaarContext, urls, ip, port, username, password string, headers http.Header) (respHeader http.Header, statusCode int, body []byte, err error) {
urls := c.options.Address + path //urls := c.options.Address + path
startTime := time.Now() startTime := time.Now()
var stringProxUrl string var stringProxUrl string
proxUrl, err := url.Parse(fmt.Sprintf("http://%s:%s@%s:%s", username, password, ip, port)) proxUrl, err := url.Parse(fmt.Sprintf("http://%s:%s@%s:%s", username, password, ip, port))
...@@ -150,8 +150,8 @@ func (c *client) SetTimeout(timeout time.Duration) { ...@@ -150,8 +150,8 @@ func (c *client) SetTimeout(timeout time.Duration) {
return 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) { func (c *client) PostWithProxy(ctx *context.UlfsaarContext, uri, ip, port, username, password string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error) {
uri := c.options.Address + path //uri := c.options.Address + path
startTime := time.Now() startTime := time.Now()
var stringProxUrl string var stringProxUrl string
...@@ -196,8 +196,8 @@ func (c *client) PostWithProxy(ctx *context.UlfsaarContext, path, ip, port, user ...@@ -196,8 +196,8 @@ func (c *client) PostWithProxy(ctx *context.UlfsaarContext, path, ip, port, user
return respHeader, statusCode, body, httpErr 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) { func (c *client) Post(ctx *context.UlfsaarContext, url string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error) {
url := c.options.Address + path //url := c.options.Address + path
startTime := time.Now() startTime := time.Now()
request := c.httpClient.R() request := c.httpClient.R()
...@@ -234,22 +234,29 @@ func (c *client) Post(ctx *context.UlfsaarContext, path string, headers http.Hea ...@@ -234,22 +234,29 @@ func (c *client) Post(ctx *context.UlfsaarContext, path string, headers http.Hea
return respHeader, statusCode, body, httpErr return respHeader, statusCode, body, httpErr
} }
func (c *client) PostFormData(ctx *context.UlfsaarContext, path string, headers http.Header, payload map[string]string) (respHeader http.Header, statusCode int, body []byte, err error) { func (c *client) PostFormDataWithProxy(ctx *context.UlfsaarContext, Url, ip, port, username, password string, headers http.Header, payload map[string]string) (respHeader http.Header, statusCode int, body []byte, err error) {
url := c.options.Address + path //Url := c.options.Address + path
startTime := time.Now() startTime := time.Now()
var stringProxUrl string
proxUrl, err := url.Parse(fmt.Sprintf("https://%s:%s@%s:%s", username, password, ip, port))
if err != nil {
stringProxUrl = fmt.Sprintf("https://%s:%s@%s:%s", username, password, ip, port)
} else {
stringProxUrl = proxUrl.String()
}
request := c.httpClient.R() request := c.httpClient.SetProxy(stringProxUrl).R()
request.SetFormData(payload)
for h, val := range headers { for h, val := range headers {
request.Header[h] = val request.Header[h] = val
} }
request.SetFormData(payload)
if headers[ContentType] == nil { if headers[ContentType] == nil {
request.Header.Set(ContentType, ApplicationJSON) request.Header.Set(ContentType, ApplicationJSON)
} }
request.Header.Set(UserAgent, UserAgentValue)
httpResp, httpErr := request.Post(url) httpResp, httpErr := request.Post(Url)
if httpResp != nil { if httpResp != nil {
body = httpResp.Body() body = httpResp.Body()
...@@ -258,7 +265,7 @@ func (c *client) PostFormData(ctx *context.UlfsaarContext, path string, headers ...@@ -258,7 +265,7 @@ func (c *client) PostFormData(ctx *context.UlfsaarContext, path string, headers
} }
ctx.Info("PostFormData", ctx.Info("PostFormData",
logger.ToField(urlKey, url), logger.ToField(urlKey, Url),
logger.ToField(requestKey, toRequest(request.Header, payload)), logger.ToField(requestKey, toRequest(request.Header, payload)),
logger.ToField(responseKey, toResponse(statusCode, respHeader, body)), logger.ToField(responseKey, toResponse(statusCode, respHeader, body)),
logger.ToField(startProcessingTimeKey, startProcessingTime(startTime)), logger.ToField(startProcessingTimeKey, startProcessingTime(startTime)),
...@@ -272,29 +279,22 @@ func (c *client) PostFormData(ctx *context.UlfsaarContext, path string, headers ...@@ -272,29 +279,22 @@ func (c *client) PostFormData(ctx *context.UlfsaarContext, path string, headers
return respHeader, statusCode, body, httpErr return respHeader, statusCode, body, httpErr
} }
func (c *client) PostFormDataWithProxy(ctx *context.UlfsaarContext, path, ip, port, username, password string, headers http.Header, payload map[string]string) (respHeader http.Header, statusCode int, body []byte, err error) { func (c *client) PostFormData(ctx *context.UlfsaarContext, url string, headers http.Header, payload map[string]string) (respHeader http.Header, statusCode int, body []byte, err error) {
searchUrl := c.options.Address + path //url := c.options.Address + path
startTime := time.Now() startTime := time.Now()
var stringProxUrl string
proxUrl, err := url.Parse(fmt.Sprintf("https://%s:%s@%s:%s", username, password, ip, port))
if err != nil {
stringProxUrl = fmt.Sprintf("https://%s:%s@%s:%s", username, password, ip, port)
} else {
stringProxUrl = proxUrl.String()
}
request := c.httpClient.SetProxy(stringProxUrl).R() request := c.httpClient.R()
request.SetFormData(payload)
for h, val := range headers { for h, val := range headers {
request.Header[h] = val request.Header[h] = val
} }
request.SetFormData(payload)
if headers[ContentType] == nil { if headers[ContentType] == nil {
request.Header.Set(ContentType, ApplicationJSON) request.Header.Set(ContentType, ApplicationJSON)
} }
request.Header.Set(UserAgent, UserAgentValue)
httpResp, httpErr := request.Post(searchUrl) httpResp, httpErr := request.Post(url)
if httpResp != nil { if httpResp != nil {
body = httpResp.Body() body = httpResp.Body()
...@@ -303,7 +303,7 @@ func (c *client) PostFormDataWithProxy(ctx *context.UlfsaarContext, path, ip, po ...@@ -303,7 +303,7 @@ func (c *client) PostFormDataWithProxy(ctx *context.UlfsaarContext, path, ip, po
} }
ctx.Info("PostFormData", ctx.Info("PostFormData",
logger.ToField(urlKey, searchUrl), logger.ToField(urlKey, url),
logger.ToField(requestKey, toRequest(request.Header, payload)), logger.ToField(requestKey, toRequest(request.Header, payload)),
logger.ToField(responseKey, toResponse(statusCode, respHeader, body)), logger.ToField(responseKey, toResponse(statusCode, respHeader, body)),
logger.ToField(startProcessingTimeKey, startProcessingTime(startTime)), logger.ToField(startProcessingTimeKey, startProcessingTime(startTime)),
...@@ -317,8 +317,8 @@ func (c *client) PostFormDataWithProxy(ctx *context.UlfsaarContext, path, ip, po ...@@ -317,8 +317,8 @@ func (c *client) PostFormDataWithProxy(ctx *context.UlfsaarContext, path, ip, po
return respHeader, statusCode, body, httpErr return respHeader, statusCode, body, httpErr
} }
func (c *client) PostMultipartFormFilesAndData(ctx *context.UlfsaarContext, path string, headers http.Header, formData []*MultipartField, formFiles []MultipartFileRequest) (respHeader http.Header, statusCode int, body []byte, err error) { func (c *client) PostMultipartFormFilesAndData(ctx *context.UlfsaarContext, url string, headers http.Header, formData []*MultipartField, formFiles []MultipartFileRequest) (respHeader http.Header, statusCode int, body []byte, err error) {
url := c.options.Address + path //url := c.options.Address + path
startTime := time.Now() startTime := time.Now()
request := c.httpClient.R() request := c.httpClient.R()
...@@ -358,8 +358,8 @@ func (c *client) PostMultipartFormFilesAndData(ctx *context.UlfsaarContext, path ...@@ -358,8 +358,8 @@ func (c *client) PostMultipartFormFilesAndData(ctx *context.UlfsaarContext, path
return respHeader, statusCode, body, httpErr return respHeader, statusCode, body, httpErr
} }
func (c *client) Put(ctx *context.UlfsaarContext, path string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error) { func (c *client) Put(ctx *context.UlfsaarContext, url string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error) {
url := c.options.Address + path //url := c.options.Address + path
startTime := time.Now() startTime := time.Now()
request := c.httpClient.R() request := c.httpClient.R()
...@@ -397,8 +397,8 @@ func (c *client) Put(ctx *context.UlfsaarContext, path string, headers http.Head ...@@ -397,8 +397,8 @@ func (c *client) Put(ctx *context.UlfsaarContext, path string, headers http.Head
return respHeader, statusCode, body, httpErr return respHeader, statusCode, body, httpErr
} }
func (c *client) Get(ctx *context.UlfsaarContext, path string, headers http.Header) (respHeader http.Header, statusCode int, body []byte, err error) { func (c *client) Get(ctx *context.UlfsaarContext, url string, headers http.Header) (respHeader http.Header, statusCode int, body []byte, err error) {
url := c.options.Address + path //url := c.options.Address + path
startTime := time.Now() startTime := time.Now()
request := c.httpClient.R() request := c.httpClient.R()
...@@ -431,8 +431,8 @@ func (c *client) Get(ctx *context.UlfsaarContext, path string, headers http.Head ...@@ -431,8 +431,8 @@ func (c *client) Get(ctx *context.UlfsaarContext, path string, headers http.Head
return respHeader, statusCode, body, httpErr return respHeader, statusCode, body, httpErr
} }
func (c *client) GetWithQueryParam(ctx *context.UlfsaarContext, path string, headers http.Header, queryParam map[string]string) (respHeader http.Header, statusCode int, body []byte, err error) { func (c *client) GetWithQueryParam(ctx *context.UlfsaarContext, url string, headers http.Header, queryParam map[string]string) (respHeader http.Header, statusCode int, body []byte, err error) {
url := c.options.Address + path //url := c.options.Address + path
startTime := time.Now() startTime := time.Now()
request := c.httpClient.R() request := c.httpClient.R()
...@@ -466,8 +466,8 @@ func (c *client) GetWithQueryParam(ctx *context.UlfsaarContext, path string, hea ...@@ -466,8 +466,8 @@ func (c *client) GetWithQueryParam(ctx *context.UlfsaarContext, path string, hea
return respHeader, statusCode, body, httpErr return respHeader, statusCode, body, httpErr
} }
func (c *client) Delete(ctx *context.UlfsaarContext, path string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error) { func (c *client) Delete(ctx *context.UlfsaarContext, url string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error) {
url := c.options.Address + path //url := c.options.Address + path
startTime := time.Now() startTime := time.Now()
request := c.httpClient.R() request := c.httpClient.R()
...@@ -502,8 +502,8 @@ func (c *client) Delete(ctx *context.UlfsaarContext, path string, headers http.H ...@@ -502,8 +502,8 @@ func (c *client) Delete(ctx *context.UlfsaarContext, path string, headers http.H
return respHeader, statusCode, body, httpErr return respHeader, statusCode, body, httpErr
} }
func (c *client) Patch(ctx *context.UlfsaarContext, path string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error) { func (c *client) Patch(ctx *context.UlfsaarContext, url string, headers http.Header, payload interface{}) (respHeader http.Header, statusCode int, body []byte, err error) {
url := c.options.Address + path //url := c.options.Address + path
startTime := time.Now() startTime := time.Now()
request := c.httpClient.R() request := c.httpClient.R()
......
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