forked from calvertyang/opentok-go-sdk
/
opentok.go
170 lines (136 loc) · 4.18 KB
/
opentok.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
package opentok
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"time"
jwt "github.com/golang-jwt/jwt/v4"
"github.com/google/uuid"
)
// OpenTok API host URL.
const defaultAPIHost = "https://api.opentok.com"
// For use in X-TB-TOKEN-AUTH header value.
const tokenSentinel = "T1=="
type issueType string
const (
// For most REST API calls, set issue type to "project".
projectToken issueType = "project"
// For Account Management REST methods, set issue type to "account".
accountToken issueType = "account"
)
// HTTPClient is an interface to allow custom clients and timeouts.
type HTTPClient interface {
Do(r *http.Request) (*http.Response, error)
}
// ResponseError encloses an error with code and message.
type ResponseError struct {
// StatusCode is the HTTP Response StatusCode that led to the error.
StatusCode int
// Message is the error message.
Message string `json:"message"`
}
// Error returns a formatted error message.
func (e *ResponseError) Error() string {
return fmt.Sprintf("TokBox error: code: %d; message: %s", e.StatusCode, e.Message)
}
// OpenTok stores the API key and secret for use in making API call.
type OpenTok struct {
apiKey string
apiSecret string
apiHost string
debug bool
httpClient HTTPClient
}
// New returns an initialized OpenTok instance with the API key and API secret.
func New(apiKey, apiSecret string) *OpenTok {
return &OpenTok{
apiKey: apiKey,
apiSecret: apiSecret,
apiHost: defaultAPIHost,
debug: false,
httpClient: http.DefaultClient,
}
}
// SetAPIHost is used to set the OpenTok API Host to specific URL.
func (ot *OpenTok) SetAPIHost(url string) error {
if url == "" {
return fmt.Errorf("OpenTok API Host cannot be empty")
}
ot.apiHost = url
return nil
}
// Debug sets the debug flage to true.
// It will dump the request/response allows you easy to debug.
func (ot *OpenTok) Debug() {
ot.debug = true
}
// SetHTTPClient specifies http client, http.DefaultClient used by default.
func (ot *OpenTok) SetHTTPClient(client HTTPClient) {
if client != nil {
ot.httpClient = client
}
}
// Generate account-level JWT token for API calls.
func (ot *OpenTok) genAccountJWT() (string, error) {
return ot.jwtToken(accountToken)
}
// Generate project-level JWT token for API calls.
func (ot *OpenTok) genProjectJWT() (string, error) {
return ot.jwtToken(projectToken)
}
// Generate JWT token for API calls.
func (ot *OpenTok) jwtToken(ist issueType) (string, error) {
type OpenTokClaims struct {
Ist issueType `json:"ist,omitempty"`
jwt.StandardClaims
}
issuedAt := time.Now().UTC()
claims := OpenTokClaims{
ist,
jwt.StandardClaims{
Issuer: ot.apiKey,
IssuedAt: issuedAt.Unix(),
ExpiresAt: issuedAt.Add((5 * time.Minute)).Unix(), // The maximum allowed expiration time range is 5 minutes.
Id: uuid.New().String(),
},
}
// Create a new token object, specifying signing method and the claims
// you would like it to contain.
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
// Sign and get the complete encoded token as a string using the api secret
return token.SignedString([]byte(ot.apiSecret))
}
// Send HTTP request.
func (ot *OpenTok) sendRequest(ctx context.Context, req *http.Request) (*http.Response, error) {
req.Header.Add("User-Agent", userAgent)
// Dump request
if ot.debug {
fmt.Println("========== Request Begin ==========")
reqDump, _ := httputil.DumpRequest(req, true)
fmt.Println(string(reqDump))
fmt.Println("========== Request End ==========")
}
res, err := ot.httpClient.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
// Dump response
if ot.debug {
fmt.Println("========== Response Begin ==========")
resDump, _ := httputil.DumpResponse(res, true)
fmt.Println(string(resDump))
fmt.Println("========== Response End ==========")
}
return res, err
}
// Parse the error rresponse by custom error struct.
func parseErrorResponse(res *http.Response) error {
resErr := &ResponseError{}
if err := json.NewDecoder(res.Body).Decode(resErr); err != nil {
return fmt.Errorf("Error decoding response from Tokbox: statusCode: %d; %w", res.StatusCode, err)
}
resErr.StatusCode = res.StatusCode
return resErr
}