forked from openfresh/wse-rest-library-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwowza.go
267 lines (233 loc) · 6.72 KB
/
wowza.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
package wserest
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"time"
"github.com/sebastien4/wse-rest-library-go/entity/application/helper"
"github.com/sebastien4/wse-rest-library-go/entity/base"
)
// VerbType is verb type
type VerbType int
const (
// POST is post method
POST VerbType = iota
// GET is get method
GET
// DELETE is delete method
DELETE
// PUT is put method
PUT
)
func (v VerbType) String() string {
switch v {
case POST:
return "POST"
case GET:
return "GET"
case DELETE:
return "DELETE"
case PUT:
return "PUT"
default:
return "Unknown"
}
}
type wowza struct {
skip map[string]interface{}
additional map[string]interface{}
settings *helper.Settings
props map[string]interface{}
baseURI string
}
func (w *wowza) init(settings *helper.Settings) {
w.settings = settings
w.skip = make(map[string]interface{})
w.additional = make(map[string]interface{})
w.props = make(map[string]interface{})
}
/*func (w *wowza) restURI() string {
return w.props["restURI"].(string)
}*/
func (w *wowza) setRestURI(uri string) {
w.props["restURI"] = uri
}
func (w *wowza) host() string {
return w.settings.Host()
}
func (w *wowza) serverInstance() string {
return w.settings.ServerInstance()
}
func (w *wowza) vHostInstance() string {
return w.settings.VhostInstance()
}
func (w *wowza) getEntities(args []base.Entity, baseURI string) []base.Entity {
var entities []base.Entity
for _, arg := range args {
if arg.RestURI() == "" {
if baseURI == "" {
arg.ResetURI()
} else {
arg.SetURI(baseURI)
}
}
entities = append(entities, arg)
}
return entities
}
func (w *wowza) debug(v ...interface{}) {
if w.settings.IsDebug() {
println(fmt.Sprint(v...))
}
}
func (w *wowza) debugf(format string, v ...interface{}) {
if w.settings.IsDebug() {
println(fmt.Sprintf(format, v...))
}
}
func (w *wowza) sendRequest(props map[string]interface{}, entities []base.Entity, verbType VerbType, queryParams string) (map[string]interface{}, error) {
if restURI, ok := props["restURI"].(string); ok {
for _, entity := range entities {
name := entity.EntityName()
props[name] = entity
}
jsonb, err := json.Marshal(props)
if err != nil {
return nil, err
}
if queryParams != "" {
restURI += "?" + queryParams
}
w.debugf("JSON REQUEST to %s with verb %s: %+v", restURI, verbType, props)
client := &http.Client{Timeout: 10 * time.Second}
req, err := http.NewRequest(verbType.String(), restURI, bytes.NewReader(jsonb))
req.Header.Add("Accept", "application/json; charset=utf-8")
req.Header.Add("Content-type", "application/json; charset=utf-8")
req.Header.Add("Content-Length", strconv.Itoa(len(jsonb)))
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if w.settings.IsUseDigest() && resp.StatusCode == http.StatusUnauthorized {
resp.Body.Close()
var (
auth *authorization
wa *wwwAuthenticate
waString string
)
if waString = resp.Header.Get("WWW-Authenticate"); waString == "" {
return nil, fmt.Errorf("failed to get WWW-Authenticate header, please check your server configuration")
}
wa = newWwwAuthenticate(waString)
dr := new(digestRequest)
dr.UpdateRequest(w.settings.Username(), w.settings.Password(), verbType.String(), restURI, string(jsonb))
dr.CertVal = true
dr.Wa = wa
if auth, err = newAuthorization(dr); err != nil {
return nil, err
}
req, err = http.NewRequest(verbType.String(), restURI, bytes.NewReader(jsonb))
req.Header.Add("Accept", "application/json; charset=utf-8")
req.Header.Add("Content-type", "application/json; charset=utf-8")
req.Header.Add("Content-Length", strconv.Itoa(len(jsonb)))
req.Header.Add("Authorization", auth.toString())
resp, err = client.Do(req)
if err != nil {
return nil, err
}
}
defer resp.Body.Close()
contents := make(map[string]interface{})
json.NewDecoder(resp.Body).Decode(&contents)
w.debugf("RETURN: %+v", contents)
w.skip = make(map[string]interface{})
w.additional = make(map[string]interface{})
return contents, nil
}
return nil, errors.New("no restURI")
}
func (w *wowza) sendRequestSeb(itf interface{}, props map[string]interface{}, entities []base.Entity, verbType VerbType, queryParams string) error {
if restURI, ok := props["restURI"].(string); ok {
for _, entity := range entities {
name := entity.EntityName()
props[name] = entity
}
jsonb, err := json.Marshal(props)
if err != nil {
return err
}
if queryParams != "" {
restURI += "?" + queryParams
}
w.debugf("JSON REQUEST to %s with verb %s: %+v", restURI, verbType, props)
client := &http.Client{Timeout: 10 * time.Second}
req, err := http.NewRequest(verbType.String(), restURI, bytes.NewReader(jsonb))
req.Header.Add("Accept", "application/json; charset=utf-8")
req.Header.Add("Content-type", "application/json; charset=utf-8")
req.Header.Add("Content-Length", strconv.Itoa(len(jsonb)))
resp, err := client.Do(req)
if err != nil {
return err
}
if w.settings.IsUseDigest() && resp.StatusCode == http.StatusUnauthorized {
resp.Body.Close()
var (
auth *authorization
wa *wwwAuthenticate
waString string
)
if waString = resp.Header.Get("WWW-Authenticate"); waString == "" {
return fmt.Errorf("failed to get WWW-Authenticate header, please check your server configuration")
}
wa = newWwwAuthenticate(waString)
dr := new(digestRequest)
dr.UpdateRequest(w.settings.Username(), w.settings.Password(), verbType.String(), restURI, string(jsonb))
dr.CertVal = true
dr.Wa = wa
if auth, err = newAuthorization(dr); err != nil {
return err
}
req, err = http.NewRequest(verbType.String(), restURI, bytes.NewReader(jsonb))
req.Header.Add("Accept", "application/json; charset=utf-8")
req.Header.Add("Content-type", "application/json; charset=utf-8")
req.Header.Add("Content-Length", strconv.Itoa(len(jsonb)))
req.Header.Add("Authorization", auth.toString())
resp, err = client.Do(req)
if err != nil {
return err
}
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(itf)
if err != nil {
return err
}
w.debugf("RETURN: %+v", itf)
w.skip = make(map[string]interface{})
w.additional = make(map[string]interface{})
return nil
}
return errors.New("no restURI")
}
func (w *wowza) AddAdditionalParameter(key string, value interface{}) {
w.additional[key] = value
}
func (w *wowza) AddSkipParameter(key string) {
w.skip[key] = true
}
func (w *wowza) preparePropertiesForRequest() map[string]interface{} {
newProps := make(map[string]interface{})
for k, v := range w.props {
if _, ok := w.skip[k]; ok {
continue
}
newProps[k] = v
}
for k, v := range w.additional {
newProps[k] = v
}
return newProps
}