forked from tulip/twiliogo
/
client.go
179 lines (139 loc) · 3.48 KB
/
client.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package twiliogo
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
const ROOT = "https://api.twilio.com"
const VERSION = "2010-04-01"
type Client interface {
AccountSid() string
AuthToken() string
RootUrl() string
get(url.Values, string) ([]byte, error)
post(url.Values, string) ([]byte, error)
delete(string) error
}
type TwilioClient struct {
accountSid string
authToken string
rootUrl string
Timeout time.Duration
HttpClient *http.Client
}
var _ Client = &TwilioClient{}
func NewClient(accountSid, authToken string) *TwilioClient {
rootUrl := ROOT + "/" + VERSION + "/Accounts/" + accountSid
return &TwilioClient{
accountSid: accountSid,
authToken: authToken,
rootUrl: rootUrl,
HttpClient: &http.Client{
Timeout: 10 * time.Second,
},
}
}
func (client *TwilioClient) post(values url.Values, uri string) ([]byte, error) {
req, err := http.NewRequest("POST", client.buildUri(uri), strings.NewReader(values.Encode()))
if err != nil {
return nil, err
}
req.SetBasicAuth(client.AccountSid(), client.AuthToken())
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, err := client.HttpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return body, err
}
if res.StatusCode != 200 && res.StatusCode != 201 {
if res.StatusCode == 500 {
return body, Error{"Server Error"}
} else {
twilioError := new(TwilioError)
json.Unmarshal(body, twilioError)
return body, twilioError
}
}
return body, err
}
func (client *TwilioClient) get(queryParams url.Values, uri string) ([]byte, error) {
if queryParams == nil {
queryParams = url.Values{}
}
req, err := http.NewRequest("GET", client.buildUri(uri), nil)
req.URL.RawQuery = queryParams.Encode()
if err != nil {
return nil, err
}
req.SetBasicAuth(client.AccountSid(), client.AuthToken())
res, err := client.HttpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return body, err
}
if res.StatusCode != 200 && res.StatusCode != 201 {
if res.StatusCode == 500 {
return body, Error{"Server Error"}
} else {
twilioError := new(TwilioError)
json.Unmarshal(body, twilioError)
return body, twilioError
}
}
return body, err
}
func (client *TwilioClient) delete(uri string) error {
req, err := http.NewRequest("DELETE", client.buildUri(uri), nil)
if err != nil {
return err
}
req.SetBasicAuth(client.AccountSid(), client.AuthToken())
res, err := client.HttpClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 204 {
return fmt.Errorf("Non-204 returned from server for DELETE: %d", res.StatusCode)
}
return nil
}
func (client *TwilioClient) AccountSid() string {
return client.accountSid
}
func (client *TwilioClient) AuthToken() string {
return client.authToken
}
func (client *TwilioClient) RootUrl() string {
return client.rootUrl
}
func (client *TwilioClient) buildUri(parts ...string) string {
if len(parts) == 0 {
return ""
}
newParts := make([]string, 0, len(parts))
// Check for "http" because sometimes we get raw URLs from following the metadata.
if !strings.HasPrefix(parts[0], "http") {
newParts = append(newParts, client.RootUrl())
}
for _, p := range parts {
p = strings.Trim(p, "/")
if p == "" {
continue
}
newParts = append(newParts, p)
}
return strings.Join(newParts, "/")
}