-
Notifications
You must be signed in to change notification settings - Fork 103
/
keys.go
91 lines (77 loc) · 2.56 KB
/
keys.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
package twilio
import (
"net/url"
"golang.org/x/net/context"
)
const keyPathPart = "Keys"
type KeyService struct {
client *Client
}
// A Twilio Key. For more documentation, see
// https://www.twilio.com/docs/api/rest/keys#instance
type Key struct {
DateCreated TwilioTime `json:"date_created"`
DateUpdated TwilioTime `json:"date_updated"`
Sid string `json:"sid"`
FriendlyName string `json:"friendly_name"`
Secret string `json:"secret"`
}
type KeyPage struct {
Page
Keys []*Key `json:"keys"`
}
func (c *KeyService) Get(ctx context.Context, sid string) (*Key, error) {
key := new(Key)
err := c.client.GetResource(ctx, keyPathPart, sid, key)
return key, err
}
func (c *KeyService) GetPage(ctx context.Context, data url.Values) (*KeyPage, error) {
iter := c.GetPageIterator(data)
return iter.Next(ctx)
}
// Create a new Key. Note the Secret is only returned in response to a Create,
// you can't retrieve it later.
//
// https://www.twilio.com/docs/api/rest/keys#list-post
func (c *KeyService) Create(ctx context.Context, data url.Values) (*Key, error) {
key := new(Key)
err := c.client.CreateResource(ctx, keyPathPart, data, key)
return key, err
}
// Update the key with the given data. Valid parameters may be found here:
// https://www.twilio.com/docs/api/rest/keys#instance-post
func (a *KeyService) Update(ctx context.Context, sid string, data url.Values) (*Key, error) {
key := new(Key)
err := a.client.UpdateResource(ctx, keyPathPart, sid, data, key)
return key, err
}
// Delete the Key with the given sid. If the Key has already been
// deleted, or does not exist, Delete returns nil. If another error or a
// timeout occurs, the error is returned.
func (r *KeyService) Delete(ctx context.Context, sid string) error {
return r.client.DeleteResource(ctx, keyPathPart, sid)
}
// KeyPageIterator lets you retrieve consecutive pages of resources.
type KeyPageIterator struct {
p *PageIterator
}
// GetPageIterator returns a KeyPageIterator with the given page
// filters. Call iterator.Next() to get the first page of resources (and again
// to retrieve subsequent pages).
func (c *KeyService) GetPageIterator(data url.Values) *KeyPageIterator {
iter := NewPageIterator(c.client, data, keyPathPart)
return &KeyPageIterator{
p: iter,
}
}
// Next returns the next page of resources. If there are no more resources,
// NoMoreResults is returned.
func (c *KeyPageIterator) Next(ctx context.Context) (*KeyPage, error) {
kp := new(KeyPage)
err := c.p.Next(ctx, kp)
if err != nil {
return nil, err
}
c.p.SetNextPageURI(kp.NextPageURI)
return kp, nil
}