github.com/wangyougui/gf/v2@v2.6.5/net/gclient/gclient_chain.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gclient 8 9 import ( 10 "time" 11 12 "github.com/wangyougui/gf/v2/net/gsvc" 13 ) 14 15 // Prefix is a chaining function, 16 // which sets the URL prefix for next request of this client. 17 // Eg: 18 // Prefix("http://127.0.0.1:8199/api/v1") 19 // Prefix("http://127.0.0.1:8199/api/v2") 20 func (c *Client) Prefix(prefix string) *Client { 21 newClient := c.Clone() 22 newClient.SetPrefix(prefix) 23 return newClient 24 } 25 26 // Header is a chaining function, 27 // which sets custom HTTP headers with map for next request. 28 func (c *Client) Header(m map[string]string) *Client { 29 newClient := c.Clone() 30 newClient.SetHeaderMap(m) 31 return newClient 32 } 33 34 // HeaderRaw is a chaining function, 35 // which sets custom HTTP header using raw string for next request. 36 func (c *Client) HeaderRaw(headers string) *Client { 37 newClient := c.Clone() 38 newClient.SetHeaderRaw(headers) 39 return newClient 40 } 41 42 // Discovery is a chaining function, which sets the discovery for client. 43 // You can use `Discovery(nil)` to disable discovery feature for current client. 44 func (c *Client) Discovery(discovery gsvc.Discovery) *Client { 45 newClient := c.Clone() 46 newClient.SetDiscovery(discovery) 47 return newClient 48 } 49 50 // Cookie is a chaining function, 51 // which sets cookie items with map for next request. 52 func (c *Client) Cookie(m map[string]string) *Client { 53 newClient := c.Clone() 54 newClient.SetCookieMap(m) 55 return newClient 56 } 57 58 // ContentType is a chaining function, 59 // which sets HTTP content type for the next request. 60 func (c *Client) ContentType(contentType string) *Client { 61 newClient := c.Clone() 62 newClient.SetContentType(contentType) 63 return newClient 64 } 65 66 // ContentJson is a chaining function, 67 // which sets the HTTP content type as "application/json" for the next request. 68 // 69 // Note that it also checks and encodes the parameter to JSON format automatically. 70 func (c *Client) ContentJson() *Client { 71 newClient := c.Clone() 72 newClient.SetContentType(httpHeaderContentTypeJson) 73 return newClient 74 } 75 76 // ContentXml is a chaining function, 77 // which sets the HTTP content type as "application/xml" for the next request. 78 // 79 // Note that it also checks and encodes the parameter to XML format automatically. 80 func (c *Client) ContentXml() *Client { 81 newClient := c.Clone() 82 newClient.SetContentType(httpHeaderContentTypeXml) 83 return newClient 84 } 85 86 // Timeout is a chaining function, 87 // which sets the timeout for next request. 88 func (c *Client) Timeout(t time.Duration) *Client { 89 newClient := c.Clone() 90 newClient.SetTimeout(t) 91 return newClient 92 } 93 94 // BasicAuth is a chaining function, 95 // which sets HTTP basic authentication information for next request. 96 func (c *Client) BasicAuth(user, pass string) *Client { 97 newClient := c.Clone() 98 newClient.SetBasicAuth(user, pass) 99 return newClient 100 } 101 102 // Retry is a chaining function, 103 // which sets retry count and interval when failure for next request. 104 func (c *Client) Retry(retryCount int, retryInterval time.Duration) *Client { 105 newClient := c.Clone() 106 newClient.SetRetry(retryCount, retryInterval) 107 return newClient 108 } 109 110 // Proxy is a chaining function, 111 // which sets proxy for next request. 112 // Make sure you pass the correct `proxyURL`. 113 // The correct pattern is like `http://USER:PASSWORD@IP:PORT` or `socks5://USER:PASSWORD@IP:PORT`. 114 // Only `http` and `socks5` proxies are supported currently. 115 func (c *Client) Proxy(proxyURL string) *Client { 116 newClient := c.Clone() 117 newClient.SetProxy(proxyURL) 118 return newClient 119 } 120 121 // RedirectLimit is a chaining function, 122 // which sets the redirect limit the number of jumps for the request. 123 func (c *Client) RedirectLimit(redirectLimit int) *Client { 124 newClient := c.Clone() 125 newClient.SetRedirectLimit(redirectLimit) 126 return newClient 127 } 128 129 // NoUrlEncode sets the mark that do not encode the parameters before sending request. 130 func (c *Client) NoUrlEncode() *Client { 131 newClient := c.Clone() 132 newClient.SetNoUrlEncode(true) 133 return newClient 134 }