forked from kevinburke/twilio-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wireless.go
237 lines (206 loc) · 7.15 KB
/
wireless.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package twilio
import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"
types "github.com/kevinburke/go-types"
)
const simPathPart = "Sims"
type SimService struct {
client *Client
}
// Sim represents a Sim resource.
type Sim struct {
Sid string `json:"sid"`
UniqueName string `json:"unique_name"`
Status Status `json:"status"`
FriendlyName types.NullString `json:"friendly_name"`
ICCID string `json:"iccid"`
CommandsCallbackMethod string `json:"commands_callback_method"`
CommandsCallbackURL types.NullString `json:"commands_callback_url"`
DateCreated TwilioTime `json:"date_created"`
DateUpdated TwilioTime `json:"date_updated"`
RatePlanSid string `json:"rate_plan_sid"`
SMSURL types.NullString `json:"sms_url"`
SMSMethod types.NullString `json:"sms_method"`
SMSFallbackMethod types.NullString `json:"sms_fallback_method"`
SMSFallbackURL types.NullString `json:"sms_fallback_url"`
VoiceURL types.NullString `json:"voice_url"`
VoiceMethod types.NullString `json:"voice_method"`
VoiceFallbackMethod types.NullString `json:"voice_fallback_method"`
VoiceFallbackURL types.NullString `json:"voice_fallback_url"`
URL string `json:"url"`
AccountSid string `json:"account_sid"`
Links map[string]string `json:"links"`
}
type SimUsageRecord struct {
AccountSid string `json:"account_sid"`
Commands CommandsUsage `json:"commands"`
Data AllDataUsage `json:"data"`
Period UsagePeriod `json:"period"`
SimSid string `json:"sim_sid"`
}
type CommandsUsage struct {
CommandUsage
Home *CommandUsage `json:"home"`
InternationalRoaming []*CommandUsage `json:"international_roaming"`
NationalRoaming *CommandUsage `json:"national_roaming"`
}
type CommandUsage struct {
FromSim uint64 `json:"from_sim"`
ToSim uint64 `json:"to_sim"`
Total uint64 `json:"total"`
}
type AllDataUsage struct {
// TODO: ugh, naming
DataUsage
Home *DataUsage `json:"home"`
InternationalRoaming []*DataUsage `json:"international_roaming"`
NationalRoaming *DataUsage `json:"national_roaming"`
}
type DataUsage struct {
Download types.Bits `json:"download"`
Total types.Bits `json:"total"`
Upload types.Bits `json:"upload"`
Units string `json:"units"`
}
// for parsing from Twilio
type jsonDataUsage struct {
Download int64 `json:"download"`
Total int64 `json:"total"`
Upload int64 `json:"upload"`
Units string `json:"units"`
}
type jsonAllDataUsage struct {
Home *DataUsage `json:"home"`
InternationalRoaming []*DataUsage `json:"international_roaming"`
NationalRoaming *DataUsage `json:"national_roaming"`
Download int64 `json:"download"`
Total int64 `json:"total"`
Upload int64 `json:"upload"`
Units string `json:"units"`
}
func (d *AllDataUsage) UnmarshalJSON(data []byte) error {
mp := new(jsonAllDataUsage)
if err := json.Unmarshal(data, mp); err != nil {
return err
}
d.Home = mp.Home
d.InternationalRoaming = mp.InternationalRoaming
d.NationalRoaming = mp.NationalRoaming
if mp.Units != "bytes" {
return fmt.Errorf("twilio: unknown units parameter %q", mp.Units)
}
d.Units = "bytes"
// multiply bytes by 8 to get bits
d.Download = types.Bits(mp.Download) * types.Byte
d.Upload = types.Bits(mp.Upload) * types.Byte
d.Total = types.Bits(mp.Total) * types.Byte
return nil
}
func (d *DataUsage) UnmarshalJSON(data []byte) error {
mp := new(jsonDataUsage)
if err := json.Unmarshal(data, mp); err != nil {
return err
}
if mp.Units != "bytes" {
return fmt.Errorf("twilio: unknown units parameter %q", mp.Units)
}
d.Units = "bytes"
// multiply by 8 to get bits
d.Download = types.Bits(mp.Download) * types.Byte
d.Upload = types.Bits(mp.Upload) * types.Byte
d.Total = types.Bits(mp.Total) * types.Byte
return nil
}
type UsagePeriod struct {
Start time.Time `json:"start"`
End time.Time `json:"end"`
}
type SimUsageRecordPage struct {
Meta Meta `json:"meta"`
UsageRecords []*SimUsageRecord `json:"usage_records"`
}
// SimPage represents a page of Sims.
type SimPage struct {
Meta Meta `json:"meta"`
Sims []*Sim `json:"sims"`
}
// Get finds a single Sim resource by its sid, or returns an error.
func (s *SimService) Get(ctx context.Context, sid string) (*Sim, error) {
sim := new(Sim)
err := s.client.GetResource(ctx, simPathPart, sid, sim)
return sim, err
}
// GetUsageRecords finds a page of UsageRecord resources.
func (s *SimService) GetUsageRecords(ctx context.Context, simSid string, data url.Values) (*SimUsageRecordPage, error) {
return s.GetUsageRecordsIterator(simSid, data).Next(ctx)
}
func (s *SimService) GetUsageRecordsIterator(simSid string, data url.Values) SimUsageRecordPageIterator {
// TODO this is messy
iter := NewPageIterator(s.client, data, simPathPart+"/"+simSid+"/UsageRecords")
return &simUsageRecordPageIterator{
p: iter,
}
}
type SimUsageRecordPageIterator interface {
// Next returns the next page of resources. If there are no more resources,
// NoMoreResults is returned.
Next(context.Context) (*SimUsageRecordPage, error)
}
type simUsageRecordPageIterator struct {
p *PageIterator
}
func (i *simUsageRecordPageIterator) Next(ctx context.Context) (*SimUsageRecordPage, error) {
ap := new(SimUsageRecordPage)
err := i.p.Next(ctx, ap)
if err != nil {
return nil, err
}
i.p.SetNextPageURI(ap.Meta.NextPageURL)
return ap, nil
}
// Update the sim with the given data. Valid parameters may be found here:
// https://www.twilio.com/docs/api/wireless/rest-api/sim#instance-post
func (c *SimService) Update(ctx context.Context, sid string, data url.Values) (*Sim, error) {
sim := new(Sim)
err := c.client.UpdateResource(ctx, simPathPart, sid, data, sim)
return sim, err
}
// SimPageIterator lets you retrieve consecutive pages of resources.
type SimPageIterator interface {
// Next returns the next page of resources. If there are no more resources,
// NoMoreResults is returned.
Next(context.Context) (*SimPage, error)
}
type simPageIterator struct {
p *PageIterator
}
// GetPage returns a single Page of resources, filtered by data.
//
// See https://www.twilio.com/docs/api/wireless/rest-api/sim#list-get.
func (f *SimService) GetPage(ctx context.Context, data url.Values) (*SimPage, error) {
return f.GetPageIterator(data).Next(ctx)
}
// GetPageIterator returns a SimPageIterator with the given page
// filters. Call iterator.Next() to get the first page of resources (and again
// to retrieve subsequent pages).
func (f *SimService) GetPageIterator(data url.Values) SimPageIterator {
iter := NewPageIterator(f.client, data, simPathPart)
return &simPageIterator{
p: iter,
}
}
// Next returns the next page of resources. If there are no more resources,
// NoMoreResults is returned.
func (f *simPageIterator) Next(ctx context.Context) (*SimPage, error) {
ap := new(SimPage)
err := f.p.Next(ctx, ap)
if err != nil {
return nil, err
}
f.p.SetNextPageURI(ap.Meta.NextPageURL)
return ap, nil
}