Change APIResponse to a struct with a Data Interface

This commit is contained in:
Praneeth Bodduluri 2015-09-18 19:18:39 +05:30 committed by Pablo Carranza Vélez
parent e1957def25
commit ea0b058721
4 changed files with 17 additions and 15 deletions

View File

@ -9,7 +9,6 @@ import (
"os" "os"
"regexp" "regexp"
"strconv" "strconv"
"strings"
"time" "time"
"resin-supervisor/gosuper/systemd" "resin-supervisor/gosuper/systemd"
@ -19,9 +18,10 @@ import (
// Use raw strings to avoid having to quote the backslashes. // Use raw strings to avoid having to quote the backslashes.
var dockerMatch = regexp.MustCompile(`(docker[0-9])|(rce[0-9])`) var dockerMatch = regexp.MustCompile(`(docker[0-9])|(rce[0-9])`)
type ApiResponse struct { // API response sent from gosupervisor
Status string type APIResponse struct {
Error string Data interface{}
Error string
} }
type PurgeBody struct { type PurgeBody struct {
@ -61,9 +61,9 @@ func parsePurgeBody(request *http.Request) (appId string, err error) {
return return
} }
func responseSender(writer http.ResponseWriter) func(string, string, int) { func responseSender(writer http.ResponseWriter) func(interface{}, string, int) {
return func(statusMsg, errorMsg string, statusCode int) { return func(data interface{}, errorMsg string, statusCode int) {
jsonResponse(writer, ApiResponse{statusMsg, errorMsg}, statusCode) jsonResponse(writer, APIResponse{data, errorMsg}, statusCode)
} }
} }
@ -167,6 +167,8 @@ func IPAddressHandler(writer http.ResponseWriter, request *http.Request) {
if ipAddr, err := ipAddress(); err != nil { if ipAddr, err := ipAddress(); err != nil {
sendError("Invalid request") sendError("Invalid request")
} else { } else {
sendResponse(strings.Join(ipAddr, " "), "", http.StatusOK) payload := make(map[string][]string)
payload["IPAddress"] = ipAddr
sendResponse(payload, "", http.StatusOK)
} }
} }

View File

@ -16,11 +16,11 @@ var purgeTests = []struct {
IsSuccess bool IsSuccess bool
HttpStatus int HttpStatus int
}{ }{
{`{"applicationId": "1"}`, "1", `{"Status":"OK","Error":""}`, true, http.StatusOK}, {`{"applicationId": "1"}`, "1", `{"Data":"OK","Error":""}`, true, http.StatusOK},
{`{"applicationId": 1}`, "1", `{"Status":"OK","Error":""}`, true, http.StatusOK}, {`{"applicationId": 1}`, "1", `{"Data":"OK","Error":""}`, true, http.StatusOK},
{`{"applicationId": "hi"}`, "1", `{"Status":"Error","Error":"Invalid applicationId 'hi'"}`, false, http.StatusBadRequest}, {`{"applicationId": "hi"}`, "1", `{"Data":"Error","Error":"Invalid applicationId 'hi'"}`, false, http.StatusBadRequest},
{`{"applicationId": "2"}`, "1", `{"Status":"Error","Error":"Invalid applicationId '2': Directory does not exist"}`, false, http.StatusNotFound}, {`{"applicationId": "2"}`, "1", `{"Data":"Error","Error":"Invalid applicationId '2': Directory does not exist"}`, false, http.StatusNotFound},
{`{}`, "1", `{"Status":"Error","Error":"applicationId is required"}`, false, http.StatusBadRequest}, {`{}`, "1", `{"Data":"Error","Error":"applicationId is required"}`, false, http.StatusBadRequest},
} }
func TestPurge(t *testing.T) { func TestPurge(t *testing.T) {

View File

@ -62,7 +62,7 @@ func TestPurge(t *testing.T) {
} else { } else {
if contents, err := ioutil.ReadAll(response.Body); err != nil { if contents, err := ioutil.ReadAll(response.Body); err != nil {
t.Fatal(err) t.Fatal(err)
} else if !strings.EqualFold(string(contents), `{"Status":"OK","Error":""}`) { } else if !strings.EqualFold(string(contents), `{"Data":"OK","Error":""}`) {
t.Errorf("Purge response didn't match the expected JSON, got: %s", contents) t.Errorf("Purge response didn't match the expected JSON, got: %s", contents)
} }
if dirContents, err := ioutil.ReadDir(dataPath); err != nil { if dirContents, err := ioutil.ReadDir(dataPath); err != nil {

View File

@ -58,7 +58,7 @@ knex.init.then ->
callback = (error, response, body ) -> callback = (error, response, body ) ->
if !error && response.statusCode == 200 if !error && response.statusCode == 200
device.updateState( device.updateState(
ip_address: body.Status ip_address: body.Data.IPAddress.join(' ')
) )
request.get({ url: "#{config.gosuperAddress}/v1/ipaddr", json: true }, callback ) request.get({ url: "#{config.gosuperAddress}/v1/ipaddr", json: true }, callback )