-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.go
68 lines (57 loc) · 1.48 KB
/
connect.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
package manager
import (
"crypto/tls"
"fmt"
"github.com/go-resty/resty/v2"
"strings"
"time"
)
var defaultConfigOptions = ConfigOptions{
APICallTimeout: 60 * time.Second,
IgnoreInvalidSSLCertificate: true,
}
// ConfigOptions contains some advanced settings on server communication.
type ConfigOptions struct {
APICallTimeout time.Duration
IgnoreInvalidSSLCertificate bool
}
// QnapSession is a container for our session state.
type QnapSession struct {
host string
sessionID string
conn *resty.Client
options *ConfigOptions
}
// String returns the session's hostname.
func (s *QnapSession) String() string {
return s.host
}
// Connect sets up our connection to the QNAP system.
func Connect(host, username, password string, configOptions *ConfigOptions) (*QnapSession, error) {
if !strings.HasPrefix(host, "http") {
host = fmt.Sprintf("https://%s", host)
}
if configOptions == nil {
configOptions = &defaultConfigOptions
}
// create the session
session := &QnapSession{
host: host,
conn: resty.New().SetHostURL(host).SetTimeout(configOptions.APICallTimeout),
options: configOptions,
}
// setup SSL certificate handling
if configOptions.IgnoreInvalidSSLCertificate {
session.conn.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
}
// perform login
err := session.Login(username, password)
if err != nil {
return nil, err
}
// done
return session, nil
}
func (s *QnapSession) Close() error {
return s.Logout()
}