forked from saintpete/twilio-go
-
Notifications
You must be signed in to change notification settings - Fork 69
/
recording.go
105 lines (91 loc) · 3.24 KB
/
recording.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
package twilio
import (
"context"
"net/url"
"strings"
)
type RecordingService struct {
client *Client
}
const recordingsPathPart = "Recordings"
type Recording struct {
Sid string `json:"sid"`
Duration TwilioDuration `json:"duration"`
CallSid string `json:"call_sid"`
Status Status `json:"status"`
Price string `json:"price"`
PriceUnit string `json:"price_unit"`
DateCreated TwilioTime `json:"date_created"`
AccountSid string `json:"account_sid"`
APIVersion string `json:"api_version"`
Channels uint `json:"channels"`
DateUpdated TwilioTime `json:"date_updated"`
URI string `json:"uri"`
}
// URL returns the URL that can be used to play this recording, based on the
// extension. No error is returned if you provide an invalid extension. As of
// October 2016, the valid values are ".wav" and ".mp3".
func (r *Recording) URL(extension string) string {
if !strings.HasPrefix(extension, ".") {
extension = "." + extension
}
return strings.Join([]string{BaseURL, r.APIVersion, "Accounts", r.AccountSid, recordingsPathPart, r.Sid + extension}, "/")
}
// FriendlyPrice flips the sign of the Price (which is usually reported from
// the API as a negative number) and adds an appropriate currency symbol in
// front of it. For example, a PriceUnit of "USD" and a Price of "-1.25" is
// reported as "$1.25".
func (r *Recording) FriendlyPrice() string {
if r == nil {
return ""
}
return price(r.PriceUnit, r.Price)
}
type RecordingPage struct {
Page
Recordings []*Recording
}
func (r *RecordingService) Get(ctx context.Context, sid string) (*Recording, error) {
recording := new(Recording)
err := r.client.GetResource(ctx, recordingsPathPart, sid, recording)
return recording, err
}
// Delete the Recording with the given sid. If the Recording has already been
// deleted, or does not exist, Delete returns nil. If another error or a
// timeout occurs, the error is returned.
func (r *RecordingService) Delete(ctx context.Context, sid string) error {
return r.client.DeleteResource(ctx, recordingsPathPart, sid)
}
func (r *RecordingService) GetPage(ctx context.Context, data url.Values) (*RecordingPage, error) {
iter := r.GetPageIterator(data)
return iter.Next(ctx)
}
func (r *RecordingService) GetTranscriptions(ctx context.Context, recordingSid string, data url.Values) (*TranscriptionPage, error) {
if data == nil {
data = url.Values{}
}
tp := new(TranscriptionPage)
err := r.client.ListResource(ctx, recordingsPathPart+"/"+recordingSid+"/Transcriptions", data, tp)
return tp, err
}
type RecordingPageIterator struct {
p *PageIterator
}
// GetPageIterator returns an iterator which can be used to retrieve pages.
func (r *RecordingService) GetPageIterator(data url.Values) *RecordingPageIterator {
iter := NewPageIterator(r.client, data, recordingsPathPart)
return &RecordingPageIterator{
p: iter,
}
}
// Next returns the next page of resources. If there are no more resources,
// NoMoreResults is returned.
func (r *RecordingPageIterator) Next(ctx context.Context) (*RecordingPage, error) {
rp := new(RecordingPage)
err := r.p.Next(ctx, rp)
if err != nil {
return nil, err
}
r.p.SetNextPageURI(rp.NextPageURI)
return rp, nil
}