httpapi

package
v1.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 11 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package httpapi 提供重构 DNS provider 共用的最小 HTTP 客户端。

只依赖标准库:统一超时、错误体截断、JSON/XML 解码与请求构造。 签名逻辑放在各自的 sdk/<vendor> 包中,通过 NewRequest/Send 组合。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloneHeader

func CloneHeader(in http.Header) http.Header

CloneHeader 复制 header,避免签名过程修改调用方数据。

func EncodeJSON

func EncodeJSON(v any) ([]byte, error)

EncodeJSON 编码 JSON 请求体。

func EncodeXML

func EncodeXML(v any) ([]byte, error)

EncodeXML 编码 XML 请求体。

func IsConflict

func IsConflict(err error) bool

IsConflict 判断错误是否为 409。

func IsNotFound

func IsNotFound(err error) bool

IsNotFound 判断错误是否为 404。

func IsRateLimited

func IsRateLimited(err error) bool

IsRateLimited 判断错误是否为 429 或 5xx。

func JSON

func JSON(body []byte, out any) error

JSON 解码响应体。

func XML

func XML(body []byte, out any) error

XML 解码响应体。

Types

type Client

type Client struct {
	HTTP      *http.Client
	BaseURL   string
	UserAgent string
	MaxBody   int64
}

Client 是最小 HTTP 客户端。

func New

func New(baseURL string) *Client

New 创建带默认超时的客户端。

func (*Client) Do

func (c *Client) Do(ctx context.Context, method, path string, query url.Values, header http.Header, body []byte) (*Response, error)

Do 是 NewRequest + Send 的便捷封装。

func (*Client) NewRequest

func (c *Client) NewRequest(ctx context.Context, method, path string, query url.Values, header http.Header, body []byte) (*http.Request, error)

NewRequest 构造请求。path 可以是以 / 开头的绝对路径,也可以是完整 URL。

func (*Client) Send

func (c *Client) Send(req *http.Request) (*Response, error)

Send 发送请求并读取有上限的响应体。

type Error

type Error struct {
	Method string
	URL    string
	Status int
	Body   string
}

Error 表示非 2xx 响应。

func (*Error) Error

func (e *Error) Error() string

type Response

type Response struct {
	Status int
	Header http.Header
	Body   []byte
}

Response 是已读取完毕的响应。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL