-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgovee.go
106 lines (91 loc) · 2.47 KB
/
govee.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
package govee
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
)
const Version = "0.0.1"
func New(apiKey string) *Client {
return &Client{
APIKey: apiKey,
}
}
type Client struct {
APIKey string
}
func (c *Client) Run(request GoveeRequest) (GoveeResponse, error) {
client := &http.Client{}
var req *http.Request
switch request.GetMethod() {
case "GET":
req = c.getRequest(request)
case "PUT":
req = c.putRequest(request)
}
req.Header.Set("Govee-API-Key", c.APIKey)
req.Header.Set("User-Agent", fmt.Sprintf("go-vee/%s", Version))
resp, err := client.Do(req)
if err != nil {
return GoveeResponse{}, fmt.Errorf("govee request error: %w", err)
}
defer resp.Body.Close()
var response GoveeResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return GoveeResponse{}, fmt.Errorf("cannot parse govee response: %w", err)
}
if resp.StatusCode != 200 {
return GoveeResponse{}, fmt.Errorf("govee request error: %s", response.Message)
}
return response, nil
}
func (c *Client) getRequest(request GoveeRequest) *http.Request {
params := request.GetParams()
url := "https://developer-api.govee.com" + request.GetEndpoint()
if len(params) > 0 {
url += "?"
for key, value := range params {
url += key + "=" + value + "&"
}
url = strings.TrimSuffix(url, "&")
}
req, _ := http.NewRequest(request.GetMethod(), url, nil)
return req
}
func (c *Client) putRequest(request GoveeRequest) *http.Request {
jsonBody, _ := json.Marshal(request.GetBody())
req, _ := http.NewRequest(request.GetMethod(), "https://developer-api.govee.com"+request.GetEndpoint(), bytes.NewBuffer(jsonBody))
req.Header.Set("Content-Type", "application/json")
return req
}
type GoveeRequest interface {
GetEndpoint() string
GetMethod() string
GetBody() interface{}
GetParams() map[string]string
}
type GoveeResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data ResponseData `json:"data"`
}
func (g GoveeResponse) Devices() Devices {
return g.Data.Devices
}
func (g GoveeResponse) Device() string {
return g.Data.Device
}
func (g GoveeResponse) Model() string {
return g.Data.Model
}
func (g GoveeResponse) Properties() []map[string]interface{} {
return g.Data.Properties
}
type ResponseData struct {
Device string `json:"device"`
Model string `json:"model"`
Properties []map[string]interface{} `json:"properties"`
Devices []Device `json:"devices"`
}