github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/api/backups/client.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  	"strings"
    10  
    11  	"github.com/juju/loggo"
    12  
    13  	"github.com/juju/juju/api/base"
    14  )
    15  
    16  var logger = loggo.GetLogger("juju.api.backups")
    17  
    18  // httpClient represents the methods of api.State (see api/http.go)
    19  // needed by backups for direct HTTP requests.
    20  type httpClient interface {
    21  	// SendHTTPRequest sends an HTTP GET request relative to the client.
    22  	SendHTTPRequest(path string, args interface{}) (*http.Request, *http.Response, error)
    23  	// SendHTTPRequestReader sends an HTTP PUT request relative to the client.
    24  	SendHTTPRequestReader(path string, attached io.Reader, meta interface{}, name string) (*http.Request, *http.Response, error)
    25  }
    26  
    27  type apiState interface {
    28  	base.APICallCloser
    29  	httpClient
    30  
    31  	// Addr returns the address used to connect to the API server.
    32  	Addr() string
    33  }
    34  
    35  // Client wraps the backups API for the client.
    36  type Client struct {
    37  	base.ClientFacade
    38  	facade        base.FacadeCaller
    39  	http          httpClient
    40  	baseFacade    base.FacadeCaller
    41  	publicAddress string
    42  }
    43  
    44  // NewClient returns a new backups API client.
    45  func NewClient(st apiState) *Client {
    46  	publicAddress := strings.SplitN(st.Addr(), ":", 2)[0]
    47  	frontend, backend := base.NewClientFacade(st, "Backups")
    48  	return &Client{
    49  		ClientFacade:  frontend,
    50  		facade:        backend,
    51  		http:          st,
    52  		publicAddress: publicAddress,
    53  	}
    54  }