From 324bd287c76edb374bac36377aac294cacdfb259 Mon Sep 17 00:00:00 2001 From: Wlad Meixner Date: Sat, 9 Feb 2019 16:46:17 +0100 Subject: [PATCH 1/3] update config --- bridge.go | 30 ++++++++++++++++++++++++++++++ config.go | 5 +++-- 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 bridge.go diff --git a/bridge.go b/bridge.go new file mode 100644 index 0000000..612beff --- /dev/null +++ b/bridge.go @@ -0,0 +1,30 @@ +package main + +import ( + "bytes" + "encoding/json" + "net/http" +) + +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 + } + + req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data)) + if err != nil { + return nil, err + } + + req.Header.Set("Content-Type", "application/json") + res, err := client.Do(req) + if err != nil { + return nil, err + } + return res, nil +} diff --git a/config.go b/config.go index a26a051..e8f8b49 100644 --- a/config.go +++ b/config.go @@ -2,6 +2,7 @@ package main // Config hue api config type Config struct { - Username string `yaml:name` - Password string `yaml:userpassword` + Username string `yaml:name` + Password string `yaml:userpassword` + BridgeAddr string `yaml:bridgeAddress` } From 815895eca74182b5db1a60476a12fa4443692812 Mon Sep 17 00:00:00 2001 From: Wlad Meixner Date: Sat, 9 Feb 2019 16:49:01 +0100 Subject: [PATCH 2/3] extended config --- config.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index e8f8b49..e35f90c 100644 --- a/config.go +++ b/config.go @@ -2,7 +2,8 @@ package main // Config hue api config type Config struct { - Username string `yaml:name` - Password string `yaml:userpassword` - BridgeAddr string `yaml:bridgeAddress` + Username string `yaml:name` + Password string `yaml:userpassword` + BridgeAddr string `yaml:bridgeAddress` + BridgeAddrScheme string `yaml:bridgeAddressScheme` } From 6c95d8cee4731655e9ab54ac58e45838c0fb5a25 Mon Sep 17 00:00:00 2001 From: Wlad Meixner Date: Sat, 9 Feb 2019 16:53:38 +0100 Subject: [PATCH 3/3] add post to bridge method --- bridge.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/bridge.go b/bridge.go index 612beff..fb0ae92 100644 --- a/bridge.go +++ b/bridge.go @@ -6,6 +6,7 @@ import ( "net/http" ) +// Bridge is the hue bridge interface type Bridge struct { Config *Config } @@ -15,16 +16,21 @@ func (b *Bridge) postToBridge(endpoint string, payload interface{}) (interface{} if errMarhshal != nil { return nil, errMarhshal } - - req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data)) + 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 +}