diff options
author | Collin Guarino <collin.guarino@gmail.com> | 2016-02-01 23:07:56 -0500 |
---|---|---|
committer | Collin Guarino <collin.guarino@gmail.com> | 2016-02-01 23:07:56 -0500 |
commit | 508673c66ad19a3352bea1faf497d3a4741894c0 (patch) | |
tree | 161876e003ee24d1cde4594fa155aa4fc69016cb | |
parent | d6a93a3bd5b0d7603847eb8029526343312e6945 (diff) |
Implementation for bridge.Get and made GetBridgeInfo use the new function.
-rw-r--r-- | bridge.go | 94 |
1 files changed, 59 insertions, 35 deletions
@@ -12,8 +12,32 @@ import ( "fmt" "strings" "bytes" + "io" ) +// type Bridge struct { +// IPAddress string +// Username string +// Info BridgeInfo +// } +// +// type BridgeInfo struct { +// Root struct { +// Device struct { +// DeviceType string `xml:"deviceType"` +// FriendlyName string `xml:"friendlyName"` +// Manufacturer string `xml:"manufacturer"` +// ManufacturerURL string `xml:"manufacturerURL"` +// ModelDescription string `xml:"modelDescription"` +// ModelName string `xml:"modelName"` +// ModelNumber string `xml:"modelNumber"` +// ModelURL string `xml:"modelURL"` +// SerialNumber string `xml:"serialNumber"` +// UDN string `xml:"UDN"` +// } `xml:"device"` +// } `xml:"root"` +// } + type Bridge struct { IPAddress string Username string @@ -21,31 +45,39 @@ type Bridge struct { } type BridgeInfo struct { - Info struct { - Info struct { - DeviceType string `xml:"deviceType"` - FriendlyName string `xml:"friendlyName"` - Manufacturer string `xml:"manufacturer"` - ManufacturerURL string `xml:"manufacturerURL"` - ModelDescription string `xml:"modelDescription"` - ModelName string `xml:"modelName"` - ModelNumber string `xml:"modelNumber"` - ModelURL string `xml:"modelURL"` - SerialNumber string `xml:"serialNumber"` - UDN string `xml:"UDN"` - } `xml:"device"` - } `xml:"root"` + XMLName xml.Name `xml:"root"` + Device Device `xml:"device"` +} + +type Device struct { + XMLName xml.Name `xml:"device"` + DeviceType string `xml:"deviceType"` + FriendlyName string `xml:"friendlyName"` + Manufacturer string `xml:"manufacturer"` + ManufacturerURL string `xml:"manufacturerURL"` + ModelDescription string `xml:"modelDescription"` + ModelName string `xml:"modelName"` + ModelNumber string `xml:"modelNumber"` + ModelURL string `xml:"modelURL"` + SerialNumber string `xml:"serialNumber"` + UDN string `xml:"UDN"` } -func (self *Bridge) get(path string) (*http.Response, error) { +func (self *Bridge) Get(path string) (io.Reader, error) { resp, err := http.Get("http://" + self.IPAddress + path) if err != nil { trace("", err) - return nil, err } else if resp.StatusCode != 200 { - trace("Invalid bridge status.", nil) + trace(fmt.Sprintf("Bridge status error: %d", resp.StatusCode), nil) } - return resp, nil + //defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + trace("Error parsing bridge description xml.", nil) + } + reader := bytes.NewReader(body) + // TODO: handle individual error codes + return reader, nil } // Error Struct @@ -125,29 +157,21 @@ func NewBridge(ip string, username string) *Bridge { } // GetBridgeInfo retreives the description.xml file from the bridge. -func GetBridgeInfo(self *Bridge) { - response, err := http.Get("http://" + self.IPAddress + "/description.xml") +func GetBridgeInfo(self *Bridge) Error { + reader, err := self.Get("/description.xml") if err != nil { - trace("", err) - } else if response.StatusCode != 200 { - trace(fmt.Sprintf("Bridge status error: %d", response.StatusCode), nil) - os.Exit(1) + return ErrResponse } - defer response.Body.Close() - - body, err := ioutil.ReadAll(response.Body) - if err != nil { - trace("Error parsing bridge description xml.", nil) - os.Exit(1) - } - - data := new(BridgeInfo) - err = xml.Unmarshal(body, &data) + data := BridgeInfo{} + err = xml.NewDecoder(reader).Decode(&data) if err != nil { trace("Error using unmarshal to split xml.", nil) os.Exit(1) } - self.Info = *data + self.Info = data + fmt.Println("Bridge Info:\n", self.Info) + + return NoErr } // CreateUser posts to ./api on the bridge to create a new whitelisted user. |