func (s *responseSuite) TestExtractAPIErrorOK(c *gc.C) { response := apihttptesting.NewHTTPResponse() failure, err := apihttp.ExtractAPIError(&response.Response) c.Assert(err, jc.ErrorIsNil) c.Check(failure, gc.IsNil) }
// Upload sends the backup archive to remote storage. func (c *Client) Upload(archive io.Reader, meta params.BackupsMetadataResult) (string, error) { // Empty out some of the metadata. meta.ID = "" meta.Stored = time.Time{} // Send the request. _, resp, err := c.http.SendHTTPRequestReader("backups", archive, &meta, "juju-backup.tar.gz") if err != nil { return "", errors.Annotate(err, "while sending HTTP request") } // Handle the response. if resp.StatusCode == http.StatusOK { var result params.BackupsMetadataResult if err := apihttp.ExtractJSONResult(resp, &result); err != nil { return "", errors.Annotate(err, "while extracting result") } return result.ID, nil } else { failure, err := apihttp.ExtractAPIError(resp) if err != nil { return "", errors.Annotate(err, "while extracting failure") } return "", errors.Trace(failure) } }
func (s *responseSuite) TestExtractAPIErrorMethodNotAllowed(c *gc.C) { response := apihttptesting.NewErrorResponse(http.StatusMethodNotAllowed, "something went wrong!") failure, err := apihttp.ExtractAPIError(&response.Response) c.Assert(err, jc.ErrorIsNil) c.Check(failure.Message, gc.Equals, "something went wrong!") c.Check(failure.Code, gc.Equals, params.CodeNotImplemented) }
func (s *responseSuite) TestExtractAPIErrorString(c *gc.C) { response := apihttptesting.NewErrorResponse(http.StatusInternalServerError, "something went wrong!") failure, err := apihttp.ExtractAPIError(&response.Response) c.Assert(err, jc.ErrorIsNil) c.Check(failure.Message, gc.Equals, "something went wrong!") c.Check(failure.Code, gc.Equals, "") }
func (s *responseSuite) TestExtractAPIErrorFailure(c *gc.C) { original := ¶ms.Error{ Message: "something went wrong!", } response := apihttptesting.NewFailureResponse(original) failure, err := apihttp.ExtractAPIError(&response.Response) c.Assert(err, jc.ErrorIsNil) c.Check(failure, gc.Not(gc.Equals), original) c.Check(failure, gc.DeepEquals, original) }
func (s *responseSuite) TestExtractAPIErrorWrongContentType(c *gc.C) { original := ¶ms.Error{ Message: "something went wrong!", } response := apihttptesting.NewFailureResponse(original) response.Header.Del("Content-Type") failure, err := apihttp.ExtractAPIError(&response.Response) c.Assert(err, jc.ErrorIsNil) c.Check(failure.Message, gc.Equals, `{"Message":"something went wrong!","Code":""}`+"\n") c.Check(failure.Code, gc.Equals, "") }
// Download returns an io.ReadCloser for the given backup id. func (c *Client) Download(id string) (io.ReadCloser, error) { // Send the request. args := params.BackupsDownloadArgs{ ID: id, } _, resp, err := c.http.SendHTTPRequest("backups", &args) if err != nil { return nil, errors.Annotate(err, "while sending HTTP request") } // Handle the response. if resp.StatusCode != http.StatusOK { failure, err := apihttp.ExtractAPIError(resp) if err != nil { return nil, errors.Annotate(err, "while extracting failure") } return nil, errors.Trace(failure) } return resp.Body, nil }