forked from fasmide/deflux
/
pairing.go
87 lines (70 loc) · 1.97 KB
/
pairing.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
package deconz
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
// APIKey is our apikey type
type APIKey string
type pairRequest struct {
DeviceType string `json:"devicetype"`
}
type pairResponse []pair
type pair struct {
Success struct {
Username string
}
}
type pairFailureResponse []pairFailure
type pairFailure struct {
Error struct {
Description string
}
}
// Pair tries to pair with deconz and returns a pairing with an API key
func Pair(u url.URL) (APIKey, error) {
// to pair we must send a POST request to "/api" containing a pairRequest
u.Path = "/api"
pr := pairRequest{
DeviceType: "Deflux",
}
var buff bytes.Buffer
enc := json.NewEncoder(&buff)
err := enc.Encode(pr)
if err != nil {
return "", fmt.Errorf("unable to marshal pair request: %s", err)
}
// send POST request and read body
response, err := http.Post(u.String(), "application/json", &buff)
if err != nil {
return "", fmt.Errorf("unable to send post request: %s", err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", fmt.Errorf("unable to read body: %s", err)
}
// this error happens if the gateway is locked
if response.StatusCode == http.StatusForbidden {
var failure pairFailureResponse
err = json.Unmarshal(body, &failure)
if err != nil {
return "", fmt.Errorf("unable to parse failure message drom deconz: %s", err)
}
return "", fmt.Errorf("unable to pair with deconz: %s", failure[0].Error.Description)
}
// we dont know what to do with other then 200 statuses
if response.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected statuscode from deconz: %d\n%s", response.StatusCode, body)
}
// finally, if none of the above failed, extract APIKey from response
var pairResp pairResponse
err = json.Unmarshal(body, &pairResp)
if err != nil {
return "", fmt.Errorf("unable to parse json from pair response: %s", err)
}
return APIKey(pairResp[0].Success.Username), nil
}