mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-22 06:57:49 +00:00
Change APIResponse to a struct with a Data Interface
This commit is contained in:
parent
e1957def25
commit
ea0b058721
@ -9,7 +9,6 @@ import (
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"resin-supervisor/gosuper/systemd"
|
||||
@ -19,9 +18,10 @@ 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
|
||||
Error string
|
||||
// API response sent from gosupervisor
|
||||
type APIResponse struct {
|
||||
Data interface{}
|
||||
Error string
|
||||
}
|
||||
|
||||
type PurgeBody struct {
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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 {
|
||||
|
@ -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 )
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user