fadd
This commit is contained in:
105
app/main/api/internal/service/tianyuanapi_sdk/query_whitelist.go
Normal file
105
app/main/api/internal/service/tianyuanapi_sdk/query_whitelist.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package tianyuanapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
// QueryWhitelistMgmtResponse 查询白名单管理接口响应
|
||||
type QueryWhitelistMgmtResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
TransactionID string `json:"transaction_id"`
|
||||
Entry *QueryWhitelistEntry `json:"entry,omitempty"`
|
||||
Timeout int64 `json:"timeout"`
|
||||
}
|
||||
|
||||
// CallQueryWhitelistMgmt 调用查询白名单管理接口(创建/追加)
|
||||
func (c *Client) CallQueryWhitelistMgmt(apiPath string, params map[string]interface{}, mgmtKey string) (*QueryWhitelistMgmtResponse, error) {
|
||||
startTime := time.Now()
|
||||
|
||||
if mgmtKey == "" {
|
||||
return nil, fmt.Errorf("Whitelist-Mgmt-Key 不能为空")
|
||||
}
|
||||
if params == nil {
|
||||
return nil, fmt.Errorf("params不能为空")
|
||||
}
|
||||
|
||||
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 + apiPath
|
||||
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", 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 := &QueryWhitelistMgmtResponse{
|
||||
Code: apiResp.Code,
|
||||
Message: apiResp.Message,
|
||||
TransactionID: apiResp.TransactionID,
|
||||
Timeout: time.Since(startTime).Milliseconds(),
|
||||
}
|
||||
|
||||
if apiResp.Data != "" {
|
||||
decryptedData, decryptErr := c.decrypt(apiResp.Data)
|
||||
if decryptErr == nil {
|
||||
var entry QueryWhitelistEntry
|
||||
if json.Unmarshal([]byte(decryptedData), &entry) == nil {
|
||||
result.Entry = &entry
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user