-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
99 lines (86 loc) · 2.7 KB
/
config.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package rchooks
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"github.com/grokify/goauth"
"github.com/grokify/goauth/authutil"
"github.com/grokify/goauth/ringcentral"
"github.com/grokify/mogo/errors/errorsutil"
rc "github.com/grokify/go-ringcentral-client/office/v1/client"
ru "github.com/grokify/go-ringcentral-client/office/v1/util"
)
type RcHooksConfig struct {
Token string `env:"RINGCENTRAL_TOKEN"`
ServerURL string `env:"RINGCENTRAL_SERVER_URL"`
WebhookDefinitionJSON string `env:"RINGCENTRAL_WEBHOOK_DEFINITION_JSON"`
WebhookDefinition rc.CreateSubscriptionRequest
}
func NewRcHooksConfigCreds(ctx context.Context, creds goauth.Credentials, hookDefJSON string) (RcHooksConfig, error) {
cfg := RcHooksConfig{
ServerURL: creds.OAuth2.ServerURL,
WebhookDefinitionJSON: hookDefJSON}
if creds.Token == nil {
tok, err := creds.NewToken(ctx)
if err != nil {
return cfg, err
}
creds.Token = tok
}
if creds.Token != nil {
cfg.Token = strings.TrimSpace(creds.Token.AccessToken)
}
return cfg, nil
}
func NewRcHooksConfigEnv(envVarTokenOrJSON, envVarServerURL, envVarHookDef string) RcHooksConfig {
return RcHooksConfig{
Token: os.Getenv(envVarTokenOrJSON),
ServerURL: os.Getenv(envVarServerURL),
WebhookDefinitionJSON: os.Getenv(envVarHookDef)}
}
func (rchConfig *RcHooksConfig) Inflate() error {
rchConfig.WebhookDefinitionJSON = strings.TrimSpace(rchConfig.WebhookDefinitionJSON)
if len(rchConfig.WebhookDefinitionJSON) <= 0 {
return fmt.Errorf("E_NO_WEBHOOK_DEFINITION")
}
req, err := ParseCreateSubscriptionRequest([]byte(rchConfig.WebhookDefinitionJSON))
if err != nil {
return errorsutil.Wrap(err, "Parse subscription definition")
}
rchConfig.WebhookDefinition = req
return nil
}
func (rchConfig *RcHooksConfig) Client() (*http.Client, error) {
return authutil.NewClientBearerTokenSimpleOrJSON(context.Background(), []byte(rchConfig.Token))
}
func (rchConfig *RcHooksConfig) ClientUtil() (ringcentral.ClientUtil, error) {
cu := ringcentral.ClientUtil{
ServerURL: rchConfig.ServerURL}
client, err := rchConfig.Client()
if err != nil {
return cu, err
}
cu.Client = client
return cu, err
}
func (rchConfig *RcHooksConfig) InitializeRcHooks(ctx context.Context) (RcHooks, error) {
rchooksUtil := RcHooks{}
err := rchConfig.Inflate()
if err != nil {
return rchooksUtil, err
}
httpClient, err := rchConfig.Client()
if err != nil {
return rchooksUtil, errorsutil.Wrap(err, "New client token")
}
apiClient, err := ru.NewApiClientHttpClientBaseURL(
httpClient, rchConfig.ServerURL)
if err != nil {
return rchooksUtil, err
} else {
rchooksUtil.Client = apiClient
return rchooksUtil, nil
}
}