mirror of
https://github.com/gosticks/go-hue-interface.git
synced 2025-10-16 11:45:35 +00:00
added bridge to main
This commit is contained in:
parent
d336ec74ac
commit
41a3c0ba41
37
bridge.go
37
bridge.go
@ -3,6 +3,8 @@ package main
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@ -11,6 +13,13 @@ type Bridge struct {
|
||||
Config *Config
|
||||
}
|
||||
|
||||
// NewBridge creates a new bridge api instance
|
||||
func NewBridge(conf *Config) *Bridge {
|
||||
return &Bridge{
|
||||
Config: conf,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bridge) postToBridge(endpoint string, payload interface{}) (interface{}, error) {
|
||||
data, errMarhshal := json.Marshal(payload)
|
||||
if errMarhshal != nil {
|
||||
@ -31,6 +40,34 @@ func (b *Bridge) postToBridge(endpoint string, payload interface{}) (interface{}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (b *Bridge) getFromBridge(endpoint string, target interface{}) error {
|
||||
|
||||
uri := b.getBridgeAPIURI() + endpoint
|
||||
|
||||
req, err := http.NewRequest("GET", uri, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
return errors.New("Mite responded with error" + res.Status + fmt.Sprint(res.StatusCode))
|
||||
}
|
||||
|
||||
// Unmarshal data
|
||||
errDecode := json.NewDecoder(res.Body).Decode(target)
|
||||
if errDecode != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bridge) getBridgeAPIURI() string {
|
||||
return b.Config.BridgeAddrScheme + "://" + b.Config.BridgeAddr + "/api/" + b.Config.Username
|
||||
}
|
||||
|
||||
31
config.go
31
config.go
@ -2,7 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
@ -20,22 +20,21 @@ func (c *Config) createNewUser() {
|
||||
// TODO: read/create the url
|
||||
url := "http://192.168.178.46/api"
|
||||
|
||||
var reqBody = []byte(`{"devicetype": "go-hue-interface#Philips hue"}`)
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBody))
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var reqBody = []byte(`{"devicetype": "go-hue-interface#Philips hue"}`)
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBody))
|
||||
|
||||
fmt.Println("response Status:", resp.Status)
|
||||
fmt.Println("response Headers:", resp.Header)
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
fmt.Println("response Status:", resp.Status)
|
||||
fmt.Println("response Headers:", resp.Header)
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
fmt.Println("response Body:", string(body))
|
||||
|
||||
json.Unmarshal([]byte(str), &res)
|
||||
fmt.Println(res)
|
||||
fmt.Println(res.Fruits[0])
|
||||
// json.Unmarshal([]byte(str), &resp)
|
||||
fmt.Println(resp)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user