-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathapiclient.go
More file actions
63 lines (54 loc) · 1.72 KB
/
apiclient.go
File metadata and controls
63 lines (54 loc) · 1.72 KB
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
63
package config
import (
"net/url"
"sync"
"github.com/hookdeck/hookdeck-cli/pkg/hookdeck"
)
var apiClient *hookdeck.Client
var apiClientOnce sync.Once
func resetAPIClient() {
apiClient = nil
apiClientOnce = sync.Once{}
}
// ResetAPIClientForTesting resets the global API client singleton so that
// tests can start with a fresh instance. Must only be called from tests.
func ResetAPIClientForTesting() {
resetAPIClient()
}
// RefreshCachedAPIClient copies the current config (API base, profile key and
// project id, log/telemetry flags) onto the cached *hookdeck.Client if one
// already exists. Use after login or other in-process profile updates so the
// singleton matches Profile without discarding the underlying http.Client.
// If GetAPIClient has never been called, this is a no-op (the next GetAPIClient
// will construct from Config).
func (c *Config) RefreshCachedAPIClient() {
if apiClient == nil {
return
}
baseURL, err := url.Parse(c.APIBaseURL)
if err != nil {
panic("Invalid API base URL: " + err.Error())
}
apiClient.BaseURL = baseURL
apiClient.APIKey = c.Profile.APIKey
apiClient.ProjectID = c.Profile.ProjectId
apiClient.Verbose = c.LogLevel == "debug"
apiClient.TelemetryDisabled = c.TelemetryDisabled
}
// GetAPIClient returns the internal API client instance
func (c *Config) GetAPIClient() *hookdeck.Client {
apiClientOnce.Do(func() {
baseURL, err := url.Parse(c.APIBaseURL)
if err != nil {
panic("Invalid API base URL: " + err.Error())
}
apiClient = &hookdeck.Client{
BaseURL: baseURL,
APIKey: c.Profile.APIKey,
ProjectID: c.Profile.ProjectId,
Verbose: c.LogLevel == "debug",
TelemetryDisabled: c.TelemetryDisabled,
}
})
return apiClient
}