本文整理匯總了Golang中github.com/intelsdi-x/snap/mgmt/rest/rbody.APIResponse類的典型用法代碼示例。如果您正苦於以下問題:Golang APIResponse類的具體用法?Golang APIResponse怎麽用?Golang APIResponse使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了APIResponse類的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
}