forked from natebrennand/twiliogo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
93 lines (82 loc) · 2.65 KB
/
account.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
package twiliogo
import (
"fmt"
"net/http"
"os"
"regexp"
"github.com/natebrennand/twiliogo/applications"
"github.com/natebrennand/twiliogo/common"
"github.com/natebrennand/twiliogo/conference"
"github.com/natebrennand/twiliogo/notifications"
"github.com/natebrennand/twiliogo/numbers"
"github.com/natebrennand/twiliogo/queues"
"github.com/natebrennand/twiliogo/recording"
"github.com/natebrennand/twiliogo/sip"
"github.com/natebrennand/twiliogo/sms"
"github.com/natebrennand/twiliogo/sms/shortcodes"
"github.com/natebrennand/twiliogo/transcription"
"github.com/natebrennand/twiliogo/usage"
"github.com/natebrennand/twiliogo/voice"
)
const (
twilioAccount = "TWILIO_ACCOUNT"
twilioToken = "TWILIO_TOKEN"
)
var validateAccountSid = regexp.MustCompile("^AC[a-z0-9]{32}$").MatchString
// Account is a catch-all account object that holds references to all Twilio resources.
type Account struct {
AccountSid string
Token string
// redundancy for usability
Sms sms.Account
ShortCode shortcodes.Account
Voice voice.Account
Recordings recording.Account
Transcriptions transcription.Account
Conferences conference.Account
Applications applications.Account
Notifications notifications.Account
Usage usage.Account
Numbers numbers.Account
SIP sip.Account
Queues queues.Account
}
// NewAccount builds an Account resource from a sid & token.
func NewAccount(sid, token string) Account {
if !validateAccountSid(sid) {
panic("Invalid Account Sid")
}
a := common.Account{
AccountSid: sid,
Token: token,
Client: http.Client{},
}
return Account{
AccountSid: sid,
Token: token,
Sms: sms.Account{Account: a},
ShortCode: shortcodes.Account{Account: a},
Voice: voice.Account{Account: a},
Usage: usage.Account{Account: a},
Recordings: recording.Account{Account: a},
Transcriptions: transcription.Account{Account: a},
Applications: applications.Account{Account: a},
Conferences: conference.Account{Account: a},
Notifications: notifications.Account{Account: a},
Numbers: numbers.Account{Account: a},
SIP: sip.Account{Account: a},
Queues: queues.Account{Account: a},
}
}
// NewAccountFromEnv builds an Account resource from environment variables.
func NewAccountFromEnv() Account {
sid := os.Getenv(twilioAccount)
token := os.Getenv(twilioToken)
if sid == "" {
panic(fmt.Sprintf("You must set the environment variable %s.", twilioAccount))
}
if token == "" {
panic(fmt.Sprintf("You must set the environment variable %s.", twilioToken))
}
return NewAccount(sid, token)
}