github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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 type ClientFacade interface { 21 // BestAPIVersion returns the API version that we were able to 22 // determine is supported by both the client and the API Server 23 BestAPIVersion() int 24 25 // Close the connection to the API server. 26 Close() error 27 } 28 29 type closer interface { 30 Close() error 31 } 32 33 type clientFacade struct { 34 facadeCaller 35 closer 36 } 37 38 var _ ClientFacade = (*clientFacade)(nil) 39 40 // NewClientFacade prepares a client-facing facade for work against the API. 41 // It is expected that most client-facing facades will embed a ClientFacade and 42 // will use a FacadeCaller so this function returns both. 43 func NewClientFacade(caller APICallCloser, facadeName string) (ClientFacade, FacadeCaller) { 44 clientFacade := clientFacade{ 45 facadeCaller: facadeCaller{ 46 facadeName: facadeName, 47 bestVersion: caller.BestFacadeVersion(facadeName), 48 caller: caller, 49 }, closer: caller, 50 } 51 return clientFacade, clientFacade 52 }