-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrequest.go
131 lines (102 loc) · 2.77 KB
/
request.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
package ewelink
import (
"net/http"
"net/url"
)
// HTTPRequest interface.
type HTTPRequest interface {
Method() string
URI() string
Query() *url.Values
Payload() payload
Headers() *http.Header
Response() Response
IsToBeSigned() bool
Session() *Session
}
type httpRequest struct {
method string
uri string
query *url.Values
payload payload
headers *http.Header
response Response
isSigned bool
session *Session
}
func (r httpRequest) Method() string {
return r.method
}
func (r httpRequest) URI() string {
return r.uri
}
func (r httpRequest) Query() *url.Values {
return r.query
}
func (r httpRequest) Payload() payload {
return r.payload
}
func (r httpRequest) Headers() *http.Header {
return r.headers
}
func (r httpRequest) Response() Response {
return r.response
}
func (r httpRequest) IsToBeSigned() bool {
return r.isSigned
}
func (r httpRequest) Session() *Session {
return r.session
}
// WebsocketRequest interface.
type WebsocketRequest interface {
Payload() payload
Response() Response
Session() *Session
}
type websocketRequest struct {
payload payload
response Response
session *Session
}
func (w websocketRequest) Response() Response {
return w.response
}
func (w websocketRequest) Payload() payload {
return w.payload
}
func (w websocketRequest) Session() *Session {
return w.session
}
func newAuthenticationRequest(payload *emailAuthenticationPayload, session *Session) HTTPRequest {
return &httpRequest{method: "POST", uri: "user/login", payload: payload, response: &AuthenticationResponse{}, isSigned: true, session: session}
}
func newGetDevicesRequest(query *url.Values, session *Session) HTTPRequest {
headers := &http.Header{}
headers.Add("Authorization", "Bearer "+session.AuthenticationToken)
return &httpRequest{method: "GET", uri: "user/device", headers: headers, query: query, response: &DevicesResponse{}, session: session}
}
func newWebsocketRequest(payload payload, response Response) WebsocketRequest {
return &websocketRequest{payload: payload, response: response}
}
func createUpdatePowerStateOfDeviceParameters(numberOfOutlets int, stateOn bool) parameters {
if numberOfOutlets == 1 {
return &DevicePowerStateParameters{PowerOn: stateOn}
}
statePerOutlet := make([]bool, numberOfOutlets)
for i := range statePerOutlet {
statePerOutlet[i] = stateOn
}
return &DeviceOutletPowerStateAction{PowerOn: statePerOutlet}
}
func createUpdatePowerStateOfOutletParameters(device *Device, outletIndex int, numberOfOutlets int, stateOn bool) parameters {
statePerOutlet := make([]bool, numberOfOutlets)
for i := range statePerOutlet {
if i == outletIndex {
statePerOutlet[i] = stateOn
continue
}
statePerOutlet[i] = device.Params.Switches[i] == on
}
return &DeviceOutletPowerStateAction{PowerOn: statePerOutlet}
}