-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
104 lines (79 loc) · 2.32 KB
/
service.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
// Copyright 2018 DataArt. All rights reserved.
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package devicehive_go
import (
"github.com/devicehive/devicehive-go/internal/transport"
"github.com/devicehive/devicehive-go/internal/transportadapter"
)
// Method uses access token directly to connect
// If access token is empty it will get access token by refresh token
// It will recreate access token on expiration by given refresh token
func ConnectWithToken(url, accessToken, refreshToken string, p *ConnectionParams) (*Client, *Error) {
c, err := connect(url, p)
if err != nil {
return nil, err
}
c.setRefreshToken(refreshToken)
if accessToken != "" {
return auth(accessToken, c)
}
accessToken, err = c.RefreshToken()
if err != nil {
return nil, err
}
return auth(accessToken, c)
}
// Method obtains access token by credentials and then connects
// It will recreate access token on expiration by given credentials
func ConnectWithCreds(url, login, password string, p *ConnectionParams) (*Client, *Error) {
c, err := connect(url, p)
if err != nil {
return nil, err
}
c.setCreds(login, password)
accTok, err := c.RefreshToken()
if err != nil {
return nil, err
}
return auth(accTok, c)
}
func connect(url string, p *ConnectionParams) (*Client, *Error) {
timeout := p.Timeout()
tspParams := createTransportParams(p)
tsp, tspErr := transport.Create(url, tspParams)
if tspErr != nil {
return nil, &Error{name: ConnectionFailedErr, reason: tspErr.Error()}
}
client := &Client{
transportAdapter: transportadapter.New(tsp),
PollingWaitTimeoutSeconds: DefaultPollingWaitTimeoutSeconds,
defaultRequestTimeout: timeout,
}
info, err := client.GetInfo()
if err == nil {
client.subscriptionTimestamp = info.ServerTimestamp.Time
}
return client, nil
}
func auth(accTok string, c *Client) (*Client, *Error) {
auth, err := c.authenticate(accTok)
if err != nil {
return nil, err
}
if auth {
return c, nil
}
return nil, nil
}
func createTransportParams(p *ConnectionParams) *transport.Params {
var tspParams *transport.Params
if p != nil {
tspParams = &transport.Params{
ReconnectionTries: p.ReconnectionTries,
ReconnectionInterval: p.ReconnectionInterval,
DefaultTimeout: p.Timeout(),
}
}
return tspParams
}