管理Golang中的多种退货类型
我是个新手,很难根据请求类型将响应对象从API调用转换为不同的结构 基本上,我有一个func发出请求管理Golang中的多种退货类型,go,Go,我是个新手,很难根据请求类型将响应对象从API调用转换为不同的结构 基本上,我有一个func发出请求 func (fpc *FPClient) request(path string, method string, params interface{}, Token string, response interface{}) *dto.AppError { client := &http.Client{ Timeout: time.Second * 15,
func (fpc *FPClient) request(path string, method string, params interface{}, Token string, response interface{}) *dto.AppError {
client := &http.Client{
Timeout: time.Second * 15,
}
requestBody, err := json.Marshal(params)
if err != nil {
//
}
req, _ := http.NewRequest(method, path, bytes.NewBuffer(requestBody))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Cookie", fmt.Sprintf("cookie=%s;", Token))
req.SetBasicAuth(fpc.username, fpc.password)
resp, err := client.Do(req)
if err != nil {
//
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
//
}
if FPErr := fpc.processErrors(resp, body); FPerr != nil {
return FPErr
}
responseData := FPApiSuccess{Head: response, Body: response}
if err := json.Unmarshal(body, &responseData); err != nil {
//
}
fmt.Printf("\n\n client Response : \n %+v \n", responseData.Body)
return nil
}
fpapisoccess
的结构是:
type FPApiSuccess struct {
Body interface{} `json:"body"`
Head interface{} `json:"head"`
}
现在,有2个调用函数,预期的API响应对这两个函数都有点不同
两个API响应都具有以下结构
{
"head": {},
"body": {}
}
但是,根据所使用的API,每个键中的嵌套细节是不同的
我想在我给出的struct参数中捕获head
和body
键,并将其发送回调用函数。request
函数中的response
参数是基于调用函数的不同结构类型
我无法让它正常工作-我只从请求
函数返回一个空白结构。这是fmt.PrintF
日志
client Response :
&{Body:{BOrderID:0 CC: Exch: EOID: Type: Local:0 Message: } Head:{ResponseCode: Status: StatusDescription:}}
这是一个空结构-理想情况下,应该用从API检索的值填充它
以下是作为参数传递的结构,作为请求函数中的response
,以供参考:
type FPApiResponse struct {
Body FPBodyResponse `json:"body"`
Head FPHeadResponse `json:"head"`
}
type FPHeadResponse struct {
ResponseCode string `json:"responseCode"`
Status string `json:"status"`
StatusDescription string `json:"statusDescription"`
}
type FPBodyResponse struct {
BOrderID int `json:"BOrderID"`
CC string `json:"CC"`
Exch string `json:"Exch"`
EOID string `json:"EOID"`
Type string `json:"Type"`
Local int `json:"Local"`
Message string `json:"Message"`
}
更新
所以我就这样做了,;而不是
responseData := FPApiSuccess{Head: response, Body: response}
是我干的
responseData := fivePaisaApiSuccess{}
现在,我在控制台中看到下面的内容
Client Response :
{Body:map[BOrderID:0 CC:52715111 Type:D Local:0 Message:Invalid Session ] Head:map[responseCode:5POrdReq status:0 statusDescription:Success]}
从本质上讲,这是可行的,但是调用函数似乎没有得到正确的响应:
下面是调用函数
func (fpc *FPClient) PlaceOrder(orderParams dto.OrderBodyParams, variety string, Token string) (string, *dto.AppError) {
var result FPApiResponse
headParams := dto.FFPOrderHeadParams{
//
}
FPOrderParams := dto.FPOrderParams{
//
}
FPErr := fpc.request(FPURL+FPPlaceOrder, http.MethodPost, FPOrderParams, brokerAccessToken, &result)
if FPErr != nil {
return "", FPErr
}
fmt.Printf("\n\n Client result : \n %+v \n", result)
if result.Head.Status != "0" {
//
}
if result.Body.Status != 0 {
//
}
return strconv.Itoa(result.Body.Broker), nil
}
结果
值为空:
{Body:{BOId:0 CC: Exch: Type: Local:0 Message: Status:0} Head:{ResponseCode: Status: StatusDescription:}}
我不明白,这个指针正在请求函数中填充
下面是我传递给请求的结构:
type FPApiResponse struct {
Body FPBodyResponse `json:"body"`
Head FPHeadResponse `json:"head"`
}
您正在将
fpapisresponse
值设置为fpapissuccess
值的Body
和Head
。这两个结构都需要head+body json对象。因此,为了将json解组为这些值,您收到的json实际上必须是您所期望的json的嵌套版本,即@mkopriva-您是对的,我将结构拆分为2个(head和body),并将它们都像这样发送responseData:=fpapisucture{head:headResponse,body:bodyResponse}
-这似乎起到了作用trick@Kannaj:这只是一个提示,因为您是golang的新手:避免过多地使用接口{}
键入。如果没有别的事,就认为这是必要的罪恶。代码>接口{}在某些方面是好的,但是当您知道您正在处理什么数据时,请使用特定的类型instead@EliasVanOotegem-明白,谢谢。我已经可以预见到它会带来的痛苦:)您正在将FPApiResponse
值设置为fpapissuccess
值的Body
和Head
。这两个结构都需要head+body json对象。因此,为了将json解组为这些值,您收到的json实际上必须是您所期望的json的嵌套版本,即@mkopriva-您是对的,我将结构拆分为2个(head和body),并将它们都像这样发送responseData:=fpapisucture{head:headResponse,body:bodyResponse}
-这似乎起到了作用trick@Kannaj:这只是一个提示,因为您是golang的新手:避免过多地使用接口{}
键入。如果没有别的事,就认为这是必要的罪恶。代码>接口{}在某些方面是好的,但是当您知道您正在处理什么数据时,请使用特定的类型instead@EliasVanOotegem-明白,谢谢。我已经可以预见到它会带来的痛苦:)