addf
This commit is contained in:
@@ -63,6 +63,7 @@ type Client struct {
|
||||
baseURL string
|
||||
timeout time.Duration
|
||||
client *http.Client
|
||||
mgmtKey string // 查询白名单管理密钥 Whitelist-Mgmt-Key
|
||||
}
|
||||
|
||||
// Config 客户端配置
|
||||
|
||||
124
app/main/api/internal/service/tianyuanapi_sdk/query_whitelist.go
Normal file
124
app/main/api/internal/service/tianyuanapi_sdk/query_whitelist.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package tianyuanapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// QueryWhitelistParams 查询白名单请求参数(加密前明文)
|
||||
type QueryWhitelistParams struct {
|
||||
Name string `json:"name"`
|
||||
IdCard string `json:"id_card"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Remark string `json:"remark,omitempty"`
|
||||
}
|
||||
|
||||
// QueryWhitelistEntry 查询白名单规则详情(解密后)
|
||||
type QueryWhitelistEntry struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
IdCardMasked string `json:"id_card_masked"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Status string `json:"status"`
|
||||
Remark string `json:"remark"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// QueryWhitelistResult 查询白名单 API 调用结果
|
||||
type QueryWhitelistResult struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
TransactionID string `json:"transaction_id"`
|
||||
Entry *QueryWhitelistEntry `json:"entry,omitempty"`
|
||||
RequestTimeMs int64 `json:"request_time_ms"`
|
||||
}
|
||||
|
||||
// SetWhitelistMgmtKey 设置白名单管理密钥
|
||||
func (c *Client) SetWhitelistMgmtKey(key string) {
|
||||
c.mgmtKey = key
|
||||
}
|
||||
|
||||
// CreateQueryWhitelistEntry 创建查询白名单规则
|
||||
func (c *Client) CreateQueryWhitelistEntry(params QueryWhitelistParams) (*QueryWhitelistResult, error) {
|
||||
return c.callQueryWhitelist("/api/v1/query-whitelist/entries", params)
|
||||
}
|
||||
|
||||
// AppendQueryWhitelistEntry 向已有规则追加产品编码
|
||||
func (c *Client) AppendQueryWhitelistEntry(params QueryWhitelistParams) (*QueryWhitelistResult, error) {
|
||||
return c.callQueryWhitelist("/api/v1/query-whitelist/entries/append", params)
|
||||
}
|
||||
|
||||
func (c *Client) callQueryWhitelist(path string, params QueryWhitelistParams) (*QueryWhitelistResult, error) {
|
||||
if c.mgmtKey == "" {
|
||||
return nil, fmt.Errorf("Whitelist-Mgmt-Key 未配置")
|
||||
}
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
jsonData, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("参数序列化失败: %v", err)
|
||||
}
|
||||
|
||||
encryptedData, err := c.encrypt(string(jsonData))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("数据加密失败: %v", err)
|
||||
}
|
||||
|
||||
requestBody := map[string]string{"data": encryptedData}
|
||||
requestBodyBytes, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求体序列化失败: %v", err)
|
||||
}
|
||||
|
||||
url := c.baseURL + path
|
||||
httpReq, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(requestBodyBytes))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建HTTP请求失败: %v", err)
|
||||
}
|
||||
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Access-Id", c.accessID)
|
||||
httpReq.Header.Set("Whitelist-Mgmt-Key", c.mgmtKey)
|
||||
httpReq.Header.Set("User-Agent", "TianyuanAPI-Go-SDK/1.0.0")
|
||||
|
||||
resp, err := c.client.Do(httpReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("请求失败: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取响应失败: %v", err)
|
||||
}
|
||||
|
||||
var apiResp ApiResponse
|
||||
if err := json.Unmarshal(body, &apiResp); err != nil {
|
||||
return nil, fmt.Errorf("响应解析失败: %v", err)
|
||||
}
|
||||
|
||||
result := &QueryWhitelistResult{
|
||||
Code: apiResp.Code,
|
||||
Message: apiResp.Message,
|
||||
TransactionID: apiResp.TransactionID,
|
||||
RequestTimeMs: time.Since(startTime).Milliseconds(),
|
||||
}
|
||||
|
||||
if apiResp.Data != "" {
|
||||
decryptedData, err := c.decrypt(apiResp.Data)
|
||||
if err == nil {
|
||||
var entry QueryWhitelistEntry
|
||||
if json.Unmarshal([]byte(decryptedData), &entry) == nil {
|
||||
result.Entry = &entry
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user