-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatch.go
175 lines (149 loc) · 6.65 KB
/
latch.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
package golatch
import (
"fmt"
"net/url"
t "time"
)
type Latch struct {
AppID string
SecretKey string
LatchAPI
}
//Constructs a new Latch struct
func NewLatch(appID string, secretKey string) *Latch {
return &Latch{
AppID: appID,
SecretKey: secretKey,
}
}
//Pairs an account with the provided pairing token
func (l *Latch) Pair(token string) (response *LatchPairResponse, err error) {
var resp *LatchResponse
if resp, err = l.DoRequest(NewLatchRequest(l.AppID, l.SecretKey, HTTP_METHOD_GET, GetLatchURL(fmt.Sprint(API_PAIR_ACTION, "/", token)), nil, nil, t.Now()), &LatchPairResponse{}); err == nil {
response = (*resp).(*LatchPairResponse)
}
return response, err
}
//Unpairs an account, given it's account ID
func (l *Latch) Unpair(accountId string) (err error) {
_, err = l.DoRequest(NewLatchRequest(l.AppID, l.SecretKey, HTTP_METHOD_GET, GetLatchURL(fmt.Sprint(API_UNPAIR_ACTION, "/", accountId)), nil, nil, t.Now()), nil)
return err
}
//Locks an account, given it's account ID
func (l *Latch) Lock(accountId string) (err error) {
_, err = l.DoRequest(NewLatchRequest(l.AppID, l.SecretKey, HTTP_METHOD_GET, GetLatchURL(fmt.Sprint(API_LOCK_ACTION, "/", accountId)), nil, nil, t.Now()), nil)
return err
}
//Unlocks an account, given it's account ID
func (l *Latch) Unlock(accountId string) (err error) {
_, err = l.DoRequest(NewLatchRequest(l.AppID, l.SecretKey, HTTP_METHOD_GET, GetLatchURL(fmt.Sprint(API_UNLOCK_ACTION, "/", accountId)), nil, nil, t.Now()), nil)
return err
}
//Locks an operation, given it's account ID and oeration ID
func (l *Latch) LockOperation(accountId string, operationId string) (err error) {
_, err = l.DoRequest(NewLatchRequest(l.AppID, l.SecretKey, HTTP_METHOD_GET, GetLatchURL(fmt.Sprint(API_LOCK_ACTION, "/", accountId, "/op/", operationId)), nil, nil, t.Now()), nil)
return err
}
//Unlocks an operation, given it's account ID and oeration ID
func (l *Latch) UnlockOperation(accountId string, operationId string) (err error) {
_, err = l.DoRequest(NewLatchRequest(l.AppID, l.SecretKey, HTTP_METHOD_GET, GetLatchURL(fmt.Sprint(API_UNLOCK_ACTION, "/", accountId, "/op/", operationId)), nil, nil, t.Now()), nil)
return err
}
//Adds a new operation
func (l *Latch) AddOperation(parentId string, name string, twoFactor string, lockOnRequest string) (response *LatchAddOperationResponse, err error) {
var resp *LatchResponse
params := url.Values{}
params.Set("parentId", parentId)
params.Set("name", name)
params.Set("two_factor", twoFactor)
params.Set("lock_on_request", lockOnRequest)
if resp, err = l.DoRequest(NewLatchRequest(l.AppID, l.SecretKey, HTTP_METHOD_PUT, GetLatchURL(API_OPERATION_ACTION), nil, params, t.Now()), &LatchAddOperationResponse{}); err == nil {
response = (*resp).(*LatchAddOperationResponse)
}
return response, err
}
//Updates an existing operation
func (l *Latch) UpdateOperation(operationId string, name string, twoFactor string, lockOnRequest string) (err error) {
params := url.Values{}
params.Set("name", name)
if twoFactor != NOT_SET {
params.Set("two_factor", twoFactor)
}
if lockOnRequest != NOT_SET {
params.Set("lock_on_request", lockOnRequest)
}
_, err = l.DoRequest(NewLatchRequest(l.AppID, l.SecretKey, HTTP_METHOD_POST, GetLatchURL(fmt.Sprint(API_OPERATION_ACTION, "/", operationId)), nil, params, t.Now()), nil)
return err
}
//Deletes an existing operation
func (l *Latch) DeleteOperation(operationId string) (err error) {
_, err = l.DoRequest(NewLatchRequest(l.AppID, l.SecretKey, HTTP_METHOD_DELETE, GetLatchURL(fmt.Sprint(API_OPERATION_ACTION, "/", operationId)), nil, nil, t.Now()), nil)
return err
}
//Shows operations information
//If operationId is empty this function will retrieve all the operations of the app
func (l *Latch) ShowOperation(operationId string) (response *LatchShowOperationResponse, err error) {
var resp *LatchResponse
var operation string
if operationId != "" {
operation += "/" + operationId
}
if resp, err = l.DoRequest(NewLatchRequest(l.AppID, l.SecretKey, HTTP_METHOD_GET, GetLatchURL(fmt.Sprint(API_OPERATION_ACTION, operation)), nil, nil, t.Now()), &LatchShowOperationResponse{}); err == nil {
response = (*resp).(*LatchShowOperationResponse)
}
return response, err
}
//Gets the status of an account, given it's account ID
//If nootp is true, the one time password won't be included in the response
//If silent is true Latch will not send push notifications to the client (requires SILVER, GOLD or PLATINUM subscription)
func (l *Latch) Status(accountId string, nootp bool, silent bool) (response *LatchStatusResponse, err error) {
query := fmt.Sprint(API_CHECK_STATUS_ACTION, "/", accountId)
if nootp {
query = fmt.Sprint(query, "/", API_NOOTP_SUFFIX)
}
if silent {
query = fmt.Sprint(query, "/", API_SILENT_SUFFIX)
}
return l.StatusRequest(query)
}
//Gets the status of an operation, given it's account ID and operation ID
//If nootp is true, the one time password won't be included in the response
//If silent is true Latch will not send push notifications to the client (requires SILVER, GOLD or PLATINUM subscription)
func (l *Latch) OperationStatus(accountId string, operationId string, nootp bool, silent bool) (response *LatchStatusResponse, err error) {
query := fmt.Sprint(API_CHECK_STATUS_ACTION, "/", accountId, "/op/", operationId)
if nootp {
query = fmt.Sprint(query, "/", API_NOOTP_SUFFIX)
}
if silent {
query = fmt.Sprint(query, "/", API_SILENT_SUFFIX)
}
return l.StatusRequest(query)
}
//Performs a status request (application or operation) against the query URL provided
//Returns a LatchStatusResponse struct on success
func (l *Latch) StatusRequest(query string) (response *LatchStatusResponse, err error) {
var resp *LatchResponse
if resp, err = l.DoRequest(NewLatchRequest(l.AppID, l.SecretKey, HTTP_METHOD_GET, GetLatchURL(query), nil, nil, t.Now()), &LatchStatusResponse{}); err == nil {
response = (*resp).(*LatchStatusResponse)
}
return response, err
}
//Gets the account's history between the from and to dates
func (l *Latch) History(accountId string, from t.Time, to t.Time) (response *LatchHistoryResponse, err error) {
var resp *LatchResponse
query := fmt.Sprintf("%s/%s", API_HISTORY_ACTION, accountId)
if !from.IsZero() || !to.IsZero() {
if !from.IsZero() {
query = fmt.Sprint(query, fmt.Sprintf("/%d", from.UnixNano()/1000000))
} else {
query = fmt.Sprint(query, fmt.Sprintf("/%d", 0))
}
}
if !to.IsZero() {
query = fmt.Sprint(query, fmt.Sprintf("/%d", to.UnixNano()/1000000))
}
if resp, err = l.DoRequest(NewLatchRequest(l.AppID, l.SecretKey, HTTP_METHOD_GET, GetLatchURL(query), nil, nil, t.Now()), &LatchHistoryResponse{AppID: l.AppID}); err == nil {
response = (*resp).(*LatchHistoryResponse)
}
return response, err
}