aboutsummaryrefslogtreecommitdiff
path: root/bridge.go
diff options
context:
space:
mode:
authorCollin Guarino <collin.guarino@gmail.com>2016-02-02 23:47:24 -0500
committerCollin Guarino <collin.guarino@gmail.com>2016-02-02 23:47:24 -0500
commit61af6c5cfd773db569910f2473499db67a5a8810 (patch)
tree1fd4ec65572c53268a7e609b8e607fdfc8302c29 /bridge.go
parentb0415daf6bd722e1bfea080de82b8a7ee4e24097 (diff)
Added handleResponse and bridge.Error to manage http responses and bridge specific errors with rest calls.
Diffstat (limited to 'bridge.go')
-rw-r--r--bridge.go40
1 files changed, 29 insertions, 11 deletions
diff --git a/bridge.go b/bridge.go
index 942318a..54000e3 100644
--- a/bridge.go
+++ b/bridge.go
@@ -42,26 +42,44 @@ type Device struct {
func (self *Bridge) Get(path string) ([]byte, io.Reader, error) {
resp, err := http.Get("http://" + self.IPAddress + path)
- if err != nil {
- trace("", err)
- } else if resp.StatusCode != 200 {
- trace(fmt.Sprintf("Bridge status error: %d", resp.StatusCode), nil)
+ if self.Error(resp, err) {
+ return []byte{}, nil, err
}
- //defer resp.Body.Close()
+ return handleResponse(resp)
+}
+
+func (self *Bridge) Post(path string) ([]byte, io.Reader, error) {
+ // resp, err := http.Post("http://" + self.IpAddress + path)
+ // if err != nil {
+ // trace("", err)
+ // }
+ return []byte{}, nil, nil
+}
+
+// HandleResponse manages the http.Response from a bridge Get/Put/Post/Delete
+// by checking it for errors and invalid return types.
+func handleResponse(resp *http.Response) ([]byte, io.Reader, error) {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
trace("Error parsing bridge description xml.", nil)
+ return []byte{}, nil, err
}
reader := bytes.NewReader(body)
- // TODO: handle individual error codes
+
return body, reader, nil
}
-func (self *Bridge) Post(path string) ([]byte, io.Reader, error) {
- // resp, err := http.Post("http://" + self.IpAddress + path)
- // if err != nil {
- // trace("", err)
- // }
+// bridge.Error handles any bridge request or response errors
+func (self *Bridge) Error(resp *http.Response, err error) (bool) {
+ if err != nil {
+ trace("", err)
+ return true
+ } else if resp.StatusCode != 200 {
+ // TODO: handle other status codes
+ trace(fmt.Sprintf("Bridge status error: %d", resp.StatusCode), nil)
+ return true
+ }
+ return false
}
// Error Struct