f
This commit is contained in:
@@ -58,19 +58,21 @@ type ApiCallOptions struct {
|
||||
|
||||
// Client 天元API客户端
|
||||
type Client struct {
|
||||
accessID string
|
||||
key string
|
||||
baseURL string
|
||||
timeout time.Duration
|
||||
client *http.Client
|
||||
accessID string
|
||||
key string
|
||||
baseURL string
|
||||
whitelistMgmtKey string
|
||||
timeout time.Duration
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// Config 客户端配置
|
||||
type Config struct {
|
||||
AccessID string // 访问ID
|
||||
Key string // AES密钥(16进制)
|
||||
BaseURL string // API基础URL
|
||||
Timeout time.Duration // 超时时间
|
||||
AccessID string // 访问ID
|
||||
Key string // AES密钥(16进制)
|
||||
BaseURL string // API基础URL
|
||||
WhitelistMgmtKey string // 查询白名单管理密钥
|
||||
Timeout time.Duration // 超时时间
|
||||
}
|
||||
|
||||
// Request 请求参数
|
||||
@@ -122,10 +124,11 @@ func NewClient(config Config) (*Client, error) {
|
||||
}
|
||||
|
||||
return &Client{
|
||||
accessID: config.AccessID,
|
||||
key: config.Key,
|
||||
baseURL: config.BaseURL,
|
||||
timeout: config.Timeout,
|
||||
accessID: config.AccessID,
|
||||
key: config.Key,
|
||||
baseURL: config.BaseURL,
|
||||
whitelistMgmtKey: config.WhitelistMgmtKey,
|
||||
timeout: config.Timeout,
|
||||
client: &http.Client{
|
||||
Timeout: config.Timeout,
|
||||
},
|
||||
|
||||
148
app/main/api/internal/service/tianyuanapi_sdk/query_whitelist.go
Normal file
148
app/main/api/internal/service/tianyuanapi_sdk/query_whitelist.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package tianyuanapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// QueryWhitelistRequest 查询白名单请求参数(加密前明文)
|
||||
type QueryWhitelistRequest struct {
|
||||
Name string `json:"name"`
|
||||
IdCard string `json:"id_card"`
|
||||
ApiCodes []string `json:"api_codes"`
|
||||
Remark string `json:"remark,omitempty"`
|
||||
}
|
||||
|
||||
// QueryWhitelistEntry 查询白名单规则(解密后 data)
|
||||
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 查询白名单接口调用结果(保留天远原始业务码)
|
||||
type QueryWhitelistResult struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
TransactionID string `json:"transaction_id"`
|
||||
Entry *QueryWhitelistEntry `json:"entry,omitempty"`
|
||||
RequestError string `json:"request_error,omitempty"`
|
||||
}
|
||||
|
||||
// CreateQueryWhitelistEntry 创建查询白名单规则
|
||||
func (c *Client) CreateQueryWhitelistEntry(req QueryWhitelistRequest) *QueryWhitelistResult {
|
||||
return c.callQueryWhitelist("query-whitelist/entries", req)
|
||||
}
|
||||
|
||||
// AppendQueryWhitelistEntry 向已有规则追加产品编码
|
||||
func (c *Client) AppendQueryWhitelistEntry(req QueryWhitelistRequest) *QueryWhitelistResult {
|
||||
return c.callQueryWhitelist("query-whitelist/entries/append", req)
|
||||
}
|
||||
|
||||
func (c *Client) callQueryWhitelist(path string, payload QueryWhitelistRequest) *QueryWhitelistResult {
|
||||
if c.whitelistMgmtKey == "" {
|
||||
return &QueryWhitelistResult{
|
||||
Code: 1010,
|
||||
Message: "缺少管理密钥",
|
||||
}
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return &QueryWhitelistResult{
|
||||
Code: 1001,
|
||||
Message: "接口异常",
|
||||
RequestError: fmt.Sprintf("参数序列化失败: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
encryptedData, err := c.encrypt(string(jsonData))
|
||||
if err != nil {
|
||||
return &QueryWhitelistResult{
|
||||
Code: 1001,
|
||||
Message: "接口异常",
|
||||
RequestError: fmt.Sprintf("数据加密失败: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
requestBody := map[string]interface{}{
|
||||
"data": encryptedData,
|
||||
}
|
||||
requestBodyBytes, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return &QueryWhitelistResult{
|
||||
Code: 1001,
|
||||
Message: "接口异常",
|
||||
RequestError: fmt.Sprintf("请求体序列化失败: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/api/v1/%s", c.baseURL, path)
|
||||
httpReq, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBodyBytes))
|
||||
if err != nil {
|
||||
return &QueryWhitelistResult{
|
||||
Code: 1001,
|
||||
Message: "接口异常",
|
||||
RequestError: fmt.Sprintf("创建HTTP请求失败: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Access-Id", c.accessID)
|
||||
httpReq.Header.Set("Whitelist-Mgmt-Key", c.whitelistMgmtKey)
|
||||
httpReq.Header.Set("User-Agent", "TianyuanAPI-Go-SDK/1.0.0")
|
||||
|
||||
resp, err := c.client.Do(httpReq)
|
||||
if err != nil {
|
||||
return &QueryWhitelistResult{
|
||||
Code: 1001,
|
||||
Message: "接口异常",
|
||||
RequestError: err.Error(),
|
||||
}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return &QueryWhitelistResult{
|
||||
Code: 1001,
|
||||
Message: "接口异常",
|
||||
RequestError: fmt.Sprintf("读取响应失败: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
var apiResp ApiResponse
|
||||
if err := json.Unmarshal(body, &apiResp); err != nil {
|
||||
return &QueryWhitelistResult{
|
||||
Code: 1001,
|
||||
Message: "接口异常",
|
||||
RequestError: fmt.Sprintf("响应解析失败: %v", err),
|
||||
}
|
||||
}
|
||||
|
||||
result := &QueryWhitelistResult{
|
||||
Code: apiResp.Code,
|
||||
Message: apiResp.Message,
|
||||
TransactionID: apiResp.TransactionID,
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user