-
Notifications
You must be signed in to change notification settings - Fork 0
/
bunny.go
55 lines (41 loc) · 1.23 KB
/
bunny.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
package lib
import (
"context"
"bunnyshell.com/cli/pkg/build"
"bunnyshell.com/cli/pkg/net"
"bunnyshell.com/sdk"
)
func GetAPI() *sdk.APIClient {
return GetApiFromProfile(CLIContext.Profile)
}
func GetContext() (context.Context, context.CancelFunc) {
return GetContextFromProfile(CLIContext.Profile)
}
func GetApiFromProfile(profile Profile) *sdk.APIClient {
return sdk.NewAPIClient(profileToConfiguration(profile))
}
func GetContextFromProfile(profile Profile) (context.Context, context.CancelFunc) {
ctx := context.WithValue(context.Background(), sdk.ContextAPIKeys, map[string]sdk.APIKey{
"ApiKeyAuth": {
Key: profile.Token,
},
})
if CLIContext.Timeout == 0 {
return ctx, func() {}
}
return context.WithTimeout(ctx, CLIContext.Timeout)
}
func profileToConfiguration(profile Profile) *sdk.Configuration {
configuration := getDefaultConfiguration()
if profile.Host != "" {
configuration.Host = profile.Host
}
return configuration
}
func getDefaultConfiguration() *sdk.Configuration {
configuration := sdk.NewConfiguration()
configuration.UserAgent = "BunnyCLI+" + build.Version + "/" + configuration.UserAgent
configuration.Debug = CLIContext.Debug
configuration.HTTPClient = net.GetCLIClient()
return configuration
}