github.com/gogf/gf/v2@v2.7.4/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/gogf/gf. 6 7 package gclient 8 9 import ( 10 "time" 11 12 "github.com/gogf/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 // TODO removed. 105 func (c *Client) Retry(retryCount int, retryInterval time.Duration) *Client { 106 newClient := c.Clone() 107 newClient.SetRetry(retryCount, retryInterval) 108 return newClient 109 } 110 111 // Proxy is a chaining function, 112 // which sets proxy for next request. 113 // Make sure you pass the correct `proxyURL`. 114 // The correct pattern is like `http://USER:PASSWORD@IP:PORT` or `socks5://USER:PASSWORD@IP:PORT`. 115 // Only `http` and `socks5` proxies are supported currently. 116 func (c *Client) Proxy(proxyURL string) *Client { 117 newClient := c.Clone() 118 newClient.SetProxy(proxyURL) 119 return newClient 120 } 121 122 // RedirectLimit is a chaining function, 123 // which sets the redirect limit the number of jumps for the request. 124 func (c *Client) RedirectLimit(redirectLimit int) *Client { 125 newClient := c.Clone() 126 newClient.SetRedirectLimit(redirectLimit) 127 return newClient 128 } 129 130 // NoUrlEncode sets the mark that do not encode the parameters before sending request. 131 func (c *Client) NoUrlEncode() *Client { 132 newClient := c.Clone() 133 newClient.SetNoUrlEncode(true) 134 return newClient 135 }