Skip to content

Commit ea2d7a8

Browse files
authored
Merge pull request #78 from guilhermehubner/read-recording
feat(voice) implementing read recording
2 parents 8d28d48 + 87d28d2 commit ea2d7a8

File tree

3 files changed

+91
-18
lines changed

3 files changed

+91
-18
lines changed

voice/recording.go

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,34 @@ type jsonRecording struct {
6666
Links map[string]string `json:"_links"`
6767
}
6868

69+
// ReadRecording fetches a single Recording based on its call ID, leg ID and the recording ID.
70+
func ReadRecording(c *messagebird.Client, callID, legID, id string) (*Recording, error) {
71+
json := new(struct {
72+
Data []*Recording `json:"data"`
73+
})
74+
75+
if err := c.Request(json, http.MethodGet, fmt.Sprintf("%s/v1/calls/%s/legs/%s/recordings/%s",
76+
apiRoot, callID, legID, id), nil); err != nil {
77+
return nil, err
78+
}
79+
80+
return json.Data[0], nil
81+
}
82+
6983
// UnmarshalJSON implements the json.Unmarshaler interface.
7084
func (rec *Recording) UnmarshalJSON(data []byte) error {
71-
var raw jsonRecording
72-
if err := json.Unmarshal(data, &raw); err != nil {
85+
recording := new(jsonRecording)
86+
87+
if err := json.Unmarshal(data, recording); err != nil {
7388
return err
7489
}
75-
createdAt, err := time.Parse(time.RFC3339, raw.CreatedAt)
76-
if err != nil {
77-
return fmt.Errorf("unable to parse Recording CreatedAt: %v", err)
78-
}
79-
updatedAt, err := time.Parse(time.RFC3339, raw.UpdatedAt)
90+
91+
r, err := parseJSON(recording)
8092
if err != nil {
81-
return fmt.Errorf("unable to parse Recording UpdatedAt: %v", err)
82-
}
83-
*rec = Recording{
84-
ID: raw.ID,
85-
Format: raw.Format,
86-
LegID: raw.LegID,
87-
Status: RecordingStatus(raw.Status),
88-
Duration: time.Second * time.Duration(raw.Duration),
89-
CreatedAt: createdAt,
90-
UpdatedAt: updatedAt,
91-
Links: raw.Links,
93+
return err
9294
}
95+
96+
*rec = *r
9397
return nil
9498
}
9599

@@ -118,3 +122,25 @@ func (rec *Recording) DownloadFile(client *messagebird.Client) (io.ReadCloser, e
118122
}
119123
return resp.Body, nil
120124
}
125+
126+
func parseJSON(recording *jsonRecording) (*Recording, error) {
127+
createdAt, err := time.Parse(time.RFC3339, recording.CreatedAt)
128+
if err != nil {
129+
return nil, fmt.Errorf("unable to parse Recording CreatedAt: %v", err)
130+
}
131+
updatedAt, err := time.Parse(time.RFC3339, recording.UpdatedAt)
132+
if err != nil {
133+
return nil, fmt.Errorf("unable to parse Recording UpdatedAt: %v", err)
134+
}
135+
136+
return &Recording{
137+
ID: recording.ID,
138+
Format: recording.Format,
139+
LegID: recording.LegID,
140+
Status: RecordingStatus(recording.Status),
141+
Duration: time.Second * time.Duration(recording.Duration),
142+
CreatedAt: createdAt,
143+
UpdatedAt: updatedAt,
144+
Links: recording.Links,
145+
}, nil
146+
}

voice/recording_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"io/ioutil"
55
"net/http"
66
"testing"
7+
8+
"github.com/messagebird/go-rest-api/internal/mbtest"
79
)
810

911
func TestRecordingGetFile(t *testing.T) {
@@ -31,3 +33,25 @@ func TestRecordingGetFile(t *testing.T) {
3133
t.Fatalf("mismatched downloaded contents")
3234
}
3335
}
36+
37+
func TestReadRecording(t *testing.T) {
38+
mbtest.WillReturnTestdata(t, "recordingObject.json", http.StatusOK)
39+
client := mbtest.Client(t)
40+
41+
recording, err := ReadRecording(client, "callid", "legid", "recid")
42+
if err != nil {
43+
t.Fatalf("unexpected error read recording: %s", err)
44+
}
45+
46+
if recording.ID != "recid" {
47+
t.Fatalf("expect %s got %s", "recid", recording.ID)
48+
}
49+
if recording.LegID != "legid" {
50+
t.Fatalf("expect %s got %s", "legid", recording.LegID)
51+
}
52+
if recording.Status != RecordingStatusDone {
53+
t.Fatalf("expect %s got %s", RecordingStatusDone, recording.Status)
54+
}
55+
56+
mbtest.AssertEndpointCalled(t, http.MethodGet, "/v1/calls/callid/legs/legid/recordings/recid")
57+
}

voice/testdata/recordingObject.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"_links": {
3+
"self": "/calls/callid/legs/legid/recordings/recid",
4+
"transcriptions": "/calls/callid/legs/legid/recordings/recid/transcriptions?page=1"
5+
},
6+
"data": [
7+
{
8+
"id": "recid",
9+
"format": "wav",
10+
"legId": "legid",
11+
"status": "done",
12+
"duration": 6,
13+
"type": "call",
14+
"createdAt": "2020-03-10T13:11:31Z",
15+
"updatedAt": "2020-03-10T13:11:38Z",
16+
"deletedAt": null,
17+
"_links": {
18+
"file": "/recordings/recid.wav",
19+
"self": "/recordings/recid"
20+
}
21+
}
22+
]
23+
}

0 commit comments

Comments
 (0)