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