rest_api

package
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	ServiceName string   `json:"service_name" yaml:"service_name"`
	Urls        []string `json:"urls"         yaml:"urls"`
	SignName    string   `json:"sign_name"    yaml:"sign_name"`
	Timeout     int      `json:"timeout"      yaml:"timeout"`
}

Config 描述单个 RestApi 实例的装配配置,由 RestApiMgr.Init 逐项解析。

字段契约:

  • ServiceName:注册到 mgr 的 key,同时作为日志/错误归属;空则跳过该实例;
  • Urls:后端地址列表,可多元素做分片,URL 形如 "https://host/path?"(问号属配置,由 Map2uri 直接拼接 query);
  • SignName:可选,指向 http_sign.SignMgr 中的 HttpSign 实例名,缺省则该实例不做签名;
  • Timeout:兜底超时(秒),仅当调用方 ctx 无 deadline 时生效,<=0 表示复用底层默认 8s。

type Option added in v1.2.6

type Option func(*RestApi)

Option 配置 RestApi,遵循 GoOne functional options 惯例(见 http_client.NewClient)。

func WithHTTPClient added in v1.2.6

func WithHTTPClient(c *apiclient.Client) Option

WithHTTPClient 注入自定义底层客户端,主要用于测试或共享特殊传输层。 传 nil 视作未设置,保留 NewClient(nil) 默认。

func WithTimeout added in v1.2.6

func WithTimeout(d time.Duration) Option

WithTimeout 设置兜底超时;仅当调用方 ctx 无 deadline 时生效。 d<=0 视作未设置,复用底层 Client 的默认超时。

type RestApi

type RestApi struct {
	// contains filtered or unexported fields
}

RestApi 是带可选签名与多后端分片的轻量出站 HTTP 客户端。

设计要点:

  • 底层统一委托 lib/api/http_client.Client.DoRequest,天然获得 context 感知、 TLS 严格校验、响应体上限、非 2xx 错误处理;不再走 lib/web/http_client 垫层;
  • 多 URL 采用 uin 取模分片,uin==0 时 atomic 自增轮询,并发安全且确定;
  • 签名链路复用 http_sign:对调用方 query map 做防御性 clone,避免就地改写;
  • 超时分层:ctx 自带 deadline 优先 → 实例 Timeout > 0 → 底层默认 8s。

func NewRestApi added in v1.2.6

func NewRestApi(conf Config, signs *http_sign.SignMgr, opts ...Option) *RestApi

NewRestApi 依据 Config 构造 RestApi 实例。

必填:ServiceName 非空且 Urls 至少一项,否则返回 nil(由调用方决定是否报警)。 signs 非 nil 且 conf.SignName 非空时,按名解析 HttpSign;解析不到则该实例不签名。

func (*RestApi) Get

func (r *RestApi) Get(ctx context.Context, uin int64, query map[string]string) ([]byte, error)

Get 发起普通 GET 请求,query 序列化进 URL;不做签名。

func (*RestApi) Post

func (r *RestApi) Post(ctx context.Context, uin int64, body []byte, headers map[string]string) ([]byte, error)

Post 发起普通 POST 请求;body 为原始字节(已是 JSON 序列化结果),headers 可为 nil。

func (*RestApi) SignedGet added in v1.2.6

func (r *RestApi) SignedGet(ctx context.Context, uin int64, query map[string]string) ([]byte, error)

SignedGet 发起带签名的 GET 请求:签名写入 query string(与历史报文格式一致)。

func (*RestApi) SignedPost added in v1.2.6

func (r *RestApi) SignedPost(ctx context.Context, uin int64, query map[string]string, body []byte, headers map[string]string) ([]byte, error)

SignedPost 发起带签名的 POST 请求:

  • query 走 query string 并参与签名;
  • body 既作为签名内容也作为请求体;
  • headers 透传(如 Authorization、Content-Type),不参与签名。

query 会做防御性 clone,调用方原始 map 不被就地改写。

type RestApiMgr

type RestApiMgr struct {
	// contains filtered or unexported fields
}

RestApiMgr 是按服务名注册的 RestApi 实例表,并发安全。

func NewRestApiMgr

func NewRestApiMgr() *RestApiMgr

NewRestApiMgr 返回一个空的 RestApiMgr。

func (*RestApiMgr) Count

func (m *RestApiMgr) Count() int

Count 返回已注册的实例数量。

func (*RestApiMgr) GetRestIns

func (m *RestApiMgr) GetRestIns(keys ...string) *RestApi

GetRestIns 返回指定 key 的实例;未传 key 时返回 "default" 实例。 不存在时返回 nil。

func (*RestApiMgr) Init

func (m *RestApiMgr) Init(cfgs []Config, signs *http_sign.SignMgr)

Init 为每个 Config 项构建并注册一个 RestApi 实例, 并从 signs 解析其可选的 HttpSign 依赖。

构造失败的配置项(缺 ServiceName 或 Urls)会被跳过并以 Warning 上报, 避免线上因静默丢弃而难以排查。

func (*RestApiMgr) SetRestIns

func (m *RestApiMgr) SetRestIns(key string, impl *RestApi)

SetRestIns 在 key 下注册(或替换)一个 RestApi 实例。 key 为空或 impl 为 nil 时为空操作。

Jump to

Keyboard shortcuts

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