-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
42 lines (34 loc) · 877 Bytes
/
user.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
package hue
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type User struct {
username string
// TODO:
//key string
}
// CreateNewUser will create a new user. This should be called only of there's none in the yaml config.
func CreateUser(addr string) (u *User, err error) {
return CreateUserExtended(addr, "go-hue-interface", "Philips hue")
}
// TODO: remove these comments
func CreateUserExtended(addr, application, deviceName string) (u *User, err error) {
uri := "http://" + addr + "/api"
var reqBody = []byte(fmt.Sprintf(`{"devicetype": "%s#%s"}`, application, deviceName))
req, err := http.NewRequest("POST", uri, bytes.NewBuffer(reqBody))
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return
}
defer res.Body.Close()
// Unmarshal data
err = json.NewDecoder(res.Body).Decode(u)
if err != nil {
return
}
return
}