From 152c47f8a7eb85b8b728c738accfc15e308ba672 Mon Sep 17 00:00:00 2001 From: Collin Guarino Date: Mon, 25 Jan 2016 22:20:11 -0500 Subject: Working implementation for CreateUser with unit tests. --- bridge.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'bridge.go') diff --git a/bridge.go b/bridge.go index 739bf73..c2b23e3 100644 --- a/bridge.go +++ b/bridge.go @@ -5,10 +5,12 @@ import ( "log" "os" "encoding/xml" + "encoding/json" "net/http" "io/ioutil" "runtime" "fmt" + "bytes" ) type Bridge struct { @@ -75,14 +77,27 @@ func GetBridgeInfo(self *Bridge) { } func CreateUser(bridge *Bridge, deviceType string) (string, error) { + // Construct the http POST + params := map[string]string{"devicetype": deviceType} + request, err := json.Marshal(params) + if err != nil { + return "", err + } + + // Send the request to create the user and read the response uri := fmt.Sprintf("http://%s/api", bridge.IPAddress) - response, err := http.PostForm(uri, uurl.Values{"deviceType": deviceType}) + response, err := http.Post(uri, "text/json", bytes.NewReader(request)) if err != nil { - // TODO: handle error + return "", err } - defer response.Body.Close + defer response.Body.Close() body, err := ioutil.ReadAll(response.Body) fmt.Printf(string(body)) + + // TODO: handle description saying "link button not pressed" + // ^ handle "error":{"type":101} + + return string(body), err } // Log the date, time, file location, line number, and function. -- cgit v1.2.3