Documentation
¶
Overview ¶
Package client 提供七牛云 API 的底层 HTTP 客户端。
Client 封装了标准库 net/http.Client,自动处理 User-Agent 设置、 请求 ID 注入(X-Reqid)、认证签名、JSON/表单序列化和错误解析。
高级用户通常使用 storage 或 storagev2 包,不需要直接使用本包。
使用默认客户端 ¶
var ret MyResponse err := client.DefaultClient.CallWithJson(ctx, &ret, "POST", url, nil, body)
错误处理 ¶
API 错误以 *ErrorInfo 返回,包含 HTTP 状态码、错误码和请求 ID:
err := client.DefaultClient.Call(ctx, &ret, "GET", url, nil)
if e, ok := err.(*client.ErrorInfo); ok {
fmt.Println(e.Code, e.Err, e.Reqid)
}
调试模式 ¶
client.TurnOnDebug() // 输出 HTTP 请求/响应详情 client.DeepDebugInfo = true // 同时输出请求/响应 body
DNS 预解析 ¶
通过 Context 注入预解析的 IP 地址,跳过 DNS 查询:
ctx = client.WithResolvedIPs(ctx, "up.qiniup.com", []net.IP{ip})
Index ¶
- Constants
- Variables
- func AddHttpHeaderRange(header http.Header, contentRange string)
- func CallRet(ctx context.Context, ret interface{}, resp *http.Response) (err error)
- func DecodeJsonFromReader(reader io.Reader, v interface{}) error
- func ResponseError(resp *http.Response) error
- func SetAppName(userApp string) error
- func TurnOnDebug()
- func WithDialTimeout(ctx context.Context, timeout time.Duration) context.Context
- func WithKeepAliveInterval(ctx context.Context, interval time.Duration) context.Context
- func WithResolvedIPs(ctx context.Context, domain string, ips []net.IP) context.Context
- type Client
- func (r Client) Call(ctx context.Context, ret interface{}, method, reqUrl string, ...) (err error)
- func (r Client) CallWith(ctx context.Context, ret interface{}, method, reqUrl string, ...) (err error)
- func (r Client) CallWith64(ctx context.Context, ret interface{}, method, reqUrl string, ...) (err error)
- func (r Client) CallWithBodyGetter(ctx context.Context, ret interface{}, method, reqUrl string, ...) (err error)
- func (r Client) CallWithForm(ctx context.Context, ret interface{}, method, reqUrl string, ...) (err error)
- func (r Client) CallWithJson(ctx context.Context, ret interface{}, method, reqUrl string, ...) (err error)
- func (r Client) CredentialedCall(ctx context.Context, cred *auth.Credentials, tokenType auth.TokenType, ...) error
- func (r Client) CredentialedCallWith(ctx context.Context, cred *auth.Credentials, tokenType auth.TokenType, ...) error
- func (r Client) CredentialedCallWith64(ctx context.Context, cred *auth.Credentials, tokenType auth.TokenType, ...) error
- func (r Client) CredentialedCallWithForm(ctx context.Context, cred *auth.Credentials, tokenType auth.TokenType, ...) error
- func (r Client) CredentialedCallWithJson(ctx context.Context, cred *auth.Credentials, tokenType auth.TokenType, ...) error
- func (r Client) Do(ctx context.Context, req *http.Request) (resp *http.Response, err error)
- func (r Client) DoRequest(ctx context.Context, method, reqUrl string, headers http.Header) (resp *http.Response, err error)
- func (r Client) DoRequestWith(ctx context.Context, method, reqUrl string, headers http.Header, ...) (resp *http.Response, err error)
- func (r Client) DoRequestWith64(ctx context.Context, method, reqUrl string, headers http.Header, ...) (resp *http.Response, err error)
- func (r Client) DoRequestWithBodyGetter(ctx context.Context, method, reqUrl string, headers http.Header, ...) (resp *http.Response, err error)
- func (r Client) DoRequestWithForm(ctx context.Context, method, reqUrl string, headers http.Header, ...) (resp *http.Response, err error)
- func (r Client) DoRequestWithJson(ctx context.Context, method, reqUrl string, headers http.Header, ...) (resp *http.Response, err error)
- type ErrorInfo
Constants ¶
View Source
const (
RequestHeaderKeyXQiniuDate = "X-Qiniu-Date"
)
Variables ¶
View Source
var ( UserAgent = getUserAgentWithAppName("default") DefaultClient = Client{&http.Client{Transport: DefaultTransport}} // 用来打印调试信息 DebugMode = false DeepDebugInfo = false )
View Source
var DefaultTransport http.RoundTripper = &http.Transport{ Proxy: http.ProxyFromEnvironment, DialContext: defaultDialFunc, MaxIdleConns: 100, IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, }
Functions ¶
func AddHttpHeaderRange ¶ added in v7.18.0
func DecodeJsonFromReader ¶ added in v7.17.0
func ResponseError ¶
func WithDialTimeout ¶ added in v7.21.0
func WithKeepAliveInterval ¶ added in v7.21.0
Types ¶
type Client ¶
Client 负责发送HTTP请求到七牛接口服务器
func (Client) CallWith64 ¶
func (Client) CallWithBodyGetter ¶ added in v7.9.7
func (Client) CallWithForm ¶
func (Client) CallWithJson ¶
func (Client) CredentialedCall ¶
func (Client) CredentialedCallWith ¶
func (Client) CredentialedCallWith64 ¶
func (Client) CredentialedCallWithForm ¶
func (Client) CredentialedCallWithJson ¶
func (Client) DoRequestWith ¶
func (Client) DoRequestWith64 ¶
func (Client) DoRequestWithBodyGetter ¶ added in v7.9.7
func (Client) DoRequestWithForm ¶
Click to show internal directories.
Click to hide internal directories.