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"
"regexp"
"strconv"
"strings"
"time"
"resin-supervisor/gosuper/systemd"
@ -19,8 +18,9 @@ import (
// Use raw strings to avoid having to quote the backslashes.
var dockerMatch = regexp.MustCompile(`(docker[0-9])|(rce[0-9])`)
type ApiResponse struct {
Status string
// API response sent from gosupervisor
type APIResponse struct {
Data interface{}
Error string
}
@ -61,9 +61,9 @@ func parsePurgeBody(request *http.Request) (appId string, err error) {
return
}
func responseSender(writer http.ResponseWriter) func(string, string, int) {
return func(statusMsg, errorMsg string, statusCode int) {
jsonResponse(writer, ApiResponse{statusMsg, errorMsg}, statusCode)
func responseSender(writer http.ResponseWriter) func(interface{}, string, int) {
return func(data interface{}, errorMsg string, statusCode int) {
jsonResponse(writer, APIResponse{data, errorMsg}, statusCode)
}
}
@ -167,6 +167,8 @@ func IPAddressHandler(writer http.ResponseWriter, request *http.Request) {
if ipAddr, err := ipAddress(); err != nil {
sendError("Invalid request")
} 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
HttpStatus int
}{
{`{"applicationId": "1"}`, "1", `{"Status":"OK","Error":""}`, true, http.StatusOK},
{`{"applicationId": 1}`, "1", `{"Status":"OK","Error":""}`, true, http.StatusOK},
{`{"applicationId": "hi"}`, "1", `{"Status":"Error","Error":"Invalid applicationId 'hi'"}`, false, http.StatusBadRequest},
{`{"applicationId": "2"}`, "1", `{"Status":"Error","Error":"Invalid applicationId '2': Directory does not exist"}`, false, http.StatusNotFound},
{`{}`, "1", `{"Status":"Error","Error":"applicationId is required"}`, false, http.StatusBadRequest},
{`{"applicationId": "1"}`, "1", `{"Data":"OK","Error":""}`, true, http.StatusOK},
{`{"applicationId": 1}`, "1", `{"Data":"OK","Error":""}`, true, http.StatusOK},
{`{"applicationId": "hi"}`, "1", `{"Data":"Error","Error":"Invalid applicationId 'hi'"}`, false, http.StatusBadRequest},
{`{"applicationId": "2"}`, "1", `{"Data":"Error","Error":"Invalid applicationId '2': Directory does not exist"}`, false, http.StatusNotFound},
{`{}`, "1", `{"Data":"Error","Error":"applicationId is required"}`, false, http.StatusBadRequest},
}
func TestPurge(t *testing.T) {

View File

@ -62,7 +62,7 @@ func TestPurge(t *testing.T) {
} else {
if contents, err := ioutil.ReadAll(response.Body); err != nil {
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)
}
if dirContents, err := ioutil.ReadDir(dataPath); err != nil {

View File

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