-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
169 lines (134 loc) · 3.76 KB
/
api.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
package august
import (
"bytes"
"encoding/json"
"io"
"net/http"
)
const (
apiBase = "https://api-production.august.com"
tokenHeader = "x-august-access-token"
)
// ErrorGeneric describes a generic error.
type ErrorGeneric struct {
Message string
}
// Error formats output.
func (e *ErrorGeneric) Error() string {
return e.Message
}
// ErrorUnAuth describes a probable expired token error.
type ErrorUnAuth struct {
}
// Error formats output.
func (*ErrorUnAuth) Error() string {
return "forbidden"
}
// Adds default headers.
func addHeaders(r *http.Request, token string) {
r.Header.Add("Accept-Version", "0.0.1")
r.Header.Add("x-august-api-key", "79fd0eb6-381d-4adf-95a0-47721289d1d9")
r.Header.Add("x-kease-api-key", "79fd0eb6-381d-4adf-95a0-47721289d1d9")
r.Header.Add("x-kease-api-key", "79fd0eb6-381d-4adf-95a0-47721289d1d9")
r.Header.Add("Content-Type", "application/json")
if "" != token {
r.Header.Add(tokenHeader, token)
}
}
// Performs August API call.
func doAPICall(a *Authenticator, method string, url string, reader io.Reader) (*http.Response, error) {
client := &http.Client{}
req, err := http.NewRequest(method, apiBase+url, reader)
addHeaders(req, a.State.AccessToken)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusUnauthorized {
return nil, &ErrorUnAuth{}
}
return nil, &ErrorGeneric{Message: "bad response"}
}
return resp, err
}
// APIProvider defines August APIs.
type APIProvider struct {
auth *Authenticator
}
// NewAPIProvider constructs a new API provider.
func NewAPIProvider(auth *Authenticator) *APIProvider {
p := &APIProvider{auth: auth}
return p
}
// GetLocks returns all available locks.
func (p *APIProvider) GetLocks() ([]*Lock, error) {
resp, err := doAPICall(p.auth, http.MethodGet, "/users/locks/mine", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
locksResp := make(map[string]*Lock)
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&locksResp)
if err != nil {
return nil, &ErrorGeneric{Message: err.Error()}
}
res := make([]*Lock, 0)
for k, v := range locksResp {
v.ID = k
res = append(res, v)
}
return res, nil
}
// GetLockDetails returns detailed information about the lock.
func (p *APIProvider) GetLockDetails(lockID string) (*LockDetails, error) {
resp, err := doAPICall(p.auth, http.MethodGet, "/locks/"+lockID, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
det := &LockDetails{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(det)
if err != nil {
return nil, &ErrorGeneric{Message: err.Error()}
}
return det, nil
}
// GetLockStatus returns current lock's status.
func (p *APIProvider) GetLockStatus(lockID string) (*LockStatus, error) {
resp, err := doAPICall(p.auth, http.MethodGet, "/locks/"+lockID+"/status", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
st := &LockStatus{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(st)
if err != nil {
return nil, &ErrorGeneric{Message: err.Error()}
}
return st, nil
}
type opTimeoutRequest struct {
TimeoutSeconds int `json:"timeout"`
}
// Lock/unlock the lock.
func (p *APIProvider) lockOperation(lockID string, op string) error {
req := &opTimeoutRequest{TimeoutSeconds: 30}
b, err := json.Marshal(req)
if err != nil {
return &ErrorGeneric{Message: err.Error()}
}
_, err = doAPICall(p.auth, http.MethodPut, "/remoteoperate/"+lockID+"/"+op, bytes.NewReader(b))
return err
}
// OpenLock unlocks the lock.
func (p *APIProvider) OpenLock(lockID string) error {
return p.lockOperation(lockID, "unlock")
}
// CloseLock locks the lock.
func (p *APIProvider) CloseLock(lockID string) error {
return p.lockOperation(lockID, "lock")
}