pcsg_bloomberg.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. PCSGBloombergPythonApiDailyRun3 = "/api/bloomberg/daily_data_run3"
  16. )
  17. // GetPCSGBloombergDailyIndex 获取彭博日度指标
  18. func GetPCSGBloombergDailyIndex() (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
  19. apiData, e := CurlPCSGBloombergDailyApi()
  20. if e != nil {
  21. err = fmt.Errorf("CurlPCSGBloombergDailyApi err: %s", e.Error())
  22. return
  23. }
  24. if len(apiData) == 0 {
  25. return
  26. }
  27. frequency := "日度"
  28. for _, v := range apiData {
  29. t := pcsg.FormatPythonBloombergGeneralData2Base(v, frequency)
  30. if t.IndexCode != "" {
  31. indexes = append(indexes, t)
  32. }
  33. }
  34. return
  35. }
  36. // GetPCSGBloombergWeeklyIndex 获取彭博周度指标
  37. func GetPCSGBloombergWeeklyIndex() (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
  38. apiData, e := CurlPCSGBloombergWeeklyApi()
  39. if e != nil {
  40. err = fmt.Errorf("GetPCSGBloombergWeeklyIndex err: %s", e.Error())
  41. return
  42. }
  43. if len(apiData) == 0 {
  44. return
  45. }
  46. frequency := "周度"
  47. for _, v := range apiData {
  48. t := pcsg.FormatPythonBloombergGeneralData2Base(v, frequency)
  49. if t.IndexCode != "" {
  50. indexes = append(indexes, t)
  51. }
  52. }
  53. return
  54. }
  55. // GetPCSGBloombergMonthlyIndex 获取彭博月度指标
  56. func GetPCSGBloombergMonthlyIndex() (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
  57. apiData, e := CurlPCSGBloombergMonthlyApi()
  58. if e != nil {
  59. err = fmt.Errorf("GetPCSGBloombergMonthlyIndex err: %s", e.Error())
  60. return
  61. }
  62. if len(apiData) == 0 {
  63. return
  64. }
  65. frequency := "月度"
  66. for _, v := range apiData {
  67. t := pcsg.FormatPythonBloombergGeneralData2Base(v, frequency)
  68. if t.IndexCode != "" {
  69. indexes = append(indexes, t)
  70. }
  71. }
  72. return
  73. }
  74. // GetPCSGBloombergDailyIndexRun3 获取彭博日度指标(Run3)
  75. func GetPCSGBloombergDailyIndexRun3() (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
  76. apiData, e := CurlPCSGBloombergDailyRun3Api()
  77. if e != nil {
  78. err = fmt.Errorf("GetPCSGBloombergDailyIndexRun3 err: %s", e.Error())
  79. return
  80. }
  81. if len(apiData) == 0 {
  82. return
  83. }
  84. frequency := "日度"
  85. for _, v := range apiData {
  86. t := pcsg.FormatPythonBloombergGeneralData2Base(v, frequency)
  87. if t.IndexCode != "" {
  88. indexes = append(indexes, t)
  89. }
  90. }
  91. return
  92. }
  93. // CurlPCSGBloombergDailyApi 请求日度指标接口
  94. func CurlPCSGBloombergDailyApi() (resultData []pcsg.PythonBloombergGeneralData, err error) {
  95. if global.CONFIG.PCSG.BloombergApiUrl == "" {
  96. err = fmt.Errorf("服务地址为空")
  97. return
  98. }
  99. url := fmt.Sprint(global.CONFIG.PCSG.BloombergApiUrl, PCSGBloombergPythonApiDaily)
  100. resp, e := http.Post(url, "application/json", bytes.NewBuffer([]byte("")))
  101. if e != nil {
  102. err = fmt.Errorf("http post err: %s", e.Error())
  103. return
  104. }
  105. defer resp.Body.Close()
  106. b, e := ioutil.ReadAll(resp.Body)
  107. if e != nil {
  108. err = fmt.Errorf("resp body read err: %s", e.Error())
  109. return
  110. }
  111. if len(b) == 0 {
  112. err = fmt.Errorf("resp body is empty")
  113. return
  114. }
  115. result := new(pcsg.PythonBloombergGeneralResult)
  116. if e = json.Unmarshal(b, &result); e != nil {
  117. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  118. return
  119. }
  120. if result.Code != 200 {
  121. err = fmt.Errorf("result: %s", string(b))
  122. return
  123. }
  124. resultData = result.Data
  125. return
  126. }
  127. // CurlPCSGBloombergWeeklyApi 请求周度指标接口
  128. func CurlPCSGBloombergWeeklyApi() (resultData []pcsg.PythonBloombergGeneralData, err error) {
  129. if global.CONFIG.PCSG.BloombergApiUrl == "" {
  130. err = fmt.Errorf("服务地址为空")
  131. return
  132. }
  133. url := fmt.Sprint(global.CONFIG.PCSG.BloombergApiUrl, PCSGBloombergPythonApiWeekly)
  134. resp, e := http.Post(url, "application/json", bytes.NewBuffer([]byte("")))
  135. if e != nil {
  136. err = fmt.Errorf("http post err: %s", e.Error())
  137. return
  138. }
  139. defer resp.Body.Close()
  140. b, e := ioutil.ReadAll(resp.Body)
  141. if e != nil {
  142. err = fmt.Errorf("resp body read err: %s", e.Error())
  143. return
  144. }
  145. if len(b) == 0 {
  146. err = fmt.Errorf("resp body is empty")
  147. return
  148. }
  149. result := new(pcsg.PythonBloombergGeneralResult)
  150. if e := json.Unmarshal(b, &result); e != nil {
  151. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  152. return
  153. }
  154. if result.Code != 200 {
  155. err = fmt.Errorf("result: %s", string(b))
  156. return
  157. }
  158. resultData = result.Data
  159. return
  160. }
  161. // CurlPCSGBloombergMonthlyApi 请求月度指标接口
  162. func CurlPCSGBloombergMonthlyApi() (resultData []pcsg.PythonBloombergGeneralData, err error) {
  163. if global.CONFIG.PCSG.BloombergApiUrl == "" {
  164. err = fmt.Errorf("服务地址为空")
  165. return
  166. }
  167. url := fmt.Sprint(global.CONFIG.PCSG.BloombergApiUrl, PCSGBloombergPythonApiMonthly)
  168. resp, e := http.Post(url, "application/json", bytes.NewBuffer([]byte("")))
  169. if e != nil {
  170. err = fmt.Errorf("http post err: %s", e.Error())
  171. return
  172. }
  173. defer resp.Body.Close()
  174. b, e := ioutil.ReadAll(resp.Body)
  175. if e != nil {
  176. err = fmt.Errorf("resp body read err: %s", e.Error())
  177. return
  178. }
  179. if len(b) == 0 {
  180. err = fmt.Errorf("resp body is empty")
  181. return
  182. }
  183. result := new(pcsg.PythonBloombergGeneralResult)
  184. if e = json.Unmarshal(b, &result); e != nil {
  185. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  186. return
  187. }
  188. if result.Code != 200 {
  189. err = fmt.Errorf("result: %s", string(b))
  190. return
  191. }
  192. resultData = result.Data
  193. return
  194. }
  195. // CurlPCSGBloombergDailyRun3Api 请求日度指标(Run3)接口
  196. func CurlPCSGBloombergDailyRun3Api() (resultData []pcsg.PythonBloombergGeneralData, err error) {
  197. if global.CONFIG.PCSG.BloombergApiUrl == "" {
  198. err = fmt.Errorf("服务地址为空")
  199. return
  200. }
  201. url := fmt.Sprint(global.CONFIG.PCSG.BloombergApiUrl, PCSGBloombergPythonApiDailyRun3)
  202. resp, e := http.Post(url, "application/json", bytes.NewBuffer([]byte("")))
  203. if e != nil {
  204. err = fmt.Errorf("http post err: %s", e.Error())
  205. return
  206. }
  207. defer resp.Body.Close()
  208. b, e := ioutil.ReadAll(resp.Body)
  209. if e != nil {
  210. err = fmt.Errorf("resp body read err: %s", e.Error())
  211. return
  212. }
  213. if len(b) == 0 {
  214. err = fmt.Errorf("resp body is empty")
  215. return
  216. }
  217. result := new(pcsg.PythonBloombergGeneralResult)
  218. if e = json.Unmarshal(b, &result); e != nil {
  219. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  220. return
  221. }
  222. if result.Code != 200 {
  223. err = fmt.Errorf("result: %s", string(b))
  224. return
  225. }
  226. resultData = result.Data
  227. return
  228. }