/
videobox.go
82 lines (73 loc) · 2.2 KB
/
videobox.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
// Package videobox provides a client for accessing Videobox services.
package videobox
import (
"encoding/json"
"net/http"
"net/url"
"time"
"github.com/machinebox/sdk-go/boxutil"
"github.com/pkg/errors"
)
// Video represents a video.
type Video struct {
ID string `json:"id"`
Status VideoStatus `json:"status"`
Error string `json:"error"`
DownloadTotal int64 `json:"downloadTotal,omitempty"`
DownloadComplete int64 `json:"downloadComplete,omitempty"`
DownloadEstimatedCompletion *time.Time `json:"downloadCompleteEstimate,omitempty"`
TotalFrames int `json:"framesCount,omitempty"`
FramesComplete int `json:"framesComplete"`
LastFrameBase64 string `json:"lastFrameBase64,omitempty"`
MillisecondsComplete int `json:"millisecondsComplete"`
Expires *time.Time `json:"expires,omitempty"`
}
// Client is an HTTP client that can make requests to the box.
type Client struct {
addr string
// HTTPClient is the http.Client that will be used to
// make requests.
HTTPClient *http.Client
}
// New makes a new Client.
func New(addr string) *Client {
return &Client{
addr: addr,
HTTPClient: &http.Client{
Timeout: 10 * time.Second,
},
}
}
// Info gets the details about the box.
func (c *Client) Info() (*boxutil.Info, error) {
var info boxutil.Info
u, err := url.Parse(c.addr + "/info")
if err != nil {
return nil, err
}
if !u.IsAbs() {
return nil, errors.New("box address must be absolute")
}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json; charset=utf-8")
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, errors.New(resp.Status)
}
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
return nil, err
}
return &info, nil
}
// ErrVideobox represents an error from Videobox.
type ErrVideobox string
func (e ErrVideobox) Error() string {
return "videobox: " + string(e)
}