pcsg_bloomberg.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package index_data
  2. import (
  3. "encoding/json"
  4. "eta/eta_bridge/global"
  5. "eta/eta_bridge/models/pcsg"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. )
  10. // GetPCSGBloombergGeneralIndex 获取指标信息
  11. func GetPCSGBloombergGeneralIndex(taskKey, frequency string) (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
  12. apiData, e := CurlPCSGBloombergGeneralDataApi(taskKey)
  13. if e != nil {
  14. err = fmt.Errorf("CurlPCSGBloombergDailyApi err: %s", e.Error())
  15. return
  16. }
  17. if len(apiData) == 0 {
  18. return
  19. }
  20. for _, v := range apiData {
  21. t := pcsg.FormatPythonBloombergGeneralData2Base(v, frequency, taskKey)
  22. if t.IndexCode != "" {
  23. indexes = append(indexes, t)
  24. }
  25. }
  26. return
  27. }
  28. // CurlPCSGBloombergGeneralDataApi 请求通用格式数据接口
  29. func CurlPCSGBloombergGeneralDataApi(taskKey string) (resultData []pcsg.PythonBloombergGeneralData, err error) {
  30. if taskKey == "" {
  31. err = fmt.Errorf("任务Key为空")
  32. return
  33. }
  34. if global.CONFIG.PCSG.BloombergApiUrl == "" {
  35. err = fmt.Errorf("服务地址为空")
  36. return
  37. }
  38. url := fmt.Sprintf("%s/api/bloomberg/general_data?task_key=%s", global.CONFIG.PCSG.BloombergApiUrl, taskKey)
  39. resp, e := http.Get(url)
  40. if e != nil {
  41. err = fmt.Errorf("http post err: %s", e.Error())
  42. return
  43. }
  44. defer resp.Body.Close()
  45. b, e := ioutil.ReadAll(resp.Body)
  46. if e != nil {
  47. err = fmt.Errorf("resp body read err: %s", e.Error())
  48. return
  49. }
  50. if len(b) == 0 {
  51. err = fmt.Errorf("resp body is empty")
  52. return
  53. }
  54. //str := `{"Code":200,"Msg":"","Data":[{"DL_REQUEST_ID":"IDpcsgDailySnap0330","DL_REQUEST_NAME":"contractsLastHistory","DL_SNAPSHOT_START_TIME":"2024-07-09T08:30:03","DL_SNAPSHOT_TZ":"JST","IDENTIFIER":"Comdty B001","RC":0,"DATE":"2024-05-09","PX_LAST":1183.88},{"DL_REQUEST_ID":"IDpcsgDailySnap0330","DL_REQUEST_NAME":"contractsLastHistory","DL_SNAPSHOT_START_TIME":"2024-07-10T08:30:03","DL_SNAPSHOT_TZ":"JST","IDENTIFIER":"Comdty B001","RC":0,"DATE":"2024-05-10","PX_LAST":1182.79},{"DL_REQUEST_ID":"IDpcsgDailySnap0330","DL_REQUEST_NAME":"contractsLastHistory","DL_SNAPSHOT_START_TIME":"2024-07-09T08:30:03","DL_SNAPSHOT_TZ":"JST","IDENTIFIER":"Comdty B002","RC":0,"DATE":"2024-05-10","PX_LAST":1162.63}]}`
  55. //b := []byte(str)
  56. result := new(pcsg.PythonBloombergGeneralResult)
  57. if e = json.Unmarshal(b, &result); e != nil {
  58. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  59. return
  60. }
  61. if result.Code != 200 {
  62. err = fmt.Errorf("result: %s", string(b))
  63. return
  64. }
  65. resultData = result.Data
  66. return
  67. }