68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package warehouse_search
|
||
|
||
import (
|
||
"ai_scheduler/internal/config"
|
||
"ai_scheduler/internal/pkg/l_request"
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
)
|
||
|
||
type Client struct {
|
||
cfg config.ToolConfig
|
||
}
|
||
|
||
func New(cfg config.ToolConfig) *Client {
|
||
return &Client{
|
||
cfg: cfg,
|
||
}
|
||
}
|
||
|
||
func (c *Client) Call(ctx context.Context, name string) (int, error) {
|
||
if name == "" {
|
||
// 如果没有仓库名,返回0,不报错,由上层业务决定是否允许
|
||
return 0, nil
|
||
}
|
||
|
||
// GET 请求参数
|
||
params := map[string]string{
|
||
"name": name,
|
||
"page": "1",
|
||
"limit": "1",
|
||
}
|
||
|
||
req := l_request.Request{
|
||
Method: "Get",
|
||
Url: c.cfg.BaseURL,
|
||
Params: params,
|
||
Headers: map[string]string{
|
||
"User-Agent": "Apifox/1.0.0 (https://apifox.com)",
|
||
"Content-Type": "application/json",
|
||
},
|
||
}
|
||
|
||
res, err := req.Send()
|
||
if err != nil {
|
||
return 0, err
|
||
}
|
||
|
||
if res.StatusCode != 200 {
|
||
return 0, fmt.Errorf("warehouse search failed with status code: %d", res.StatusCode)
|
||
}
|
||
|
||
var resData SearchResponse
|
||
if err := json.Unmarshal([]byte(res.Text), &resData); err != nil {
|
||
return 0, fmt.Errorf("failed to parse warehouse search response: %w", err)
|
||
}
|
||
|
||
if resData.Code != 200 {
|
||
return 0, fmt.Errorf("warehouse search business error: %s", resData.Msg)
|
||
}
|
||
|
||
if len(resData.Data.List) == 0 {
|
||
return 0, fmt.Errorf("warehouse not found: %s", name)
|
||
}
|
||
|
||
return resData.Data.List[0].ID, nil
|
||
}
|