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 }
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 }
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 }