-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
670 lines (558 loc) · 15 KB
/
client.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
package w3streamsdk
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"mime/multipart"
"net/http"
"net/url"
"path"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
type Client struct {
BaseURL *url.URL
PublicKey string
SecretKey string
httpClient Doer
chunkSize int64
applicationName string
applicationVersion string
sdkName string
sdkVersion string
BearerToken string
ApiKey ApiKeyServiceI
LiveStream LiveStreamServiceI
Players PlayersServiceI
Playlist PlaylistServiceI
Video VideoServiceI
VideoChapter VideoChapterServiceI
Watermark WatermarkServiceI
Webhook WebhookServiceI
}
type ErrorResponse struct {
Response *http.Response
Body []byte
}
func (r *ErrorResponse) Error() string {
var bodyStr string
if len(r.Body) > 0 {
var jsonBody interface{}
if err := json.Unmarshal(r.Body, &jsonBody); err == nil {
prettyJSON, err := json.MarshalIndent(jsonBody, "", " ")
if err == nil {
bodyStr = string(prettyJSON)
}
}
if bodyStr == "" {
bodyStr = string(r.Body)
}
} else {
bodyStr = "Empty body"
}
return fmt.Sprintf(
"[%d]: %v %v\n %s\n",
r.Response.StatusCode,
r.Response.Request.Method,
r.Response.Request.URL,
bodyStr,
)
}
type AuthCredentials struct {
PublicKey string `json:"public_key"`
SecretKey string `json:"secret_key"`
BearerToken string `json:"bearer_token"`
}
const (
defaultBaseURL = "https://api.w3stream.xyz/api/"
defaultChunkSize = 50 * 1024 * 1024
minChunkSize = 5 * 1024 * 1024
maxChunkSize = 128 * 1024 * 1024
)
type Doer interface {
Do(*http.Request) (*http.Response, error)
}
type Builder struct {
publicKey string
secretKey string
baseURL string
uploadChunkSize int64
httpClient Doer
applicationName string
applicationVersion string
sdkName string
sdkVersion string
bearerToken string
}
func (cb *Builder) BaseURL(url string) *Builder {
cb.baseURL = url
return cb
}
func (cb *Builder) SecretKey(key string) *Builder {
cb.secretKey = key
return cb
}
func (cb *Builder) PublicKey(secret string) *Builder {
cb.publicKey = secret
return cb
}
func (cb *Builder) BearerToken(token string) *Builder {
cb.bearerToken = token
return cb
}
func (cb *Builder) ApplicationName(applicationName string) *Builder {
cb.applicationName = applicationName
return cb
}
func (cb *Builder) ApplicationVersion(applicationVersion string) *Builder {
cb.applicationVersion = applicationVersion
return cb
}
func (cb *Builder) SdkName(sdkName string) *Builder {
cb.sdkName = sdkName
return cb
}
func (cb *Builder) SdkVersion(sdkVersion string) *Builder {
cb.sdkVersion = sdkVersion
return cb
}
func (cb *Builder) UploadChunkSize(size int64) *Builder {
if size < minChunkSize {
cb.uploadChunkSize = minChunkSize
} else if size > maxChunkSize {
cb.uploadChunkSize = maxChunkSize
} else {
cb.uploadChunkSize = size
}
return cb
}
func (cb *Builder) HTTPClient(httpClient Doer) *Builder {
cb.httpClient = httpClient
return cb
}
func ClientBuilder(credentials AuthCredentials) *Builder {
builder := &Builder{
baseURL: defaultBaseURL,
uploadChunkSize: defaultChunkSize,
}
if credentials.BearerToken != "" {
builder.bearerToken = credentials.BearerToken
} else {
builder.secretKey = credentials.SecretKey
builder.publicKey = credentials.PublicKey
}
return builder
}
func (cb *Builder) Build() *Client {
baseURL, _ := url.Parse(cb.baseURL)
var httpClient Doer
if cb.httpClient == nil {
httpClient = http.DefaultClient
} else {
httpClient = cb.httpClient
}
c := &Client{
BaseURL: baseURL,
SecretKey: cb.secretKey,
PublicKey: cb.publicKey,
httpClient: httpClient,
chunkSize: cb.uploadChunkSize,
applicationName: cb.applicationName,
applicationVersion: cb.applicationVersion,
sdkName: cb.sdkName,
sdkVersion: cb.sdkVersion,
BearerToken: cb.bearerToken,
}
c.ApiKey = &ApiKeyService{client: c}
c.LiveStream = &LiveStreamService{client: c}
c.Players = &PlayersService{client: c}
c.Playlist = &PlaylistService{client: c}
c.Video = &VideoService{client: c}
c.VideoChapter = &VideoChapterService{client: c}
c.Watermark = &WatermarkService{client: c}
c.Webhook = &WebhookService{client: c}
return c
}
// ChunkSize changes chunk size for video upload, by default its 50MB
func (c *Client) ChunkSize(size int64) {
c.chunkSize = size
}
func (c *Client) UploadVideo(ctx context.Context, id string, fileName string, fileReader io.Reader, fileSize int64) error {
localVarPath := "/videos/{id}/part"
localVarPath = strings.Replace(localVarPath, "{"+"id"+"}", url.PathEscape(parameterToString(id, "")), -1)
localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := make(map[string]string)
requests, err := c.prepareRangeRequests(ctx, localVarPath, fileName, fileReader, fileSize, localVarHeaderParams, localVarQueryParams, localVarFormParams)
if err != nil {
return err
}
res := new(ResponseSuccess)
for _, req := range requests {
_, err = c.do(req, res)
if err != nil {
return err
}
}
localVarPath = "/videos/{id}/complete"
localVarPath = strings.Replace(localVarPath, "{"+"id"+"}", url.PathEscape(parameterToString(id, "")), -1)
localVarHeaderParams = make(map[string]string)
localVarQueryParams = url.Values{}
var localVarPostBody interface{}
req, err := c.prepareRequest(ctx, http.MethodGet, localVarPath, localVarPostBody, localVarHeaderParams, localVarQueryParams)
if err != nil {
return err
}
res = new(ResponseSuccess)
_, err = c.do(req, res)
if err != nil {
return err
}
return nil
}
func (c *Client) prepareRequest(
ctx context.Context,
method,
urlStr string,
body interface{},
headerParams map[string]string,
queryParams url.Values) (*http.Request, error) {
u, err := url.Parse(c.BaseURL.String())
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, urlStr)
query := u.Query()
for k, v := range queryParams {
for _, iv := range v {
query.Add(k, iv)
}
}
u.RawQuery = query.Encode()
buf := new(bytes.Buffer)
if body != nil {
switch v := body.(type) {
case *bytes.Buffer:
buf = v
default:
err = json.NewEncoder(buf).Encode(body)
if err != nil {
return nil, err
}
}
}
req, err := http.NewRequestWithContext(ctx, method, u.String(), buf)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
for headerName := range headerParams {
req.Header.Set(headerName, headerParams[headerName])
}
if c.BearerToken != "" {
req.Header.Set("Authorization", "Bearer "+c.BearerToken)
} else {
req.SetBasicAuth(c.PublicKey, c.SecretKey)
}
return req, nil
}
func getOriginHeaderValue(name string, version string) (string, error) {
if name == "" && version == "" {
return "", nil
}
var re = regexp.MustCompile(`(?m)^[\w-]{1,50}$`)
if !re.MatchString(name) {
return "", errors.New("Invalid name value. Allowed characters: A-Z, a-z, 0-9, '-', '_'. Max length: 50.")
}
var reVersion = regexp.MustCompile(`(?m)^\d{1,3}(\.\d{1,3}(\.\d{1,3})?)?$`)
if !reVersion.MatchString(version) {
return "", errors.New("Invalid version value. The version should match the xxx[.yyy][.zzz] pattern.")
}
return name + ":" + version, nil
}
func (c *Client) prepareProgressiveUploadRequest(
ctx context.Context,
urlStr string,
fileName string,
fileReader io.Reader,
fileSize int64,
headerParams map[string]string,
queryParams url.Values,
formParams map[string]string,
part int32,
isLast bool) (*http.Request, error) {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
for key, val := range formParams {
err := writer.WriteField(key, val)
if err != nil {
return nil, err
}
}
partWriter, err := writer.CreateFormFile("file", fileName)
if err != nil {
return nil, err
}
io.Copy(partWriter, fileReader)
if err != nil {
return nil, err
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := c.prepareRequest(ctx, http.MethodPost, urlStr, body, headerParams, queryParams)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
var ranges string
if isLast {
ranges = fmt.Sprintf("part %d/%d", part, part)
} else {
ranges = fmt.Sprintf("part %d/*", part)
}
req.Header.Set("Content-Range", ranges)
return req, nil
}
func (c *Client) prepareRangeRequests(ctx context.Context, urlStr string, fileName string, fileReader io.Reader, fileSize int64, headerParams map[string]string, queryParams url.Values, formParams map[string]string) ([]*http.Request, error) {
chunkSize := int64(50 * 1024 * 1024)
var requests []*http.Request
partsCount := int(math.Ceil(float64(fileSize) / float64(chunkSize)))
startByte := int64(0)
if partsCount == 1 {
endByte := startByte + chunkSize - 1
if endByte >= fileSize {
endByte = fileSize - 1
}
bytesToRead := endByte - startByte + 1
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
for key, val := range formParams {
err := writer.WriteField(key, val)
if err != nil {
return nil, err
}
}
partWriter, err := writer.CreateFormFile("file", fileName)
if err != nil {
return nil, err
}
hash := md5.New()
multiWriter := io.MultiWriter(partWriter, hash)
limitedReader := io.LimitReader(fileReader, bytesToRead)
_, err = io.Copy(multiWriter, limitedReader)
if err != nil {
return nil, err
}
md5Hash := hex.EncodeToString(hash.Sum(nil))
if err := writer.WriteField("hash", md5Hash); err != nil {
return nil, err
}
if err := writer.WriteField("index", "1"); err != nil {
return nil, err
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := c.prepareRequest(ctx, http.MethodPost, urlStr, body, headerParams, queryParams)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
ranges := fmt.Sprintf("bytes %d-%d/%d", startByte, endByte, fileSize)
req.Header.Set("Content-Range", ranges)
requests = append(requests, req)
} else {
for part := 1; part <= partsCount; part++ {
var endByte int64
if part == partsCount {
endByte = fileSize - 1
} else {
endByte = startByte + chunkSize - 1
if endByte >= fileSize {
endByte = fileSize - 1
}
}
bytesToRead := endByte - startByte + 1
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
for key, val := range formParams {
err := writer.WriteField(key, val)
if err != nil {
return nil, err
}
}
partWriter, err := writer.CreateFormFile("file", fileName)
if err != nil {
return nil, err
}
hash := md5.New()
multiWriter := io.MultiWriter(partWriter, hash)
limitedReader := io.LimitReader(fileReader, bytesToRead)
_, err = io.Copy(multiWriter, limitedReader)
if err != nil {
return nil, err
}
md5Hash := hex.EncodeToString(hash.Sum(nil))
if err := writer.WriteField("hash", md5Hash); err != nil {
return nil, err
}
if err := writer.WriteField("index", strconv.Itoa(part)); err != nil {
return nil, err
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := c.prepareRequest(ctx, http.MethodPost, urlStr, body, headerParams, queryParams)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
ranges := fmt.Sprintf("bytes %d-%d/%d", startByte, endByte, fileSize)
req.Header.Set("Content-Range", ranges)
requests = append(requests, req)
startByte = endByte + 1
if startByte >= fileSize {
break
}
}
}
return requests, nil
}
func (c *Client) prepareUploadRequest(
ctx context.Context,
urlStr string,
fileName string,
fileReader io.Reader,
headerParams map[string]string,
queryParams url.Values,
formParams map[string]string) (*http.Request, error) {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
partWriter, err := writer.CreateFormFile("file", fileName)
if err != nil {
return nil, err
}
_, err = io.Copy(partWriter, fileReader)
if err != nil {
return nil, err
}
for key, val := range formParams {
err = writer.WriteField(key, val)
if err != nil {
return nil, err
}
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := c.prepareRequest(ctx, http.MethodPost, urlStr, body, headerParams, queryParams)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, nil
}
func (c *Client) do(req *http.Request, v interface{}) (*http.Response, error) {
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
err = checkResponse(resp)
if err != nil {
return nil, err
}
if v != nil {
err = json.NewDecoder(resp.Body).Decode(v)
if err != nil {
return nil, err
}
}
return resp, nil
}
func checkResponse(r *http.Response) error {
if c := r.StatusCode; c >= 200 && c <= 299 {
return nil
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %v", err)
}
defer r.Body.Close()
errorResponse := &ErrorResponse{
Response: r,
Body: body,
}
return errorResponse
}
// parameterToString convert interface{} parameters to string, using a delimiter if format is provided.
func parameterToString(obj interface{}, collectionFormat string) string {
var delimiter string
switch collectionFormat {
case "pipes":
delimiter = "|"
case "ssv":
delimiter = " "
case "tsv":
delimiter = "\t"
case "csv":
delimiter = ","
}
if reflect.TypeOf(obj).Kind() == reflect.Slice {
return strings.Trim(strings.Replace(fmt.Sprint(obj), " ", delimiter, -1), "[]")
} else if t, ok := obj.(time.Time); ok {
return t.Format(time.RFC3339)
}
return fmt.Sprintf("%v", obj)
}
func addDeepQueryParams(filter interface{}, prefix string, queryParams url.Values) {
v := reflect.ValueOf(filter)
if v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
}
switch v.Kind() {
case reflect.Struct:
t := v.Type()
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
fieldType := t.Field(i)
if field.Kind() == reflect.Ptr && !field.IsNil() {
field = field.Elem()
}
lowercaseFirstChar := strings.ToLower(string(fieldType.Name[0]))
restOfString := fieldType.Name[1:]
lowercaseName := lowercaseFirstChar + restOfString
if field.Kind() == reflect.Slice {
for j := 0; j < field.Len(); j++ {
item := field.Index(j)
queryParams.Add(fmt.Sprintf("%s[%s][%d]", prefix, lowercaseName, j), item.String())
}
} else if field.Kind() == reflect.String {
queryParams.Add(fmt.Sprintf("%s[%s]", prefix, lowercaseName), field.String())
}
}
case reflect.Map:
for _, key := range v.MapKeys() {
val := v.MapIndex(key)
queryParams.Add(fmt.Sprintf("%s[%s]", prefix, key.String()), fmt.Sprintf("%v", val))
}
default:
fmt.Println("Unsupported type")
}
}