htService.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_mini_crm_ht/utils"
  6. httpClient "eta/eta_mini_crm_ht/utils/http"
  7. "fmt"
  8. "io"
  9. )
  10. const (
  11. GetIndexInfoUrl = "/htApi/v1/index/getIndexInfo"
  12. )
  13. type IndexInfo struct {
  14. IndexCode string `json:"indexCode"`
  15. IndexName string `json:"indexName"`
  16. Frequency string `json:"frequency"`
  17. SourceName string `json:"sourceName"`
  18. LatestDataDate string `json:"latestDataDate"`
  19. LastPushTime string `json:"lastPushTime"`
  20. Remark string `json:"remark"`
  21. Unit string `json:"unit"`
  22. DataList []interface{} `json:"dataList"`
  23. }
  24. type HTResponse struct {
  25. Code string `json:"code"`
  26. Success bool `json:"success"`
  27. Message string `json:"message"`
  28. Data json.RawMessage `json:"data"`
  29. }
  30. func GetIndexInfo(indexCode string) (indexInfo *IndexInfo, err error) {
  31. client := httpClient.DefaultClient()
  32. if utils.HT_ADDRESS == "" {
  33. err = errors.New("未配置海通钢联地址")
  34. return
  35. }
  36. url := fmt.Sprintf("%s%s?indexCode=%s", utils.HT_ADDRESS, GetIndexInfoUrl, indexCode)
  37. resp, err := client.Get(url)
  38. if err != nil {
  39. utils.FileLog.Error("获取钢联指标信息失败", err.Error())
  40. return
  41. }
  42. defer func(Body io.ReadCloser) {
  43. closeErr := Body.Close()
  44. if closeErr != nil {
  45. utils.FileLog.Error("关闭Response失败:%v", closeErr)
  46. }
  47. }(resp.Body)
  48. body, _ := io.ReadAll(resp.Body)
  49. var htRes HTResponse
  50. err = json.Unmarshal(body, &htRes)
  51. if err != nil {
  52. utils.FileLog.Error("解析应答信息失败:%v", err)
  53. return
  54. }
  55. if htRes.Success {
  56. err = json.Unmarshal(htRes.Data, &indexInfo)
  57. } else {
  58. err = errors.New(htRes.Message)
  59. }
  60. return
  61. }