github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/base/clientfacade.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package base
     5  
     6  // APICallCloser is the same as APICaller, but also provides a Close() method
     7  // for when we are done with this connection.
     8  type APICallCloser interface {
     9  	APICaller
    10  
    11  	// Close is used when we have finished with this connection.
    12  	Close() error
    13  }
    14  
    15  // ClientFacade should be embedded by client-side facades that are intended as
    16  // "client" (aka user facing) facades versus agent facing facades.
    17  // They provide two common methods for writing the client side code.
    18  // BestAPIVersion() is used to allow for compatibility testing, and Close() is
    19  // used to indicate when we are done with the connection.
    20  //go:generate mockgen -package mocks -destination mocks/clientfacade_mock.go github.com/juju/juju/api/base APICallCloser,ClientFacade
    21  type ClientFacade interface {
    22  	// BestAPIVersion returns the API version that we were able to
    23  	// determine is supported by both the client and the API Server
    24  	BestAPIVersion() int
    25  
    26  	// Close the connection to the API server.
    27  	Close() error
    28  }
    29  
    30  type closer interface {
    31  	Close() error
    32  }
    33  
    34  type clientFacade struct {
    35  	facadeCaller
    36  	closer
    37  }
    38  
    39  var _ ClientFacade = (*clientFacade)(nil)
    40  
    41  // NewClientFacade prepares a client-facing facade for work against the API.
    42  // It is expected that most client-facing facades will embed a ClientFacade and
    43  // will use a FacadeCaller so this function returns both.
    44  func NewClientFacade(caller APICallCloser, facadeName string) (ClientFacade, FacadeCaller) {
    45  	clientFacade := clientFacade{
    46  		facadeCaller: facadeCaller{
    47  			facadeName:  facadeName,
    48  			bestVersion: caller.BestFacadeVersion(facadeName),
    49  			caller:      caller,
    50  		}, closer: caller,
    51  	}
    52  	return clientFacade, clientFacade
    53  }