mirror of
https://github.com/gosticks/go-hue-interface.git
synced 2025-10-16 11:45:35 +00:00
Merge branch 'master' of https://github.com/gosticks/go-hue-interface
This commit is contained in:
commit
d336ec74ac
36
bridge.go
Normal file
36
bridge.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Bridge is the hue bridge interface
|
||||||
|
type Bridge struct {
|
||||||
|
Config *Config
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bridge) postToBridge(endpoint string, payload interface{}) (interface{}, error) {
|
||||||
|
data, errMarhshal := json.Marshal(payload)
|
||||||
|
if errMarhshal != nil {
|
||||||
|
return nil, errMarhshal
|
||||||
|
}
|
||||||
|
uri := b.getBridgeAPIURI() + endpoint
|
||||||
|
req, err := http.NewRequest(http.MethodPost, uri, bytes.NewBuffer(data))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
client := &http.Client{}
|
||||||
|
res, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bridge) getBridgeAPIURI() string {
|
||||||
|
return b.Config.BridgeAddrScheme + "://" + b.Config.BridgeAddr + "/api/" + b.Config.Username
|
||||||
|
}
|
||||||
@ -9,8 +9,10 @@ import (
|
|||||||
|
|
||||||
// Config hue api config
|
// Config hue api config
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Username string `yaml:name`
|
Username string `yaml:name`
|
||||||
Password string `yaml:userpassword`
|
Password string `yaml:userpassword`
|
||||||
|
BridgeAddr string `yaml:bridgeAddress`
|
||||||
|
BridgeAddrScheme string `yaml:bridgeAddressScheme`
|
||||||
}
|
}
|
||||||
|
|
||||||
// createNewUser will create a new user. This should be called only of there's none in the yaml config.
|
// createNewUser will create a new user. This should be called only of there's none in the yaml config.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user