-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
94 lines (86 loc) · 2.4 KB
/
http.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
package homeassistant
import (
"github.com/valyala/fasthttp"
"net"
"time"
"fmt"
"errors"
)
//HomeAssistant - main struct that the user will use
type HomeAssistant struct {
URL string
bearerToken string
httpClient *fasthttp.Client
}
//this http Client will communicate with Home Assistant in LAN, so https is not required
var urlTemplate = "http://%s:%d/api/"
var bearerTemplate = "Bearer %s"
//Creates Home Asisstnat object, initialize httpClient
func NewHomeAssistant(host string, port int, bearer string) (ha *HomeAssistant) {
fastClient := &fasthttp.Client{
Dial: func(addr string) (net.Conn, error) {
return fasthttp.DialTimeout(addr, time.Second*10)
},
}
url := fmt.Sprintf(urlTemplate, host, port)
ha = &HomeAssistant{
URL: url,
bearerToken: bearer,
httpClient: fastClient,
}
return
}
//Connect - Checks connection with HomeAssistant and its availability
func (ha *HomeAssistant) Connect() error {
//'/api/' - Returns a message if the HA API is up and running
req, resp := ha.createReqResp(ha.URL)
err := ha.httpClient.Do(req, resp)
if err != nil {
return err
}
if resp.StatusCode() != 200 {
//Unathorized
if resp.StatusCode() == 401 {
return errors.New("Bearer token is invalid")
} else {
return errors.New(string(resp.Body()))
}
}
return nil
}
func (ha *HomeAssistant) createReqResp(url string) (*fasthttp.Request, *fasthttp.Response) {
req := fasthttp.AcquireRequest()
//create request with header 'Authorization: Bearer ABCDEFG...'
req.SetRequestURI(url)
req.Header.Set("Authorization", fmt.Sprintf(bearerTemplate, ha.bearerToken))
req.Header.SetContentType("application/json")
//create response
resp := fasthttp.AcquireResponse()
return req, resp
}
//httpGet - send GET request
func (ha *HomeAssistant) httpGet(path string) ([]byte, error) {
url := ha.URL + path
req, resp := ha.createReqResp(url)
req.Header.SetMethod("GET")
err := ha.httpClient.Do(req, resp)
if err != nil {
return nil, err
}
return resp.Body(), nil
}
//httpPost - send POST request with json
func (ha *HomeAssistant) httpPost(path string, json []byte) ([]byte, error) {
url := ha.URL + path
req, resp := ha.createReqResp(url)
req.Header.SetMethod("POST")
req.SetBody(json)
err := ha.httpClient.Do(req, resp)
if err != nil {
return nil, err
}
if (resp.StatusCode() != 200) && (resp.StatusCode() != 201) {
return nil, errors.New(string(resp.Body()))
}
return resp.Body(), nil
}