增加对响应体的解析

This commit is contained in:
白茶清欢 2022-06-30 18:26:16 +08:00
parent f766b4c7e3
commit 04f782ac07
4 changed files with 70 additions and 20 deletions

View File

@ -79,4 +79,6 @@ const (
DefaultMessageField = "message" DefaultMessageField = "message"
// DefaultDataField 默认数据字段 // DefaultDataField 默认数据字段
DefaultDataField = "data" DefaultDataField = "data"
// SpecialFiledVal 特殊的 code / message / data配置
SpecialFiledVal = "-"
) )

3
go.mod
View File

@ -23,6 +23,9 @@ require (
github.com/pelletier/go-toml/v2 v2.0.2 // indirect github.com/pelletier/go-toml/v2 v2.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/tidwall/gjson v1.14.1 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect github.com/ugorji/go/codec v1.2.7 // indirect
go.uber.org/atomic v1.9.0 // indirect go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect go.uber.org/multierr v1.8.0 // indirect

6
go.sum
View File

@ -68,6 +68,12 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo= github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=

63
rpc.go
View File

@ -18,6 +18,8 @@ import (
"sync" "sync"
"time" "time"
"github.com/tidwall/gjson"
"git.zhangdeman.cn/zhangdeman/util" "git.zhangdeman.cn/zhangdeman/util"
"github.com/ddliu/go-httpclient" "github.com/ddliu/go-httpclient"
@ -226,6 +228,7 @@ func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, par
body []byte body []byte
response *httpclient.Response response *httpclient.Response
responseBody []byte responseBody []byte
code, message, data string
) )
if serviceConfig, apiConfig, err = r.GetServiceApi(serviceFlag, apiFlag); nil != err { if serviceConfig, apiConfig, err = r.GetServiceApi(serviceFlag, apiFlag); nil != err {
return err return err
@ -237,21 +240,12 @@ func (r *request) Send(ctx *gin.Context, serviceFlag string, apiFlag string, par
return err return err
} }
// 判断状态码
isHttpSuccess := false
for _, successCode := range apiConfig.SuccessHttpCodeList {
if successCode == response.StatusCode {
isHttpSuccess = true
break
}
}
if !isHttpSuccess {
return fmt.Errorf("http响应状态码异常 -> %v", response.StatusCode)
}
if responseBody, err = io.ReadAll(response.Body); nil != err { if responseBody, err = io.ReadAll(response.Body); nil != err {
return err return err
} }
// 解析响应的业务数据
code, message, data = r.getCodeAndMessageAndData(apiConfig, responseBody)
return nil return nil
} }
@ -317,3 +311,48 @@ func (r *request) getFullURLAndBody(serviceConfig *Service, apiConfig *Api, para
} }
return strings.ReplaceAll(fullURL, "//", "/") + "?" + query, body return strings.ReplaceAll(fullURL, "//", "/") + "?" + query, body
} }
// validateResponseHttpCode 验证http状态码
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:18 2022/6/30
func (r *request) validateResponseHttpCode(apiConfig *Api, response *httpclient.Response) error {
// 判断状态码
isHttpSuccess := false
for _, successCode := range apiConfig.SuccessHttpCodeList {
if successCode == response.StatusCode {
isHttpSuccess = true
break
}
}
if !isHttpSuccess {
return fmt.Errorf("http响应状态码异常 -> %v", response.StatusCode)
}
return nil
}
// getCodeAndMessageAndData 读取业务状态码 + 文案 + 数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:20 2022/6/30
func (r *request) getCodeAndMessageAndData(apiConfig *Api, responseBody []byte) (string, string, string) {
var (
code = SpecialFiledVal
message = SpecialFiledVal
data string
)
if apiConfig.CodeField != SpecialFiledVal {
code = gjson.Get(string(responseBody), apiConfig.CodeField).String()
}
if apiConfig.MessageField != SpecialFiledVal {
message = gjson.Get(string(responseBody), apiConfig.MessageField).String()
}
if apiConfig.DataField == SpecialFiledVal {
data = string(responseBody)
} else {
data = gjson.Get(string(responseBody), apiConfig.DataField).String()
}
return code, message, data
}