-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
62 lines (53 loc) · 1.57 KB
/
controller.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
55
56
57
58
59
60
61
62
package rebrandly
import (
"net/http"
"sync"
cleanhttp "github.com/hashicorp/go-cleanhttp"
)
// Controller abstracts the access to the cpg API
type Controller struct {
apiKey string
workspace string
workspaceAccess sync.RWMutex
useragent string
useragentAccess sync.RWMutex
client *http.Client
}
// New returns an initialized and ready to use Controller
func New(APIKey string) *Controller {
return &Controller{
apiKey: APIKey,
client: cleanhttp.DefaultPooledClient(),
}
}
// ClearWorkspace allows to reset the controller on the default workspace
func (c *Controller) ClearWorkspace() {
c.SetWorkspace("")
}
// GetWorkspace returns the current workspace on which the controller work against.
// Empty means default workspace.
func (c *Controller) GetWorkspace() (workspace string) {
c.workspaceAccess.RLock()
workspace = c.workspace
c.workspaceAccess.RUnlock()
return
}
// SetWorkspace changes the workspace on which the controller will work against.
func (c *Controller) SetWorkspace(workspace string) {
c.workspaceAccess.Lock()
c.workspace = workspace
c.workspaceAccess.Unlock()
}
// SetUserAgent allows tu customize the user agent used by the controller when performing http requests.
func (c *Controller) SetUserAgent(ua string) {
c.useragentAccess.Lock()
c.useragent = ua
c.useragentAccess.Unlock()
}
// GetUserAgent returns the current user agent configured. If empty, golang default ua will be used.
func (c *Controller) GetUserAgent() (ua string) {
c.useragentAccess.RLock()
ua = c.useragent
c.useragentAccess.RUnlock()
return
}