github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/api/backups/download.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package backups
     5  
     6  import (
     7  	"io"
     8  	"net/http"
     9  
    10  	"github.com/juju/errors"
    11  
    12  	apihttp "github.com/juju/juju/apiserver/http"
    13  	"github.com/juju/juju/apiserver/params"
    14  )
    15  
    16  // Download returns an io.ReadCloser for the given backup id.
    17  func (c *Client) Download(id string) (io.ReadCloser, error) {
    18  	// Send the request.
    19  	args := params.BackupsDownloadArgs{
    20  		ID: id,
    21  	}
    22  	_, resp, err := c.http.SendHTTPRequest("backups", &args)
    23  	if err != nil {
    24  		return nil, errors.Annotate(err, "while sending HTTP request")
    25  	}
    26  
    27  	// Handle the response.
    28  	if resp.StatusCode != http.StatusOK {
    29  		failure, err := apihttp.ExtractAPIError(resp)
    30  		if err != nil {
    31  			return nil, errors.Annotate(err, "while extracting failure")
    32  		}
    33  		return nil, errors.Trace(failure)
    34  	}
    35  
    36  	return resp.Body, nil
    37  }