chore(discover): release v1.0.8 with Call[T] generic support and config improvements (by AI)

This commit is contained in:
AI Engineer 2026-05-09 17:31:39 +08:00
parent 18fdafe09f
commit 5de04b4a63
3 changed files with 22 additions and 0 deletions

View File

@ -88,6 +88,22 @@ func (c *Caller) Head(app, path string, headers ...string) *gohttp.Result {
return c.Do("HEAD", app, path, nil, headers...) return c.Do("HEAD", app, path, nil, headers...)
} }
// Call 发起通用的泛型请求并自动解析响应
func Call[T any](method, app, path string, data any, headers ...string) (T, error) {
return CallT[T](DefaultDiscoverer.NewCaller(nil, nil), method, app, path, data, headers...)
}
// CallT 发起泛型请求并自动解析响应 (由于 Go 方法不支持泛型,故使用函数)
func CallT[T any](c *Caller, method, app, path string, data any, headers ...string) (T, error) {
var result T
res := c.Do(method, app, path, data, headers...)
if res.Error != nil {
return result, res.Error
}
err := res.To(&result)
return result, err
}
// Do 发起通用请求 // Do 发起通用请求
func (c *Caller) Do(method, app, path string, data any, headers ...string) *gohttp.Result { func (c *Caller) Do(method, app, path string, data any, headers ...string) *gohttp.Result {
r, _ := c.DoWithNode(method, app, "", path, data, headers...) r, _ := c.DoWithNode(method, app, "", path, data, headers...)

View File

@ -129,6 +129,10 @@ func (d *Discoverer) Init() {
SetConfig(conf) // 保持全局 Config 变量同步 SetConfig(conf) // 保持全局 Config 变量同步
} }
if conf.App == "" {
conf.App = os.Getenv("DISCOVER_APP")
}
if conf.CallRetryTimes <= 0 { if conf.CallRetryTimes <= 0 {
conf.CallRetryTimes = 10 conf.CallRetryTimes = 10
} }

View File

@ -28,6 +28,8 @@ discover:
### 服务调用 (Caller) ### 服务调用 (Caller)
- `NewCaller(request *http.Request, logger *log.Logger) *Caller`: 创建调用器。传入原始请求可自动透传 Header。 - `NewCaller(request *http.Request, logger *log.Logger) *Caller`: 创建调用器。传入原始请求可自动透传 Header。
- `Call[T](method, app, path, data, headers...) (T, error)`: **[推荐]** 泛型快捷调用,自动解析 JSON 结果。
- `CallT[T](caller, ...) (T, error)`: 针对指定调用器的泛型调用。
- `Caller.Get / Post / Put / Delete / Head`: 发起同步请求。 - `Caller.Get / Post / Put / Delete / Head`: 发起同步请求。
- `Caller.Do(method, app, path, data, headers...)`: 发起通用请求,返回 `http.Result` - `Caller.Do(method, app, path, data, headers...)`: 发起通用请求,返回 `http.Result`
- `Caller.Open(app, path, headers...)`: 发起 WebSocket 连接。 - `Caller.Open(app, path, headers...)`: 发起 WebSocket 连接。