-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwilio.go
48 lines (40 loc) · 1.02 KB
/
twilio.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func (c collector) getUsageRecords() (*usageRecordsResponse, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.twilio.com/2010-04-01/Accounts/%s/Usage/Records/Today.json", c.accountId), nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(c.sid, c.apiKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
respBody, _ := ioutil.ReadAll(resp.Body)
result := usageRecordsResponse{}
err = json.Unmarshal(respBody, &result)
if err != nil {
return nil, err
}
if result.Code != 0 || result.Status != 0 {
return nil, fmt.Errorf("twilio request failed with code %d and status %d", result.Code, result.Status)
}
return &result, nil
}
type usageRecord struct {
AccountSid string `json:"account_sid"`
Category string
Count string
Usage string
Price string
}
type usageRecordsResponse struct {
Code int
Status int
UsageRecords []usageRecord `json:"usage_records"`
}