-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatch_user.go
80 lines (65 loc) · 2.76 KB
/
latch_user.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
package golatch
import (
"fmt"
"net/url"
t "time"
)
//Struct to use the Latch User API
type LatchUser struct {
UserID string
SecretKey string
LatchAPI
}
//Constructs a new LatchUser struct
func NewLatchUser(userID string, secretKey string) *LatchUser {
return &LatchUser{
UserID: userID,
SecretKey: secretKey,
}
}
//Gets the user's subscription information
func (l *LatchUser) Subscription() (response *LatchSubscriptionResponse, err error) {
var resp *LatchResponse
if resp, err = l.DoRequest(NewLatchRequest(l.UserID, l.SecretKey, HTTP_METHOD_GET, GetLatchURL(API_SUBSCRIPTION_ACTION), nil, nil, t.Now()), &LatchSubscriptionResponse{}); err == nil {
response = (*resp).(*LatchSubscriptionResponse)
}
return response, err
}
//Shows existing applications
func (l *LatchUser) ShowApplications() (response *LatchShowApplicationsResponse, err error) {
var resp *LatchResponse
if resp, err = l.DoRequest(NewLatchRequest(l.UserID, l.SecretKey, HTTP_METHOD_GET, GetLatchURL(API_APPLICATION_ACTION), nil, nil, t.Now()), &LatchShowApplicationsResponse{}); err == nil {
response = (*resp).(*LatchShowApplicationsResponse)
}
return response, err
}
//Adds a new application
func (l *LatchUser) AddApplication(applicationInfo *LatchApplicationInfo) (response *LatchAddApplicationResponse, err error) {
var resp *LatchResponse
params := prepareApplicationParams(applicationInfo)
if resp, err = l.DoRequest(NewLatchRequest(l.UserID, l.SecretKey, HTTP_METHOD_PUT, GetLatchURL(API_APPLICATION_ACTION), nil, *params, t.Now()), &LatchAddApplicationResponse{}); err == nil {
response = (*resp).(*LatchAddApplicationResponse)
}
return response, err
}
//Updates application information
func (l *LatchUser) UpdateApplication(appID string, applicationInfo *LatchApplicationInfo) (err error) {
params := prepareApplicationParams(applicationInfo)
_, err = l.DoRequest(NewLatchRequest(l.UserID, l.SecretKey, HTTP_METHOD_POST, GetLatchURL(fmt.Sprint(API_APPLICATION_ACTION, "/", appID)), nil, *params, t.Now()), nil)
return err
}
//Deletes an existing application
func (l *LatchUser) DeleteApplication(applicationId string) (err error) {
_, err = l.DoRequest(NewLatchRequest(l.UserID, l.SecretKey, HTTP_METHOD_DELETE, GetLatchURL(fmt.Sprint(API_APPLICATION_ACTION, "/", applicationId)), nil, nil, t.Now()), nil)
return err
}
//Initializes params for adding/updating application information
func prepareApplicationParams(applicationInfo *LatchApplicationInfo) (params *url.Values) {
params = &url.Values{}
params.Set("name", applicationInfo.Name)
params.Set("contactEmail", applicationInfo.ContactEmail)
params.Set("contactPhone", applicationInfo.ContactPhone)
params.Set("two_factor", applicationInfo.TwoFactor)
params.Set("lock_on_request", applicationInfo.LockOnRequest)
return params
}