@@ -66,30 +66,34 @@ type jsonRecording struct {
66
66
Links map [string ]string `json:"_links"`
67
67
}
68
68
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
+
69
83
// UnmarshalJSON implements the json.Unmarshaler interface.
70
84
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 {
73
88
return err
74
89
}
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 )
80
92
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
92
94
}
95
+
96
+ * rec = * r
93
97
return nil
94
98
}
95
99
@@ -118,3 +122,25 @@ func (rec *Recording) DownloadFile(client *messagebird.Client) (io.ReadCloser, e
118
122
}
119
123
return resp .Body , nil
120
124
}
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
+ }
0 commit comments