Skip to content

Commit

Permalink
Add an APIResponse struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbuell committed Oct 19, 2015
1 parent 943ad5d commit b4a90b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
10 changes: 10 additions & 0 deletions bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hue

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -60,8 +61,17 @@ func (bridge *Bridge) GetLight(id string) (*Light, error) {
return &light, nil
}

type APIResponse struct {
Result map[string]interface{} `json:"success,omitempty"`
Error *ErrorResponse `json:"error,omitempty"`
}

type ErrorResponse struct {
Address string `json:"address"`
Description string `json:"description"`
Type int16 `json:"type"`
}

func (err ErrorResponse) Error() string {
return fmt.Sprintf("API did not return success (%s): %s)", err.Address, err.Description)
}
30 changes: 14 additions & 16 deletions lights.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"path"
)
Expand Down Expand Up @@ -89,42 +88,42 @@ type SetState struct {
XYDelta []float64 `json:"xy_inc,omitempty"`
}

func (light *Light) SetName(name string) error {
func (light *Light) SetName(name string) (map[string]interface{}, error) {
lightsUrl := light.bridge.baseUrl
lightsUrl.Path = path.Join(lightsUrl.Path, "lights", light.index)
var putBody bytes.Buffer
err := json.NewEncoder(&putBody).Encode(map[string]string{"name": name})
if err != nil {
return err
return nil, err
}

setRequest, err := http.NewRequest("PUT", lightsUrl.String(), &putBody)
if err != nil {
return err
return nil, err
}

resp, err := light.bridge.client.Do(setRequest)
if err != nil {
return err
return nil, err
}
defer resp.Body.Close()

// Unmarshal
var response []map[string]map[string]string
var response []APIResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return err
return nil, err
}

if len(response) != 1 {
return errors.New("Expected one result in SetLightName response.")
return nil, errors.New("Expected one result in SetLightName response.")
}

if _, ok := response[0]["success"]; !ok {
return errors.New("API did not return success.")
if response[0].Error != nil {
return nil, response[0].Error
}

return nil
return response[0].Result, nil
}

func (light *Light) SetState(state SetState) (map[string]interface{}, error) {
Expand All @@ -148,7 +147,7 @@ func (light *Light) SetState(state SetState) (map[string]interface{}, error) {
defer resp.Body.Close()

// Unmarshal
var response []map[string]map[string]interface{}
var response []APIResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return nil, err
Expand All @@ -160,11 +159,10 @@ func (light *Light) SetState(state SetState) (map[string]interface{}, error) {

updateValues := make(map[string]interface{})
for _, responseItem := range response {
responseValueMap, isSuccess := responseItem["success"]
if !isSuccess {
return nil, errors.New(fmt.Sprintf("API did not return success: %+v", responseItem))
if responseItem.Error != nil {
return nil, responseItem.Error
}
for updatePath, updateValue := range responseValueMap {
for updatePath, updateValue := range responseItem.Result {
updateValues[updatePath] = updateValue
}
}
Expand Down

0 comments on commit b4a90b8

Please sign in to comment.