- refactor result parser.
This commit is contained in:
@@ -73,21 +73,24 @@ var (
|
||||
ResponseCodeLoadUnexpected: 1,
|
||||
}
|
||||
|
||||
innerCodeKeys = []string{"code", "retCode", "errorCode"}
|
||||
// innerCodeKeys = []string{"code", "retCode", "errorCode"}
|
||||
// innerCodeOKCodes = map[string]int{
|
||||
// "None": 1,
|
||||
// "0": 1,
|
||||
// "1": 1,
|
||||
// "200": 1,
|
||||
// "190005": 1, // 部分失败
|
||||
// }
|
||||
// noPageInnerDataKeys = []string{"result", "data"}
|
||||
|
||||
innerCodeOKCodes = map[string]int{
|
||||
"None": 1,
|
||||
"0": 1,
|
||||
"1": 1,
|
||||
"200": 1,
|
||||
"190005": 1, // 部分失败
|
||||
}
|
||||
|
||||
noPageInnerDataKeys = []string{"result", "data"}
|
||||
havePageInner2DataKeys = []string{"result", "resultList"}
|
||||
havePageTotalCountKeys = []string{"totalCount", "count"}
|
||||
)
|
||||
|
||||
var (
|
||||
nullResultParser = genNoPageResultParser("code", "msg", "", "0")
|
||||
)
|
||||
|
||||
type PageResultParser func(map[string]interface{}, int) ([]interface{}, int, error)
|
||||
|
||||
func (a *API) signParams(jdParams map[string]interface{}) string {
|
||||
@@ -153,6 +156,7 @@ func (a *API) AccessAPI(apiStr string, jdParams map[string]interface{}) (retVal
|
||||
a.config,
|
||||
func(response *http.Response) (errLevel string, err error) {
|
||||
jsonResult1, err := utils.HTTPResponse2Json(response)
|
||||
// baseapi.SugarLogger.Debug(utils.Format4Output(jsonResult1, false))
|
||||
if err != nil {
|
||||
return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong
|
||||
}
|
||||
@@ -173,32 +177,34 @@ func (a *API) AccessAPI(apiStr string, jdParams map[string]interface{}) (retVal
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
func normalNoPageResultParser(data map[string]interface{}) (interface{}, error) {
|
||||
innerCode := ""
|
||||
for _, innerCodeKey := range innerCodeKeys {
|
||||
if innerCode2, ok := data[innerCodeKey]; ok {
|
||||
innerCode = forceInnerCode2Str(innerCode2)
|
||||
break
|
||||
func genNoPageResultParser(codeKey, msgKey, resultKey, okCode string) func(data map[string]interface{}) (interface{}, error) {
|
||||
return func(data map[string]interface{}) (interface{}, error) {
|
||||
rawInnerCode, ok := data[codeKey]
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("genNoPageResultParser codeKey %s can not be found in result:%v", codeKey, data))
|
||||
}
|
||||
}
|
||||
if innerCode == "" {
|
||||
panic(fmt.Sprintf("can not find innerCode, data:%v", data))
|
||||
}
|
||||
if _, ok := innerCodeOKCodes[innerCode]; ok {
|
||||
for _, innerDataKey := range noPageInnerDataKeys {
|
||||
if innerData, ok := data[innerDataKey]; ok {
|
||||
return innerData, nil
|
||||
innerCode := forceInnerCode2Str(rawInnerCode)
|
||||
if innerCode == okCode {
|
||||
if resultKey != "" {
|
||||
if innerData, ok := data[resultKey]; ok {
|
||||
return innerData, nil
|
||||
}
|
||||
baseapi.SugarLogger.Warnf("genNoPageResultParser resultKey %s can not be found in result:%v", resultKey, data)
|
||||
return nil, nil // 容错
|
||||
// panic(fmt.Sprintf("genNoPageResultParser resultKey %s can not be found in result:%v", resultKey, data))
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
panic(fmt.Sprintf("can not find inner data, data:%v", data))
|
||||
// return nil, platformapi.ErrResponseDataFormatWrong
|
||||
if msg, ok := data[msgKey]; ok {
|
||||
return nil, utils.NewErrorCode(msg.(string), innerCode, 1)
|
||||
}
|
||||
panic(fmt.Sprintf("genNoPageResultParser msgKey %s can not be found in result:%v", msgKey, data))
|
||||
}
|
||||
return nil, utils.NewErrorCode(getErrMsgFromData(data), innerCode, 1)
|
||||
}
|
||||
|
||||
func (a *API) AccessAPINoPage(apiStr string, jdParams map[string]interface{}, keyToRemove, keyToKeep []string, resultParser func(data map[string]interface{}) (interface{}, error)) (interface{}, error) {
|
||||
if resultParser == nil {
|
||||
resultParser = normalNoPageResultParser
|
||||
resultParser = genNoPageResultParser("code", "msg", "result", "0")
|
||||
}
|
||||
jsonResult, err := a.AccessAPI(apiStr, jdParams)
|
||||
if err != nil {
|
||||
@@ -305,7 +311,7 @@ func (a *API) AccessAPIHavePage(apiStr string, jdParams map[string]interface{},
|
||||
}
|
||||
|
||||
innerCode := forceInnerCode2Str(data["code"])
|
||||
if innerCode != "0" && innerCode != "200" {
|
||||
if innerCode != "0" {
|
||||
return nil, totalCount, utils.NewErrorCode(getErrMsgFromData(data), innerCode, 1)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user