/
streams.go
248 lines (201 loc) · 6.25 KB
/
streams.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
238
239
240
241
242
243
244
245
246
247
248
package filespot
import (
"context"
"net/http"
)
const streamsBasePath = "/1/streams"
// StreamsService implements interface with API /streams endpoint.
// See https://doc.platformcraft.ru/filespot/api/en/#streams
type StreamsService interface {
List(context.Context) ([]Stream, *http.Response, error)
Get(context.Context, string) (*Stream, *http.Response, error)
Create(context.Context, *StreamCreateRequest) (*Stream, *http.Response, error)
Delete(context.Context, string) (*http.Response, error)
Start(context.Context, string, *StreamStartRequest) (*http.Response, error)
Stop(context.Context, string) ([]File, *http.Response, error)
CreateSchedule(context.Context, string) (string, *http.Response, error)
Rec(context.Context, string) (*Record, *http.Response, error)
DeleteSchedule(context.Context, string, string) (*http.Response, error)
}
// StreamsCli handles communication with API
type StreamsCli struct {
client *Client
}
// Stream represents a platformcraft Stream
type Stream struct {
ID string `json:"id"`
User string `json:"user"`
Name string `json:"name"`
URL string `json:"url"`
IsInstantRecording bool `json:"is_instant_recording"`
}
// File represents a platformcraft Record File
type File struct {
ID string `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
Size int `json:"size"`
ContentType string `json:"content_type"`
CreateDate string `json:"create_date"`
LatestUpdate string `json:"latest_update"`
ResourceURL string `json:"resource_url"`
Video string `json:"video"`
CDNURL string `json:"cdn_url"`
Status string `json:"status"`
}
// Record represents a platformcraft Record
type Record struct {
Status string `json:"status"`
Files []string `json:"files"`
}
// streamsRoot represents a List root
type streamsRoot struct {
Streams []Stream `json:"streams"`
}
// streamRoot represents a Get root
type streamRoot struct {
Stream *Stream `json:"stream"`
}
// stopRoot represents a Stop root
type stopRoot struct {
Files []File `json:"files"`
}
// recordRoot represents a Rec root
type recordRoot struct {
Record *Record `json:"record"`
}
// StreamCreateRequest identifies Stream for the Create request
type StreamCreateRequest struct {
Name string `json:"name"`
URL string `json:"url"`
}
// StreamStartRequest identifies streaming process for the Start request
type StreamStartRequest struct {
StopTimeout int `json:"stop_timeout"`
}
// List of Streams
func (c StreamsCli) List(ctx context.Context) ([]Stream, *http.Response, error) {
req, err := c.client.NewRequest(ctx, http.MethodGet, streamsBasePath, nil)
if err != nil {
return nil, nil, err
}
data := new(streamsRoot)
resp, err := c.client.Do(ctx, req, data)
if err != nil {
return nil, resp, err
}
return data.Streams, resp, err
}
// Get Stream
func (c StreamsCli) Get(ctx context.Context, id string) (*Stream, *http.Response, error) {
endpointURL := streamsBasePath + "/" + id
req, err := c.client.NewRequest(ctx, http.MethodGet, endpointURL, nil)
if err != nil {
return nil, nil, err
}
data := new(streamRoot)
resp, err := c.client.Do(ctx, req, data)
if err != nil {
return nil, resp, err
}
return data.Stream, resp, err
}
// Create Stream
func (c StreamsCli) Create(ctx context.Context, streamCreateRequest *StreamCreateRequest) (*Stream, *http.Response, error) {
req, err := c.client.NewRequest(ctx, http.MethodPost, streamsBasePath, streamCreateRequest)
if err != nil {
return nil, nil, err
}
data := new(streamRoot)
resp, err := c.client.Do(ctx, req, data)
if err != nil {
return nil, resp, err
}
return data.Stream, resp, err
}
// Delete Stream
func (c StreamsCli) Delete(ctx context.Context, id string) (*http.Response, error) {
endpointURL := streamsBasePath + "/" + id
req, err := c.client.NewRequest(ctx, http.MethodDelete, endpointURL, nil)
if err != nil {
return nil, err
}
data := &struct{}{}
resp, err := c.client.Do(ctx, req, data)
if err != nil {
return resp, err
}
return resp, err
}
// Start Stream
func (c StreamsCli) Start(ctx context.Context, id string, streamStartRequest *StreamStartRequest) (*http.Response, error) {
endpointURL := streamsBasePath + "/rec/instant/start/" + id
req, err := c.client.NewRequest(ctx, http.MethodPost, endpointURL, streamStartRequest)
if err != nil {
return nil, err
}
data := &struct{}{}
resp, err := c.client.Do(ctx, req, data)
if err != nil {
return resp, err
}
return resp, err
}
// Stop Stream
func (c StreamsCli) Stop(ctx context.Context, id string) ([]File, *http.Response, error) {
endpointURL := streamsBasePath + "/rec/instant/stop/" + id
req, err := c.client.NewRequest(ctx, http.MethodPost, endpointURL, nil)
if err != nil {
return nil, nil, err
}
data := new(stopRoot)
resp, err := c.client.Do(ctx, req, data)
if err != nil {
return nil, resp, err
}
return data.Files, resp, err
}
// CreateSchedule returns record_id
func (c StreamsCli) CreateSchedule(ctx context.Context, id string) (string, *http.Response, error) {
endpointURL := streamsBasePath + "/rec/schedule/new/" + id
req, err := c.client.NewRequest(ctx, http.MethodPost, endpointURL, nil)
if err != nil {
return "", nil, err
}
data := &struct {
RecordID string `json:"record_id"`
}{}
resp, err := c.client.Do(ctx, req, data)
if err != nil {
return "", resp, err
}
return data.RecordID, resp, err
}
// Rec returns Record
func (c StreamsCli) Rec(ctx context.Context, id string) (*Record, *http.Response, error) {
endpointURL := streamsBasePath + "/rec/" + id
req, err := c.client.NewRequest(ctx, http.MethodGet, endpointURL, nil)
if err != nil {
return nil, nil, err
}
data := new(recordRoot)
resp, err := c.client.Do(ctx, req, data)
if err != nil {
return nil, resp, err
}
return data.Record, resp, err
}
// DeleteSchedule deletes record
func (c StreamsCli) DeleteSchedule(ctx context.Context, streamID string, recordID string) (*http.Response, error) {
endpointURL := streamsBasePath + "/rec/schedule/del/" + streamID + "/" + recordID
req, err := c.client.NewRequest(ctx, http.MethodDelete, endpointURL, nil)
if err != nil {
return nil, err
}
data := &struct{}{}
resp, err := c.client.Do(ctx, req, data)
if err != nil {
return resp, err
}
return resp, err
}