本文整理汇总了Golang中github.com/intelsdi-x/snap/mgmt/rest/rbody.APIResponse.JSONResponse方法的典型用法代码示例。如果您正苦于以下问题:Golang APIResponse.JSONResponse方法的具体用法?Golang APIResponse.JSONResponse怎么用?Golang APIResponse.JSONResponse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/intelsdi-x/snap/mgmt/rest/rbody.APIResponse
的用法示例。
在下文中一共展示了APIResponse.JSONResponse方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: httpRespToAPIResp
func httpRespToAPIResp(rsp *http.Response) (*rbody.APIResponse, error) {
if rsp.StatusCode == 401 {
return nil, fmt.Errorf("Invalid credentials")
}
resp := new(rbody.APIResponse)
b, err := ioutil.ReadAll(rsp.Body)
rsp.Body.Close()
if err != nil {
return nil, err
}
jErr := json.Unmarshal(b, resp)
// If unmarshaling fails show first part of response to help debug
// connection issues.
if jErr != nil {
bound := 1000
var invalidResp string
if len(b) > bound {
invalidResp = string(b[:bound])
} else {
invalidResp = string(b)
}
return nil, fmt.Errorf("Unknown API response: %s\n\n Received: %s", jErr, invalidResp)
}
if resp == nil {
// Catch corner case where JSON gives no error but resp is nil
return nil, ErrNilResponse
}
// Add copy of JSON response string
resp.JSONResponse = string(b)
return resp, nil
}
示例2: getStreamingAPIResponse
func getStreamingAPIResponse(resp *http.Response) *rbody.APIResponse {
r := new(rbody.APIResponse)
rb := readBody(resp)
err := json.Unmarshal(rb, r)
if err != nil {
log.Fatal(err)
}
r.JSONResponse = string(rb)
return r
}
示例3: httpRespToAPIResp
func httpRespToAPIResp(rsp *http.Response) (*rbody.APIResponse, error) {
resp := new(rbody.APIResponse)
b, err := ioutil.ReadAll(rsp.Body)
rsp.Body.Close()
if err != nil {
return nil, err
}
jErr := json.Unmarshal(b, resp)
if jErr != nil {
return nil, jErr
}
if resp == nil {
// Catch corner case where JSON gives no error but resp is nil
return nil, ErrNilResponse
}
// Add copy of JSON response string
resp.JSONResponse = string(b)
return resp, nil
}