/
config.go
96 lines (77 loc) · 2.74 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
// Copyright 2017 Avaya Cloud Inc. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package zang
import (
"fmt"
"net/url"
"os"
"strings"
)
// Config - Configuration struct, designed to be
type Config struct {
ApiUrl string
ApiVersion string
AccountSid string
AuthToken string
ResponseContentType string
}
// OptionString - Will return ENV value back based on provided name casted as string
func OptionString(name, def string) string {
if res := os.Getenv(name); res != "" {
return string(res)
}
return def
}
// Validate - Will validate client configuration and return error if any issues are found
func (c *Config) Validate() error {
if _, err := url.ParseRequestURI(c.ApiUrl); err != nil {
return fmt.Errorf("Make sure to provide valid Api URL. You've provided: %s. Error: %s", c.ApiUrl, err)
}
if c.ApiVersion != "v2" {
return fmt.Errorf("Make sure to provide valid api version. You've provided: %s.", c.ApiVersion)
}
if len(c.AccountSid) != 34 || !strings.HasPrefix(c.AccountSid, "AC") {
return fmt.Errorf("Make sure to provide valid AccountSid. You've provided: %s", c.AccountSid)
}
if len(c.AuthToken) != 32 {
return fmt.Errorf("Make sure to provide valid AuthToken. You've provided: %s", c.AuthToken)
}
if !StringInSlice(c.ResponseContentType, AvailableResponseContentTypes) {
return fmt.Errorf("Make sure to provide valid response content type. You've provided: %s. Available are: %v", c.AuthToken, AvailableResponseContentTypes)
}
return nil
}
// SetCredentials - Will set account sid and auth token to desired account
func (c *Config) SetCredentials(accountSid, authToken string) error {
if len(accountSid) != 34 || !strings.HasPrefix(accountSid, "AC") {
return fmt.Errorf("Make sure to provide valid AccountSid. You've provided: %s", accountSid)
}
if len(authToken) != 32 {
return fmt.Errorf("Make sure to provide valid AuthToken. You've provided: %s", authToken)
}
c.AccountSid = accountSid
c.AuthToken = authToken
return nil
}
// NewConfig -
func NewConfig() (*Config, error) {
cfg := &Config{
ApiUrl: OptionString("ZANG_CLOUD_API_URL", ZangApiUrl),
ApiVersion: OptionString("ZANG_CLOUD_API_VERSION", ZangApiVersion),
AccountSid: OptionString("ZANG_CLOUD_ACCOUNT_SID", ""),
AuthToken: OptionString("ZANG_CLOUD_AUTH_TOKEN", ""),
ResponseContentType: OptionString("ZANG_CLOUD_RESPONSE_TYPE", DefaultResponseContentType),
}
if err := cfg.Validate(); err != nil {
return nil, err
}
return cfg, nil
}
// NewCustomConfig -
func NewCustomConfig(cfg *Config) (*Config, error) {
if err := cfg.Validate(); err != nil {
return nil, err
}
return cfg, nil
}