You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

resty.go 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package aisafety
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "net/url"
  8. "sort"
  9. "strings"
  10. "code.gitea.io/gitea/modules/log"
  11. "github.com/go-resty/resty/v2"
  12. )
  13. var (
  14. restyClient *resty.Client
  15. HOST string
  16. KEY string
  17. )
  18. type TaskReq struct {
  19. UnionId string //评测任务ID,唯一标识,由后端生成UUID
  20. EvalName string //评测任务名称
  21. EvalContent string //评测任务描述
  22. TLPath string //
  23. Indicators []string //评测指标,由GetAlgorithmList接口返回的指标列表中的title属性值
  24. CDPath string
  25. CDName string //对抗数据集名称
  26. BDPath string
  27. BDName string //原数据集名称
  28. }
  29. const (
  30. APPID = "1"
  31. LogPageSize = 500
  32. LogPageTokenExpired = "5m"
  33. pageSize = 15
  34. Success = "S000"
  35. )
  36. func getRestyClient() *resty.Client {
  37. if restyClient == nil {
  38. restyClient = resty.New()
  39. }
  40. return restyClient
  41. }
  42. func checkSetting() {
  43. if len(HOST) != 0 && len(KEY) != 0 {
  44. return
  45. }
  46. _ = loginCloudbrain()
  47. }
  48. func loginCloudbrain() error {
  49. HOST = "http://221.122.70.196:8081/atp-api"
  50. KEY = "1"
  51. return nil
  52. }
  53. func createSign(params map[string]interface{}, signKey string) string {
  54. var sceneList []string
  55. for k := range params {
  56. sceneList = append(sceneList, k)
  57. }
  58. sort.Strings(sceneList)
  59. re := ""
  60. for _, key := range sceneList {
  61. if params[key] != nil {
  62. re += key + "=" + fmt.Sprint(params[key]) + "&"
  63. }
  64. }
  65. re += "key=" + signKey
  66. log.Info("sign key:" + re)
  67. h := md5.New()
  68. h.Write([]byte(re))
  69. return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
  70. }
  71. func getParams(req TaskReq) (map[string]interface{}, string) {
  72. params := make(map[string]interface{})
  73. reStr := ""
  74. params["unionId"] = req.UnionId
  75. reStr += "unionId=" + req.UnionId
  76. params["evalName"] = req.EvalName
  77. reStr += "&evalName=" + req.EvalName
  78. params["evalContent"] = req.EvalContent
  79. reStr += "&evalContent=" + url.QueryEscape(req.EvalContent)
  80. params["TLPath"] = req.TLPath
  81. reStr += "&TLPath=" + url.QueryEscape(req.TLPath)
  82. params["CDName"] = req.CDName
  83. reStr += "&CDName=" + url.QueryEscape(req.CDName)
  84. params["BDName"] = req.BDName
  85. reStr += "&BDName=" + url.QueryEscape(req.BDName)
  86. if req.CDPath != "" {
  87. params["CDPath"] = req.CDPath
  88. reStr += "&CDPath=" + url.QueryEscape(req.CDPath)
  89. }
  90. if req.BDPath != "" {
  91. params["BDPath"] = req.BDPath
  92. reStr += "&BDPath=" + url.QueryEscape(req.BDPath)
  93. }
  94. indicators := ""
  95. if len(req.Indicators) > 0 {
  96. for _, tmp := range req.Indicators {
  97. indicators += tmp + ","
  98. }
  99. }
  100. if len(indicators) > 0 {
  101. indicators = indicators[0 : len(indicators)-1]
  102. }
  103. params["Indicators"] = indicators
  104. reStr += "&Indicators=" + url.QueryEscape(indicators)
  105. return params, reStr
  106. }
  107. func CreateSafetyTask(req TaskReq, json string) (string, error) {
  108. checkSetting()
  109. client := getRestyClient()
  110. //reqPara, _ := json.Marshal(body)
  111. log.Warn("job req:", json)
  112. params, urlQuerys := getParams(req)
  113. bodyMap := make(map[string]interface{})
  114. reJsonMap := make(map[string]interface{})
  115. reJsonMap["resJson"] = json
  116. bodyMap["externalEvalParam"] = reJsonMap
  117. res, err := client.R().
  118. SetHeader("Content-Type", "application/json").
  119. SetHeader("appId", APPID).
  120. SetHeader("sign", createSign(params, KEY)).
  121. //SetAuthToken(TOKEN).
  122. SetBody(bodyMap).
  123. Post(HOST + "/v1/external/eval-standard/create?" + urlQuerys)
  124. log.Info("url=" + HOST + "/v1/external/eval-standard/create?" + urlQuerys)
  125. responseStr := string(res.Body())
  126. log.Info("CreateSafetyTask responseStr=" + responseStr + " res code=" + fmt.Sprint(res.StatusCode()))
  127. if err != nil {
  128. return "", fmt.Errorf("resty create job: %s", err)
  129. } else {
  130. log.Info("result string=" + " res code=" + fmt.Sprint(res.StatusCode()))
  131. }
  132. return "", nil
  133. }
  134. func GetAlgorithmList() error {
  135. checkSetting()
  136. client := getRestyClient()
  137. params := make(map[string]interface{})
  138. jsonResult := make(map[string]interface{})
  139. sign := createSign(params, KEY)
  140. res, err := client.R().
  141. SetHeader("Content-Type", "application/json").
  142. SetHeader("appId", APPID).
  143. SetHeader("sign", sign).
  144. Get(HOST + "/v1/external/eval-standard/algorithmList")
  145. log.Info("url=" + HOST + "/v1/external/eval-standard/algorithmList" + " sign=" + sign + " appId=" + APPID)
  146. jsonerr := json.Unmarshal(res.Body(), &jsonResult)
  147. if jsonerr == nil {
  148. log.Info("jsonResult code=" + fmt.Sprint(jsonResult["msg"]))
  149. }
  150. responseStr := string(res.Body())
  151. log.Info("GetAlgorithmList responseStr=" + responseStr + " res code=" + fmt.Sprint(res.StatusCode()))
  152. if err != nil {
  153. log.Info("error =" + err.Error())
  154. return fmt.Errorf("resty GetJob: %v", err)
  155. }
  156. return nil
  157. }
  158. func GetTaskStatus(jobID string) error {
  159. checkSetting()
  160. client := getRestyClient()
  161. var taskResult string
  162. params := make(map[string]interface{})
  163. params["serialNo"] = jobID
  164. res, err := client.R().
  165. SetHeader("Content-Type", "application/json").
  166. SetHeader("appId", APPID).
  167. SetHeader("sign", createSign(params, KEY)).
  168. SetResult(&taskResult).
  169. Get(HOST + "/v1/external/eval-standard/query?serialNo=" + jobID)
  170. log.Info("url=" + HOST + "/v1/external/eval-standard/query?serialNo=" + jobID)
  171. responseStr := string(res.Body())
  172. log.Info("GetTaskStatus responseStr=" + responseStr + " res code=" + fmt.Sprint(res.StatusCode()))
  173. if err != nil {
  174. log.Info("error =" + err.Error())
  175. return fmt.Errorf("resty GetJob: %v", err)
  176. }
  177. return nil
  178. }