2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-03-25 17:13:05 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
2021-01-25 14:54:38 +08:00
|
|
|
package client
|
2020-03-25 17:13:05 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-04-04 22:46:52 +08:00
|
|
|
// Prefix is a chaining function,
|
|
|
|
// which sets the URL prefix for next request of this client.
|
|
|
|
func (c *Client) Prefix(prefix string) *Client {
|
2021-11-25 16:23:18 +08:00
|
|
|
newClient := c.Clone()
|
2020-04-04 22:46:52 +08:00
|
|
|
newClient.SetPrefix(prefix)
|
|
|
|
return newClient
|
|
|
|
}
|
|
|
|
|
2020-03-25 17:13:05 +08:00
|
|
|
// Header is a chaining function,
|
|
|
|
// which sets custom HTTP headers with map for next request.
|
|
|
|
func (c *Client) Header(m map[string]string) *Client {
|
2021-11-25 16:23:18 +08:00
|
|
|
newClient := c.Clone()
|
2020-03-25 17:13:05 +08:00
|
|
|
newClient.SetHeaderMap(m)
|
|
|
|
return newClient
|
|
|
|
}
|
|
|
|
|
2021-08-02 19:58:04 +08:00
|
|
|
// HeaderRaw is a chaining function,
|
2020-03-25 17:13:05 +08:00
|
|
|
// which sets custom HTTP header using raw string for next request.
|
|
|
|
func (c *Client) HeaderRaw(headers string) *Client {
|
2021-11-25 16:23:18 +08:00
|
|
|
newClient := c.Clone()
|
2020-03-25 17:13:05 +08:00
|
|
|
newClient.SetHeaderRaw(headers)
|
|
|
|
return newClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cookie is a chaining function,
|
|
|
|
// which sets cookie items with map for next request.
|
|
|
|
func (c *Client) Cookie(m map[string]string) *Client {
|
2021-11-25 16:23:18 +08:00
|
|
|
newClient := c.Clone()
|
2020-03-25 17:13:05 +08:00
|
|
|
newClient.SetCookieMap(m)
|
|
|
|
return newClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContentType is a chaining function,
|
|
|
|
// which sets HTTP content type for the next request.
|
|
|
|
func (c *Client) ContentType(contentType string) *Client {
|
2021-11-25 16:23:18 +08:00
|
|
|
newClient := c.Clone()
|
2020-03-25 17:13:05 +08:00
|
|
|
newClient.SetContentType(contentType)
|
|
|
|
return newClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContentJson is a chaining function,
|
|
|
|
// which sets the HTTP content type as "application/json" for the next request.
|
|
|
|
//
|
|
|
|
// Note that it also checks and encodes the parameter to JSON format automatically.
|
|
|
|
func (c *Client) ContentJson() *Client {
|
2021-11-25 16:23:18 +08:00
|
|
|
newClient := c.Clone()
|
2020-03-25 17:13:05 +08:00
|
|
|
newClient.SetContentType("application/json")
|
|
|
|
return newClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContentXml is a chaining function,
|
|
|
|
// which sets the HTTP content type as "application/xml" for the next request.
|
|
|
|
//
|
|
|
|
// Note that it also checks and encodes the parameter to XML format automatically.
|
|
|
|
func (c *Client) ContentXml() *Client {
|
2021-11-25 16:23:18 +08:00
|
|
|
newClient := c.Clone()
|
2020-03-25 17:13:05 +08:00
|
|
|
newClient.SetContentType("application/xml")
|
|
|
|
return newClient
|
|
|
|
}
|
|
|
|
|
2021-08-02 19:58:04 +08:00
|
|
|
// Timeout is a chaining function,
|
2020-03-25 17:13:05 +08:00
|
|
|
// which sets the timeout for next request.
|
|
|
|
func (c *Client) Timeout(t time.Duration) *Client {
|
2021-11-25 16:23:18 +08:00
|
|
|
newClient := c.Clone()
|
2020-03-25 17:13:05 +08:00
|
|
|
newClient.SetTimeout(t)
|
|
|
|
return newClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// BasicAuth is a chaining function,
|
|
|
|
// which sets HTTP basic authentication information for next request.
|
|
|
|
func (c *Client) BasicAuth(user, pass string) *Client {
|
2021-11-25 16:23:18 +08:00
|
|
|
newClient := c.Clone()
|
2020-03-25 17:13:05 +08:00
|
|
|
newClient.SetBasicAuth(user, pass)
|
|
|
|
return newClient
|
|
|
|
}
|
|
|
|
|
2020-04-16 15:43:21 +08:00
|
|
|
// 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 {
|
2021-11-25 16:23:18 +08:00
|
|
|
newClient := c.Clone()
|
2020-04-16 15:43:21 +08:00
|
|
|
newClient.SetRetry(retryCount, retryInterval)
|
2021-01-25 14:54:38 +08:00
|
|
|
return newClient
|
|
|
|
}
|
|
|
|
|
2020-06-22 21:12:11 +08:00
|
|
|
// Proxy is a chaining function,
|
|
|
|
// which sets proxy for next request.
|
|
|
|
// Make sure you pass the correct `proxyURL`.
|
|
|
|
// 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 {
|
2021-11-25 16:23:18 +08:00
|
|
|
newClient := c.Clone()
|
2020-06-22 21:12:11 +08:00
|
|
|
newClient.SetProxy(proxyURL)
|
2021-01-25 14:54:38 +08:00
|
|
|
return newClient
|
2020-06-22 21:12:11 +08:00
|
|
|
}
|
2020-09-09 22:26:46 +08:00
|
|
|
|
|
|
|
// RedirectLimit is a chaining function,
|
|
|
|
// which sets the redirect limit the number of jumps for the request.
|
|
|
|
func (c *Client) RedirectLimit(redirectLimit int) *Client {
|
2021-11-25 16:23:18 +08:00
|
|
|
newClient := c.Clone()
|
2020-09-09 22:26:46 +08:00
|
|
|
newClient.SetRedirectLimit(redirectLimit)
|
2021-01-25 14:54:38 +08:00
|
|
|
return newClient
|
2020-09-09 22:26:46 +08:00
|
|
|
}
|