forked from pjebs/tokbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokbox.go
executable file
·269 lines (235 loc) · 6.12 KB
/
tokbox.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
package tokbox
import (
"bytes"
"github.com/google/go-querystring/query"
"io/ioutil"
"net/http"
"net/url"
"encoding/json"
"fmt"
"strings"
"time"
"golang.org/x/net/context"
"github.com/dgrijalva/jwt-go"
"github.com/myesui/uuid"
)
const (
apiHost = "https://api.opentok.com"
apiSession = "/session/create"
)
const (
Days30 = 2592000 //30 * 24 * 60 * 60
Weeks1 = 604800 //7 * 24 * 60 * 60
Hours24 = 86400 //24 * 60 * 60
Hours2 = 7200 //60 * 60 * 2
Hours1 = 3600 //60 * 60
)
type MediaMode string
const (
/**
* The session will send streams using the OpenTok Media Router.
*/
MediaRouter MediaMode = "disabled"
/**
* The session will attempt send streams directly between clients. If clients cannot connect
* due to firewall restrictions, the session uses the OpenTok TURN server to relay streams.
*/
P2P = "enabled"
)
type Role string
const (
/**
* A publisher can publish streams, subscribe to streams, and signal.
*/
Publisher Role = "publisher"
/**
* A subscriber can only subscribe to streams.
*/
Subscriber = "subscriber"
/**
* In addition to the privileges granted to a publisher, in clients using the OpenTok.js 2.2
* library, a moderator can call the <code>forceUnpublish()</code> and
* <code>forceDisconnect()</code> method of the Session object.
*/
Moderator = "moderator"
)
type Tokbox struct {
Client *http.Client
apiKey string
partnerSecret string
BetaUrl string //Endpoint for Beta Programs
Archive ArchiveService // Archive sdk
Stream StreamService // Stream sdk
}
func New(apikey, partnerSecret string) *Tokbox {
//return &Tokbox{apikey, partnerSecret, "",nil}
tb := &Tokbox{
Client: http.DefaultClient,
apiKey: apikey,
partnerSecret: partnerSecret,
BetaUrl: "",
}
tb.Archive = &ArchiveServiceOp{tb}
tb.Stream = &StreamServiceOp{tb}
return tb
}
func (t *Tokbox) jwtToken() (string, error) {
type TokboxClaims struct {
Ist string `json:"ist,omitempty"`
jwt.StandardClaims
}
claims := TokboxClaims{
"project",
jwt.StandardClaims{
Issuer: t.apiKey,
IssuedAt: time.Now().UTC().Unix(),
ExpiresAt: time.Now().UTC().Unix() + (2 * 24 * 60 * 60), // 2 hours; //NB: The maximum allowed expiration time range is 5 minutes.
Id: uuid.NewV4().String(),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(t.partnerSecret))
}
// Creates a new tokbox session or returns an error.
// See README file for full documentation: https://github.com/pjebs/tokbox
// NOTE: ctx must be nil if *not* using Google App Engine
func (t *Tokbox) NewSession(location string, mm MediaMode, ctx ...context.Context) (*Session, error) {
params := url.Values{}
if len(location) > 0 {
params.Add("location", location)
}
params.Add("p2p.preference", string(mm))
var endpoint string
if t.BetaUrl == "" {
endpoint = apiHost
} else {
endpoint = t.BetaUrl
}
req, err := http.NewRequest("POST", endpoint+apiSession, strings.NewReader(params.Encode()))
if err != nil {
return nil, err
}
//Create jwt token
jwt, err := t.jwtToken()
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("X-OPENTOK-AUTH", jwt)
if len(ctx) == 0 {
ctx = append(ctx, nil)
}
res, err := client(ctx[0]).Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("Tokbox returns error code: %v", res.StatusCode)
}
var s []Session
if err = json.NewDecoder(res.Body).Decode(&s); err != nil {
return nil, err
}
if len(s) < 1 {
return nil, fmt.Errorf("Tokbox did not return a session")
}
o := s[0]
o.T = t
return &o, nil
}
func (this *Tokbox) NewRequest(method, urlStr string, body, options interface{}) (*http.Request, error) {
rel, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
if options != nil {
optionsQuery, err := query.Values(options)
if err != nil {
return nil, err
}
for k, values := range rel.Query() {
for _, v := range values {
optionsQuery.Add(k, v)
}
}
}
var js []byte = nil
if body != nil {
js, err = json.Marshal(body)
if err != nil {
return nil, err
}
}
req, err := http.NewRequest(method, rel.String(), bytes.NewBuffer(js))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
token, err := this.jwtToken()
if err != nil {
return nil, err
}
req.Header.Add("X-OPENTOK-AUTH", token)
return req, nil
}
func (this *Tokbox) CreateAndDo(method, path string, data, options, resource interface{}) error {
req, err := this.NewRequest(method, path, data, options)
if err != nil {
return err
}
err = this.Do(req, resource)
if err != nil {
return err
}
return nil
}
func (this *Tokbox) Do(req *http.Request, v interface{}) error {
resp, err := this.Client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
err = CheckResponseError(resp)
if err != nil {
return err
}
if v != nil {
decoder := json.NewDecoder(resp.Body)
err := decoder.Decode(&v)
if err != nil {
return err
}
}
return nil
}
func CheckResponseError(resp *http.Response) error {
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return nil
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
responseError := &ResponseError{}
err = json.Unmarshal(bodyBytes, responseError)
if err != nil {
responseError.Code = resp.StatusCode
responseError.Message = string(bodyBytes)
responseError.Description = codeErrDict[resp.StatusCode]
}
return responseError
}
func (this *Tokbox) Get(path string, resource, options interface{}) error {
return this.CreateAndDo("GET", path, nil, options, resource)
}
func (this *Tokbox) Post(path string, data, resource interface{}) error {
return this.CreateAndDo("POST", path, data, nil, resource)
}
func (this *Tokbox) Put(path string, data, resource interface{}) error {
return this.CreateAndDo("PUT", path, data, nil, resource)
}
func (this *Tokbox) Delete(path string, resource, options interface{}) error {
return this.CreateAndDo("DELETE", path, nil, options, resource)
}