From 9105590c69723fa13e3f3ed44b48d1249858e966 Mon Sep 17 00:00:00 2001 From: John Guo Date: Thu, 25 Nov 2021 16:23:18 +0800 Subject: [PATCH] improve chaining feature for ghttp.Client --- net/ghttp/internal/client/client.go | 1 - net/ghttp/internal/client/client_chain.go | 65 +++++------------------ 2 files changed, 13 insertions(+), 53 deletions(-) diff --git a/net/ghttp/internal/client/client.go b/net/ghttp/internal/client/client.go index 1738495db..4802c6946 100644 --- a/net/ghttp/internal/client/client.go +++ b/net/ghttp/internal/client/client.go @@ -32,7 +32,6 @@ import ( type Client struct { http.Client // Underlying HTTP Client. dump bool // Mark this request will be dumped. - parent *Client // Parent http client, this is used for chaining operations. header map[string]string // Custom header map. cookies map[string]string // Custom cookie map. prefix string // Prefix for request. diff --git a/net/ghttp/internal/client/client_chain.go b/net/ghttp/internal/client/client_chain.go index 7069db7f8..898360987 100644 --- a/net/ghttp/internal/client/client_chain.go +++ b/net/ghttp/internal/client/client_chain.go @@ -13,10 +13,7 @@ import ( // Prefix is a chaining function, // which sets the URL prefix for next request of this client. func (c *Client) Prefix(prefix string) *Client { - newClient := c - if c.parent == nil { - newClient = c.Clone() - } + newClient := c.Clone() newClient.SetPrefix(prefix) return newClient } @@ -24,10 +21,7 @@ func (c *Client) Prefix(prefix string) *Client { // Header is a chaining function, // which sets custom HTTP headers with map for next request. func (c *Client) Header(m map[string]string) *Client { - newClient := c - if c.parent == nil { - newClient = c.Clone() - } + newClient := c.Clone() newClient.SetHeaderMap(m) return newClient } @@ -35,10 +29,7 @@ func (c *Client) Header(m map[string]string) *Client { // HeaderRaw is a chaining function, // which sets custom HTTP header using raw string for next request. func (c *Client) HeaderRaw(headers string) *Client { - newClient := c - if c.parent == nil { - newClient = c.Clone() - } + newClient := c.Clone() newClient.SetHeaderRaw(headers) return newClient } @@ -46,10 +37,7 @@ func (c *Client) HeaderRaw(headers string) *Client { // Cookie is a chaining function, // which sets cookie items with map for next request. func (c *Client) Cookie(m map[string]string) *Client { - newClient := c - if c.parent == nil { - newClient = c.Clone() - } + newClient := c.Clone() newClient.SetCookieMap(m) return newClient } @@ -57,10 +45,7 @@ func (c *Client) Cookie(m map[string]string) *Client { // ContentType is a chaining function, // which sets HTTP content type for the next request. func (c *Client) ContentType(contentType string) *Client { - newClient := c - if c.parent == nil { - newClient = c.Clone() - } + newClient := c.Clone() newClient.SetContentType(contentType) return newClient } @@ -70,10 +55,7 @@ func (c *Client) ContentType(contentType string) *Client { // // Note that it also checks and encodes the parameter to JSON format automatically. func (c *Client) ContentJson() *Client { - newClient := c - if c.parent == nil { - newClient = c.Clone() - } + newClient := c.Clone() newClient.SetContentType("application/json") return newClient } @@ -83,10 +65,7 @@ func (c *Client) ContentJson() *Client { // // Note that it also checks and encodes the parameter to XML format automatically. func (c *Client) ContentXml() *Client { - newClient := c - if c.parent == nil { - newClient = c.Clone() - } + newClient := c.Clone() newClient.SetContentType("application/xml") return newClient } @@ -94,10 +73,7 @@ func (c *Client) ContentXml() *Client { // Timeout is a chaining function, // which sets the timeout for next request. func (c *Client) Timeout(t time.Duration) *Client { - newClient := c - if c.parent == nil { - newClient = c.Clone() - } + newClient := c.Clone() newClient.SetTimeout(t) return newClient } @@ -105,10 +81,7 @@ func (c *Client) Timeout(t time.Duration) *Client { // BasicAuth is a chaining function, // which sets HTTP basic authentication information for next request. func (c *Client) BasicAuth(user, pass string) *Client { - newClient := c - if c.parent == nil { - newClient = c.Clone() - } + newClient := c.Clone() newClient.SetBasicAuth(user, pass) return newClient } @@ -116,10 +89,7 @@ func (c *Client) BasicAuth(user, pass string) *Client { // Retry is a chaining function, // which sets retry count and interval when failure for next request. func (c *Client) Retry(retryCount int, retryInterval time.Duration) *Client { - newClient := c - if c.parent == nil { - newClient = c.Clone() - } + newClient := c.Clone() newClient.SetRetry(retryCount, retryInterval) return newClient } @@ -127,10 +97,7 @@ func (c *Client) Retry(retryCount int, retryInterval time.Duration) *Client { // Dump is a chaining function, // which enables/disables dump feature for this request. func (c *Client) Dump(dump ...bool) *Client { - newClient := c - if c.parent == nil { - newClient = c.Clone() - } + newClient := c.Clone() if len(dump) > 0 { newClient.SetDump(dump[0]) } else { @@ -145,10 +112,7 @@ func (c *Client) Dump(dump ...bool) *Client { // The correct pattern is like `http://USER:PASSWORD@IP:PORT` or `socks5://USER:PASSWORD@IP:PORT`. // Only `http` and `socks5` proxies are supported currently. func (c *Client) Proxy(proxyURL string) *Client { - newClient := c - if c.parent == nil { - newClient = c.Clone() - } + newClient := c.Clone() newClient.SetProxy(proxyURL) return newClient } @@ -156,10 +120,7 @@ func (c *Client) Proxy(proxyURL string) *Client { // RedirectLimit is a chaining function, // which sets the redirect limit the number of jumps for the request. func (c *Client) RedirectLimit(redirectLimit int) *Client { - newClient := c - if c.parent == nil { - newClient = c.Clone() - } + newClient := c.Clone() newClient.SetRedirectLimit(redirectLimit) return newClient }