Browse Source

update jsonutil

marion 4 years ago
parent
commit
0f52fc2acc
1 changed files with 59 additions and 59 deletions
  1. 59 59
      utils/http_utils.go

+ 59 - 59
utils/http_utils.go

@@ -132,87 +132,87 @@ func NewPoolingHttpClient() *http.Client {
 }
 
 // 设置https忽略本地证书校验
-func (r *RequestPromise) SetSkipTls() *RequestPromise {
-	r.isSkipTls = true
-	return r
+func (v *RequestPromise) SetSkipTls() *RequestPromise {
+	v.isSkipTls = true
+	return v
 }
 
 // 设置http header
-func (r *RequestPromise) SetHeader(key string, value string) *RequestPromise {
+func (v *RequestPromise) SetHeader(key string, value string) *RequestPromise {
 	if len(strings.TrimSpace(key)) == 0 {
-		return r
+		return v
 	}
 	key = strings.TrimSpace(key)
-	if nil == r.headers {
-		r.headers = make(http.Header)
+	if nil == v.headers {
+		v.headers = make(http.Header)
 	}
-	r.headers.Set(key, value)
-	return r
+	v.headers.Set(key, value)
+	return v
 }
 
 // 设置http响应的编码,默认utf8
-func (r *RequestPromise) SetEncoding(encoding Charset) *RequestPromise {
+func (v *RequestPromise) SetEncoding(encoding Charset) *RequestPromise {
 	if encoding == UTF8 {
-		return r
+		return v
 	}
-	r.encoding = encoding
-	return r
+	v.encoding = encoding
+	return v
 }
 
 // 设置超时时间,从连接到接收到响应的总时间
 // 如果此处不设置则采用http client中设置的超时时间,默认http client超时时间30秒
 // 如果此处设置不等于零的值,则覆盖http client中设置的超时时间
 // 如果此处设置小于零的值,意为不超时
-func (r *RequestPromise) SetTimeout(timeout time.Duration) *RequestPromise {
+func (v *RequestPromise) SetTimeout(timeout time.Duration) *RequestPromise {
 	if timeout == 0 {
-		return r
+		return v
 	}
-	r.timeout = timeout
-	return r
+	v.timeout = timeout
+	return v
 }
 
 // 设置http或https代理,默认无代理
-func (r *RequestPromise) SetHttpProxy(proxyUri string) *RequestPromise {
+func (v *RequestPromise) SetHttpProxy(proxyUri string) *RequestPromise {
 	if len(strings.TrimSpace(proxyUri)) == 0 {
-		return r
+		return v
 	}
 	proxyUri = strings.TrimSpace(proxyUri)
 	uri, err := (&url.URL{}).Parse(proxyUri)
 	if nil != err {
-		return r
+		return v
 	}
-	r.proxy = http.ProxyURL(uri)
-	return r
+	v.proxy = http.ProxyURL(uri)
+	return v
 }
 
 // 设置socket5代理,默认无代理
-func (r *RequestPromise) SetSocket5Proxy(proxyUri string) *RequestPromise {
+func (v *RequestPromise) SetSocket5Proxy(proxyUri string) *RequestPromise {
 	if len(strings.TrimSpace(proxyUri)) == 0 {
-		return r
+		return v
 	}
 	proxyUri = strings.TrimSpace(proxyUri)
 	dialer, err := proxy.SOCKS5("tcp", proxyUri, nil, proxy.Direct)
 	if nil != err {
-		return r
+		return v
 	}
-	r.dialContext = func(_ context.Context, network string, address string) (net.Conn, error) {
+	v.dialContext = func(_ context.Context, network string, address string) (net.Conn, error) {
 		return dialer.Dial(network, address)
 	}
-	return r
+	return v
 }
 
 // 设置事先实例化好的http client,默认每次请求会新建一个http client
-func (r *RequestPromise) SetClient(client *http.Client) *RequestPromise {
+func (v *RequestPromise) SetClient(client *http.Client) *RequestPromise {
 	if nil == client {
-		return r
+		return v
 	}
-	r.client = client
-	return r
+	v.client = client
+	return v
 }
 
 // 发起请求并返回响应内容
 // FORM方式提交数据请设置Content-Type=application/x-www-form-urlencoded请求头,且io.Reader传url.Values.Encode得到的字符串的reader
-func (r *RequestPromise) Call(httpMethod string, targetUri string, data io.Reader) ([]byte, error) {
+func (v *RequestPromise) Call(httpMethod string, targetUri string, data io.Reader) ([]byte, error) {
 	targetUri = strings.TrimSpace(targetUri)
 	if len(targetUri) == 0 {
 		return nil, nil
@@ -228,37 +228,37 @@ func (r *RequestPromise) Call(httpMethod string, targetUri string, data io.Reade
 	if err != nil {
 		return nil, err
 	}
-	if nil != r.headers {
-		req.Header = r.headers
+	if nil != v.headers {
+		req.Header = v.headers
 	}
 
-	r.initClient()
+	v.initClient()
 
 	// send http request & get http response
-	resp, err := r.client.Do(req)
+	resp, err := v.client.Do(req)
 	if err != nil {
 		return nil, err
 	}
 
-	return r.readResponseBody(resp)
+	return v.readResponseBody(resp)
 }
 
-func (r *RequestPromise) initClient() {
+func (v *RequestPromise) initClient() {
 	// http client handle
-	if nil == r.client { // create new http client instance
-		r.client = &http.Client{Timeout: DefaultHttpTimeout} // default timeout
+	if nil == v.client { // create new http client instance
+		v.client = &http.Client{Timeout: DefaultHttpTimeout} // default timeout
 	}
-	if r.timeout < 0 {
-		r.timeout = DefaultHttpTimeout // default timeout
+	if v.timeout < 0 {
+		v.timeout = DefaultHttpTimeout // default timeout
 	}
-	if r.timeout > 0 {
-		r.client.Timeout = r.timeout
+	if v.timeout > 0 {
+		v.client.Timeout = v.timeout
 	}
-	if r.isSkipTls {
-		if nil == r.client.Transport {
-			r.client.Transport = &http.Transport{}
+	if v.isSkipTls {
+		if nil == v.client.Transport {
+			v.client.Transport = &http.Transport{}
 		}
-		transport := (r.client.Transport).(*http.Transport)
+		transport := (v.client.Transport).(*http.Transport)
 		transport.TLSClientConfig = &tls.Config{
 			InsecureSkipVerify: true,
 			//VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
@@ -266,21 +266,21 @@ func (r *RequestPromise) initClient() {
 			//},
 		}
 	}
-	if nil != r.proxy || nil != r.dialContext {
-		if nil == r.client.Transport {
-			r.client.Transport = &http.Transport{}
+	if nil != v.proxy || nil != v.dialContext {
+		if nil == v.client.Transport {
+			v.client.Transport = &http.Transport{}
 		}
-		transport := (r.client.Transport).(*http.Transport)
-		if nil != r.proxy {
-			transport.Proxy = r.proxy
+		transport := (v.client.Transport).(*http.Transport)
+		if nil != v.proxy {
+			transport.Proxy = v.proxy
 		}
-		if nil != r.dialContext {
-			transport.DialContext = r.dialContext
+		if nil != v.dialContext {
+			transport.DialContext = v.dialContext
 		}
 	}
 }
 
-func (r *RequestPromise) readResponseBody(resp *http.Response) ([]byte, error) {
+func (v *RequestPromise) readResponseBody(resp *http.Response) ([]byte, error) {
 	defer func(body io.ReadCloser) {
 		_ = body.Close()
 	}(resp.Body)
@@ -310,8 +310,8 @@ func (r *RequestPromise) readResponseBody(resp *http.Response) ([]byte, error) {
 	if err != nil {
 		return nil, err
 	}
-	if r.encoding != "" {
-		body = ConvertToEncodingBytes(body, r.encoding)
+	if v.encoding != "" {
+		body = ConvertToEncodingBytes(body, v.encoding)
 	}
 	return body, nil
 }