1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package index_data
- import (
- "encoding/json"
- "eta/eta_bridge/global"
- "eta/eta_bridge/models/pcsg"
- "fmt"
- "io/ioutil"
- "net/http"
- )
- // GetPCSGBloombergGeneralIndex 获取指标信息
- func GetPCSGBloombergGeneralIndex(taskKey, frequency string) (indexes []pcsg.BaseFromBloombergApiIndexAndData, err error) {
- apiData, e := CurlPCSGBloombergGeneralDataApi(taskKey)
- if e != nil {
- err = fmt.Errorf("CurlPCSGBloombergDailyApi err: %s", e.Error())
- return
- }
- if len(apiData) == 0 {
- return
- }
- for _, v := range apiData {
- t := pcsg.FormatPythonBloombergGeneralData2Base(v, frequency, taskKey)
- if t.IndexCode != "" {
- indexes = append(indexes, t)
- }
- }
- return
- }
- // CurlPCSGBloombergGeneralDataApi 请求通用格式数据接口
- func CurlPCSGBloombergGeneralDataApi(taskKey string) (resultData []pcsg.PythonBloombergGeneralData, err error) {
- if taskKey == "" {
- err = fmt.Errorf("任务Key为空")
- return
- }
- if global.CONFIG.PCSG.BloombergApiUrl == "" {
- err = fmt.Errorf("服务地址为空")
- return
- }
- url := fmt.Sprintf("%s/api/bloomberg/general_data?task_key=%s", global.CONFIG.PCSG.BloombergApiUrl, taskKey)
- resp, e := http.Get(url)
- if e != nil {
- err = fmt.Errorf("http post err: %s", e.Error())
- return
- }
- defer resp.Body.Close()
- b, e := ioutil.ReadAll(resp.Body)
- if e != nil {
- err = fmt.Errorf("resp body read err: %s", e.Error())
- return
- }
- if len(b) == 0 {
- err = fmt.Errorf("resp body is empty")
- return
- }
- //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}]}`
- //b := []byte(str)
- result := new(pcsg.PythonBloombergGeneralResult)
- if e = json.Unmarshal(b, &result); e != nil {
- err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
- return
- }
- if result.Code != 200 {
- err = fmt.Errorf("result: %s", string(b))
- return
- }
- resultData = result.Data
- return
- }
|