pcsg_bloomberg.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package index_data
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "eta/eta_bridge/global"
  6. "eta/eta_bridge/models/pcsg"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. )
  11. var (
  12. PCSGBloombergPythonApiDaily = "/api/bloomberg/daily_data"
  13. PCSGBloombergPythonApiWeekly = "/api/bloomberg/weekly_data"
  14. PCSGBloombergPythonApiMonthly = "/api/bloomberg/monthly_data"
  15. )
  16. // GetPCSGBloombergDailyIndex 获取彭博日度指标
  17. func GetPCSGBloombergDailyIndex() (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
  18. apiData, e := CurlPCSGBloombergDailyApi()
  19. if e != nil {
  20. err = fmt.Errorf("CurlPCSGBloombergDailyApi err: %s", e.Error())
  21. return
  22. }
  23. if len(apiData) == 0 {
  24. return
  25. }
  26. frequency := "日度"
  27. for _, v := range apiData {
  28. t := pcsg.FormatPythonBloombergGeneralData2Base(v, frequency)
  29. if t.IndexCode != "" {
  30. indexes = append(indexes, t)
  31. }
  32. }
  33. return
  34. }
  35. // GetPCSGBloombergWeeklyIndex 获取彭博周度指标
  36. func GetPCSGBloombergWeeklyIndex() (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
  37. apiData, e := CurlPCSGBloombergWeeklyApi()
  38. if e != nil {
  39. err = fmt.Errorf("GetPCSGBloombergWeeklyIndex err: %s", e.Error())
  40. return
  41. }
  42. if len(apiData) == 0 {
  43. return
  44. }
  45. frequency := "周度"
  46. for _, v := range apiData {
  47. t := pcsg.FormatPythonBloombergGeneralData2Base(v, frequency)
  48. if t.IndexCode != "" {
  49. indexes = append(indexes, t)
  50. }
  51. }
  52. return
  53. }
  54. // GetPCSGBloombergMonthlyIndex 获取彭博月度指标
  55. func GetPCSGBloombergMonthlyIndex() (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
  56. apiData, e := CurlPCSGBloombergMonthlyApi()
  57. if e != nil {
  58. err = fmt.Errorf("GetPCSGBloombergMonthlyIndex err: %s", e.Error())
  59. return
  60. }
  61. if len(apiData) == 0 {
  62. return
  63. }
  64. frequency := "月度"
  65. for _, v := range apiData {
  66. t := pcsg.FormatPythonBloombergGeneralData2Base(v, frequency)
  67. if t.IndexCode != "" {
  68. indexes = append(indexes, t)
  69. }
  70. }
  71. return
  72. }
  73. // CurlPCSGBloombergDailyApi 请求日度指标接口
  74. func CurlPCSGBloombergDailyApi() (resultData []pcsg.PythonBloombergGeneralData, err error) {
  75. if global.CONFIG.PCSG.BloombergApiUrl == "" {
  76. err = fmt.Errorf("服务地址为空")
  77. return
  78. }
  79. url := fmt.Sprint(global.CONFIG.PCSG.BloombergApiUrl, PCSGBloombergPythonApiDaily)
  80. resp, e := http.Post(url, "application/json", bytes.NewBuffer([]byte("")))
  81. if e != nil {
  82. err = fmt.Errorf("http post err: %s", e.Error())
  83. return
  84. }
  85. defer resp.Body.Close()
  86. b, e := ioutil.ReadAll(resp.Body)
  87. if e != nil {
  88. err = fmt.Errorf("resp body read err: %s", e.Error())
  89. return
  90. }
  91. if len(b) == 0 {
  92. err = fmt.Errorf("resp body is empty")
  93. return
  94. }
  95. result := new(pcsg.PythonBloombergGeneralResult)
  96. if e = json.Unmarshal(b, &result); e != nil {
  97. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  98. return
  99. }
  100. if result.Code != 200 {
  101. err = fmt.Errorf("result: %s", string(b))
  102. return
  103. }
  104. resultData = result.Data
  105. return
  106. }
  107. // CurlPCSGBloombergWeeklyApi 请求周度指标接口
  108. func CurlPCSGBloombergWeeklyApi() (resultData []pcsg.PythonBloombergGeneralData, err error) {
  109. if global.CONFIG.PCSG.BloombergApiUrl == "" {
  110. err = fmt.Errorf("服务地址为空")
  111. return
  112. }
  113. url := fmt.Sprint(global.CONFIG.PCSG.BloombergApiUrl, PCSGBloombergPythonApiWeekly)
  114. resp, e := http.Post(url, "application/json", bytes.NewBuffer([]byte("")))
  115. if e != nil {
  116. err = fmt.Errorf("http post err: %s", e.Error())
  117. return
  118. }
  119. defer resp.Body.Close()
  120. b, e := ioutil.ReadAll(resp.Body)
  121. if e != nil {
  122. err = fmt.Errorf("resp body read err: %s", e.Error())
  123. return
  124. }
  125. if len(b) == 0 {
  126. err = fmt.Errorf("resp body is empty")
  127. return
  128. }
  129. result := new(pcsg.PythonBloombergGeneralResult)
  130. if e := json.Unmarshal(b, &result); e != nil {
  131. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  132. return
  133. }
  134. if result.Code != 200 {
  135. err = fmt.Errorf("result: %s", string(b))
  136. return
  137. }
  138. resultData = result.Data
  139. return
  140. }
  141. // CurlPCSGBloombergMonthlyApi 请求月度指标接口
  142. func CurlPCSGBloombergMonthlyApi() (resultData []pcsg.PythonBloombergGeneralData, err error) {
  143. if global.CONFIG.PCSG.BloombergApiUrl == "" {
  144. err = fmt.Errorf("服务地址为空")
  145. return
  146. }
  147. url := fmt.Sprint(global.CONFIG.PCSG.BloombergApiUrl, PCSGBloombergPythonApiMonthly)
  148. resp, e := http.Post(url, "application/json", bytes.NewBuffer([]byte("")))
  149. if e != nil {
  150. err = fmt.Errorf("http post err: %s", e.Error())
  151. return
  152. }
  153. defer resp.Body.Close()
  154. b, e := ioutil.ReadAll(resp.Body)
  155. if e != nil {
  156. err = fmt.Errorf("resp body read err: %s", e.Error())
  157. return
  158. }
  159. if len(b) == 0 {
  160. err = fmt.Errorf("resp body is empty")
  161. return
  162. }
  163. result := new(pcsg.PythonBloombergGeneralResult)
  164. if e = json.Unmarshal(b, &result); e != nil {
  165. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  166. return
  167. }
  168. if result.Code != 200 {
  169. err = fmt.Errorf("result: %s", string(b))
  170. return
  171. }
  172. resultData = result.Data
  173. return
  174. }