Exemplo n.º 1
0
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)
}
Exemplo n.º 2
0
// 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)
	}
}
Exemplo n.º 3
0
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)
}
Exemplo n.º 4
0
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, "")
}
Exemplo n.º 5
0
func (s *responseSuite) TestExtractAPIErrorFailure(c *gc.C) {
	original := &params.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)
}
Exemplo n.º 6
0
func (s *responseSuite) TestExtractAPIErrorWrongContentType(c *gc.C) {
	original := &params.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, "")
}
Exemplo n.º 7
0
// 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
}