From 9ae1f50f46f2483e8a9ac088cbc23309c9c752c5 Mon Sep 17 00:00:00 2001 From: Marc Di Luzio Date: Fri, 5 Jun 2020 22:29:51 +0100 Subject: [PATCH] Rename /commands to /command Make it a verb not a GET fetch --- cmd/rove/main.go | 8 ++++---- pkg/rove/api.go | 16 ++++++++-------- pkg/rove/integration_test.go | 6 +++--- pkg/server/routes.go | 12 ++++++------ pkg/server/routes_test.go | 12 ++++++------ 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/cmd/rove/main.go b/cmd/rove/main.go index eae9302..086a2c9 100644 --- a/cmd/rove/main.go +++ b/cmd/rove/main.go @@ -20,7 +20,7 @@ func Usage() { fmt.Println("\tstatus \tprints the server status") fmt.Println("\tregister\tregisters an account and stores it (use with -name)") fmt.Println("\tspawn \tspawns a rover for the current account") - fmt.Println("\tcommands\tissues commands to the rover") + fmt.Println("\tcommand \tissues commands to the rover") fmt.Println("\tradar \tgathers radar data for the current rover") fmt.Println("\trover \tgets data for current rover") fmt.Println("\nOptions:") @@ -136,13 +136,13 @@ func main() { fmt.Printf("Spawned at position %+v\n", response.Position) } - case "commands": + case "command": verifyId(config) - d := rove.CommandsData{Id: config.Account} + d := rove.CommandData{Id: config.Account} // TODO: Send real commands in - if response, err := server.Commands(d); err != nil { + if response, err := server.Command(d); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) diff --git a/pkg/rove/api.go b/pkg/rove/api.go index 8a75766..4d3512e 100644 --- a/pkg/rove/api.go +++ b/pkg/rove/api.go @@ -67,22 +67,22 @@ type SpawnResponse struct { } // ============================== -// API: /commands method: POST +// API: /command method: POST -// Commands issues a set of commands from the user -func (s Server) Commands(d CommandsData) (r CommandsResponse, err error) { - err = s.POST("commands", d, &r) +// Command issues a set of commands from the user +func (s Server) Command(d CommandData) (r CommandResponse, err error) { + err = s.POST("command", d, &r) return } -// CommandsData is a set of commands to execute in order -type CommandsData struct { +// CommandData is a set of commands to execute in order +type CommandData struct { Id string `json:"id"` Commands []Command `json:"commands"` } -// CommandsResponse is the response to be sent back -type CommandsResponse struct { +// CommandResponse is the response to be sent back +type CommandResponse struct { Success bool `json:"success"` Error string `json:"error,omitempty"` } diff --git a/pkg/rove/integration_test.go b/pkg/rove/integration_test.go index 06ed9ab..465f3f0 100644 --- a/pkg/rove/integration_test.go +++ b/pkg/rove/integration_test.go @@ -55,7 +55,7 @@ func TestServer_Spawn(t *testing.T) { assert.True(t, r2.Success) } -func TestServer_Commands(t *testing.T) { +func TestServer_Command(t *testing.T) { d1 := RegisterData{ Name: uuid.New().String(), } @@ -71,7 +71,7 @@ func TestServer_Commands(t *testing.T) { assert.NoError(t, err) assert.True(t, r2.Success) - c := CommandsData{ + c := CommandData{ Id: r1.Id, Commands: []Command{ { @@ -81,7 +81,7 @@ func TestServer_Commands(t *testing.T) { }, }, } - r3, err := server.Commands(c) + r3, err := server.Command(c) assert.NoError(t, err) assert.True(t, r3.Success) } diff --git a/pkg/server/routes.go b/pkg/server/routes.go index 8462857..41486d0 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -39,9 +39,9 @@ var Routes = []Route{ handler: HandleSpawn, }, { - path: "/commands", + path: "/command", method: http.MethodPost, - handler: HandleCommands, + handler: HandleCommand, }, { path: "/radar", @@ -122,13 +122,13 @@ func HandleSpawn(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) { } // HandleSpawn will spawn the player entity for the associated account -func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) { - var response = rove.CommandsResponse{ +func HandleCommand(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) { + var response = rove.CommandResponse{ Success: false, } // Decode the commands, verify them and the account, and execute the commands - var data rove.CommandsData + var data rove.CommandData if err := json.NewDecoder(b).Decode(&data); err != nil { fmt.Printf("Failed to decode json: %s\n", err) response.Error = err.Error() @@ -162,7 +162,7 @@ func HandleRadar(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) { } // Decode the radar message, verify it, and respond with the radar info - var data rove.CommandsData + var data rove.CommandData if err := json.NewDecoder(b).Decode(&data); err != nil { fmt.Printf("Failed to decode json: %s\n", err) response.Error = err.Error() diff --git a/pkg/server/routes_test.go b/pkg/server/routes_test.go index 828df1f..7b9a789 100644 --- a/pkg/server/routes_test.go +++ b/pkg/server/routes_test.go @@ -74,7 +74,7 @@ func TestHandleSpawn(t *testing.T) { } } -func TestHandleCommands(t *testing.T) { +func TestHandleCommand(t *testing.T) { s := NewServer() a, err := s.accountant.RegisterAccount("test") assert.NoError(t, err, "Error registering account") @@ -85,7 +85,7 @@ func TestHandleCommands(t *testing.T) { pos, err := s.world.RoverPosition(inst) assert.NoError(t, err, "Couldn't get rover position") - data := rove.CommandsData{ + data := rove.CommandData{ Id: a.Id.String(), Commands: []rove.Command{ { @@ -99,16 +99,16 @@ func TestHandleCommands(t *testing.T) { b, err := json.Marshal(data) assert.NoError(t, err, "Error marshalling data") - request, _ := http.NewRequest(http.MethodPost, "/commands", bytes.NewReader(b)) + request, _ := http.NewRequest(http.MethodPost, "/command", bytes.NewReader(b)) response := httptest.NewRecorder() - s.wrapHandler(http.MethodPost, HandleCommands)(response, request) + s.wrapHandler(http.MethodPost, HandleCommand)(response, request) - var status rove.CommandsResponse + var status rove.CommandResponse json.NewDecoder(response.Body).Decode(&status) if status.Success != true { - t.Errorf("got false for /commands") + t.Errorf("got false for /command") } attrib, err := s.world.RoverAttributes(inst)