/
builder.go
64 lines (60 loc) · 1.75 KB
/
builder.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
package haivision
import (
"crypto/tls"
"log"
"net/http"
"github.com/go-resty/resty/v2"
)
// Builder is used to build a new haivision client
func BuildHaivision(url string, debug bool, username string, password string, header *HeaderConfigurator, insecure *bool) (IHaivisionClient, error) {
// init haivision
haivisionClient := &haivisionSdk{
Url: url,
restClient: resty.New(),
}
//
if debug {
haivisionClient.restClient.SetDebug(true)
haivisionClient.debug = true
log.Println("Debug mode is enabled for the haivision client ")
}
// You can override all below settings and options at request level if you want to
//--------------------------------------------------------------------------------
// Host URL for all request. So you can use relative URL in the request
haivisionClient.restClient.SetBaseURL(url)
if insecure != nil {
haivisionClient.restClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
log.Println("Insecure mode is enabled for the haivision client ")
}
respSessionId, err := haivisionClient.InitSession(username, password)
if err != nil {
return nil, err
}
// Set Cookie for all request
haivisionClient.restClient.SetCookie(&http.Cookie{
Name: "sessionID",
Value: respSessionId.Response.SessionID,
})
//
deviceResponse, err := haivisionClient.GetDeviceInfo()
if err != nil {
return nil, err
}
if deviceResponse != nil {
haivisionClient.DeviceID = (*deviceResponse)[0].ID
haivisionClient.HType = (*deviceResponse)[0].Type
}
//
if header != nil {
// Headers for all request
for h, v := range header.GetHeaders() {
haivisionClient.restClient.SetHeader(h, v)
}
}
return haivisionClient, nil
}
func (o *haivisionSdk) debugPrint(data interface{}) {
if o.debug {
log.Println(data)
}
}