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-12-03 23:32:00 +08:00
|
|
|
package gclient
|
2020-03-25 17:13:05 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2023-07-04 20:21:12 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/net/gsvc"
|
2020-03-25 17:13:05 +08:00
|
|
|
)
|
|
|
|
|
2020-04-04 22:46:52 +08:00
|
|
|
// Prefix is a chaining function,
|
|
|
|
// which sets the URL prefix for next request of this client.
|
2022-06-15 16:28:17 +08:00
|
|
|
// Eg:
|
|
|
|
// Prefix("http://127.0.0.1:8199/api/v1")
|
|
|
|
// Prefix("http://127.0.0.1:8199/api/v2")
|
2020-04-04 22:46:52 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-07-04 20:21:12 +08:00
|
|
|
// Discovery is a chaining function, which sets the discovery for client.
|
|
|
|
// You can use `Discovery(nil)` to disable discovery feature for current client.
|
|
|
|
func (c *Client) Discovery(discovery gsvc.Discovery) *Client {
|
|
|
|
newClient := c.Clone()
|
|
|
|
newClient.SetDiscovery(discovery)
|
|
|
|
return newClient
|
|
|
|
}
|
|
|
|
|
2020-03-25 17:13:05 +08:00
|
|
|
// 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()
|
2022-01-10 16:42:30 +08:00
|
|
|
newClient.SetContentType(httpHeaderContentTypeJson)
|
2020-03-25 17:13:05 +08:00
|
|
|
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()
|
2022-01-10 16:42:30 +08:00
|
|
|
newClient.SetContentType(httpHeaderContentTypeXml)
|
2020-03-25 17:13:05 +08:00
|
|
|
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.
|
2024-03-24 21:18:30 +08:00
|
|
|
// TODO removed.
|
2020-04-16 15:43:21 +08:00
|
|
|
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
|
|
|
}
|
2023-10-17 21:19:59 +08:00
|
|
|
|
|
|
|
// NoUrlEncode sets the mark that do not encode the parameters before sending request.
|
|
|
|
func (c *Client) NoUrlEncode() *Client {
|
|
|
|
newClient := c.Clone()
|
|
|
|
newClient.SetNoUrlEncode(true)
|
|
|
|
return newClient
|
|
|
|
}
|