96b1edff45
iotlcm-upstream-chan:运营商上游通道适配服务(统一 /sim/* 协议)。 - 内联原私有依赖:yunze-public → internal/pub/(10 包 18 文件), yunze-pb/pb → internal/pb/(9 文件);Consul 注册改用官方 gogf/gf/contrib/registry/consul/v2(API 兼容) - Go module:yz-svr → git.5iot.com/iotlcm-public/iotlcm-upstream-chan - 修复 mingqian 包测试:实现重构后 `adapter.operations` 字段已移除, 测试断言按当前实现语义重写(不改产品行为) - 凭据与内部标识脱敏:MySQL/Redis/Consul 凭据、私网与生产 IP、 内网域名、真实手机号、注释里的真实 token/transid - 移除内部资产:CI 触发器、生产通道种子 SQL、真实凭据测试资源文档 - Dockerfile 改用公共基础镜像,移除写死的 registry 凭据 - 补 Apache-2.0 LICENSE,重写 README,LF 行尾 + .gitattributes
105 lines
2.8 KiB
Go
105 lines
2.8 KiB
Go
package yunda
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
BaseURL string
|
|
AppKey string
|
|
AppSecret string
|
|
Timeout time.Duration
|
|
}
|
|
type Client struct {
|
|
baseURL string
|
|
appKey string
|
|
appSecret string
|
|
httpClient *http.Client
|
|
}
|
|
type Response struct {
|
|
Status json.RawMessage `json:"status"`
|
|
Message string `json:"message"`
|
|
Result json.RawMessage `json:"result"`
|
|
}
|
|
|
|
func NewClient(cfg Config) *Client {
|
|
timeout := cfg.Timeout
|
|
if timeout <= 0 {
|
|
timeout = 10 * time.Second
|
|
}
|
|
return &Client{baseURL: strings.TrimRight(strings.TrimSpace(cfg.BaseURL), "/"), appKey: strings.TrimSpace(cfg.AppKey), appSecret: cfg.AppSecret, httpClient: &http.Client{Timeout: timeout}}
|
|
}
|
|
|
|
func (c *Client) Post(ctx context.Context, path string, params map[string]string, out any) (Response, error) {
|
|
if c.baseURL == "" {
|
|
return Response{}, errors.New("yunda base URL is required")
|
|
}
|
|
if c.appKey == "" || c.appSecret == "" {
|
|
return Response{}, errors.New("yunda appKey and appSecret are required")
|
|
}
|
|
signed := make(map[string]string, len(params)+3)
|
|
for key, value := range params {
|
|
if strings.TrimSpace(value) != "" {
|
|
signed[key] = value
|
|
}
|
|
}
|
|
signed["appKey"] = c.appKey
|
|
signed["timestamp"] = strconv.FormatInt(time.Now().Unix(), 10)
|
|
signed["sign"] = BuildSign(c.appKey, c.appSecret, signed)
|
|
form := url.Values{}
|
|
for key, value := range signed {
|
|
form.Set(key, value)
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/"+strings.TrimLeft(path, "/"), strings.NewReader(form.Encode()))
|
|
if err != nil {
|
|
return Response{}, err
|
|
}
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return Response{}, err
|
|
}
|
|
defer resp.Body.Close()
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return Response{}, err
|
|
}
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
return Response{}, fmt.Errorf("yunda HTTP status %d: %s", resp.StatusCode, strings.TrimSpace(string(body)))
|
|
}
|
|
var response Response
|
|
if err = json.Unmarshal(body, &response); err != nil {
|
|
return Response{}, fmt.Errorf("decode yunda response: %w", err)
|
|
}
|
|
if status := response.StatusString(); status != "0" && status != "" {
|
|
return response, fmt.Errorf("yunda business status %s: %s", status, response.Message)
|
|
}
|
|
if out != nil && len(response.Result) > 0 && string(response.Result) != "null" {
|
|
if err = json.Unmarshal(response.Result, out); err != nil {
|
|
return response, fmt.Errorf("decode yunda result: %w", err)
|
|
}
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
func (r Response) StatusString() string {
|
|
var value string
|
|
if json.Unmarshal(r.Status, &value) == nil {
|
|
return value
|
|
}
|
|
var number json.Number
|
|
if json.Unmarshal(r.Status, &number) == nil {
|
|
return number.String()
|
|
}
|
|
return ""
|
|
}
|