Rename /commands to /command

Make it a verb not a GET fetch
This commit is contained in:
Marc Di Luzio 2020-06-05 22:29:51 +01:00
parent 9d57f48f98
commit 9ae1f50f46
5 changed files with 27 additions and 27 deletions

View file

@ -20,7 +20,7 @@ func Usage() {
fmt.Println("\tstatus \tprints the server status") fmt.Println("\tstatus \tprints the server status")
fmt.Println("\tregister\tregisters an account and stores it (use with -name)") fmt.Println("\tregister\tregisters an account and stores it (use with -name)")
fmt.Println("\tspawn \tspawns a rover for the current account") 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("\tradar \tgathers radar data for the current rover")
fmt.Println("\trover \tgets data for current rover") fmt.Println("\trover \tgets data for current rover")
fmt.Println("\nOptions:") fmt.Println("\nOptions:")
@ -136,13 +136,13 @@ func main() {
fmt.Printf("Spawned at position %+v\n", response.Position) fmt.Printf("Spawned at position %+v\n", response.Position)
} }
case "commands": case "command":
verifyId(config) verifyId(config)
d := rove.CommandsData{Id: config.Account} d := rove.CommandData{Id: config.Account}
// TODO: Send real commands in // 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) fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)

View file

@ -67,22 +67,22 @@ type SpawnResponse struct {
} }
// ============================== // ==============================
// API: /commands method: POST // API: /command method: POST
// Commands issues a set of commands from the user // Command issues a set of commands from the user
func (s Server) Commands(d CommandsData) (r CommandsResponse, err error) { func (s Server) Command(d CommandData) (r CommandResponse, err error) {
err = s.POST("commands", d, &r) err = s.POST("command", d, &r)
return return
} }
// CommandsData is a set of commands to execute in order // CommandData is a set of commands to execute in order
type CommandsData struct { type CommandData struct {
Id string `json:"id"` Id string `json:"id"`
Commands []Command `json:"commands"` Commands []Command `json:"commands"`
} }
// CommandsResponse is the response to be sent back // CommandResponse is the response to be sent back
type CommandsResponse struct { type CommandResponse struct {
Success bool `json:"success"` Success bool `json:"success"`
Error string `json:"error,omitempty"` Error string `json:"error,omitempty"`
} }

View file

@ -55,7 +55,7 @@ func TestServer_Spawn(t *testing.T) {
assert.True(t, r2.Success) assert.True(t, r2.Success)
} }
func TestServer_Commands(t *testing.T) { func TestServer_Command(t *testing.T) {
d1 := RegisterData{ d1 := RegisterData{
Name: uuid.New().String(), Name: uuid.New().String(),
} }
@ -71,7 +71,7 @@ func TestServer_Commands(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, r2.Success) assert.True(t, r2.Success)
c := CommandsData{ c := CommandData{
Id: r1.Id, Id: r1.Id,
Commands: []Command{ 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.NoError(t, err)
assert.True(t, r3.Success) assert.True(t, r3.Success)
} }

View file

@ -39,9 +39,9 @@ var Routes = []Route{
handler: HandleSpawn, handler: HandleSpawn,
}, },
{ {
path: "/commands", path: "/command",
method: http.MethodPost, method: http.MethodPost,
handler: HandleCommands, handler: HandleCommand,
}, },
{ {
path: "/radar", 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 // HandleSpawn will spawn the player entity for the associated account
func HandleCommands(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) { func HandleCommand(s *Server, b io.ReadCloser, w io.Writer) (interface{}, error) {
var response = rove.CommandsResponse{ var response = rove.CommandResponse{
Success: false, Success: false,
} }
// Decode the commands, verify them and the account, and execute the commands // 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 { if err := json.NewDecoder(b).Decode(&data); err != nil {
fmt.Printf("Failed to decode json: %s\n", err) fmt.Printf("Failed to decode json: %s\n", err)
response.Error = err.Error() 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 // 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 { if err := json.NewDecoder(b).Decode(&data); err != nil {
fmt.Printf("Failed to decode json: %s\n", err) fmt.Printf("Failed to decode json: %s\n", err)
response.Error = err.Error() response.Error = err.Error()

View file

@ -74,7 +74,7 @@ func TestHandleSpawn(t *testing.T) {
} }
} }
func TestHandleCommands(t *testing.T) { func TestHandleCommand(t *testing.T) {
s := NewServer() s := NewServer()
a, err := s.accountant.RegisterAccount("test") a, err := s.accountant.RegisterAccount("test")
assert.NoError(t, err, "Error registering account") assert.NoError(t, err, "Error registering account")
@ -85,7 +85,7 @@ func TestHandleCommands(t *testing.T) {
pos, err := s.world.RoverPosition(inst) pos, err := s.world.RoverPosition(inst)
assert.NoError(t, err, "Couldn't get rover position") assert.NoError(t, err, "Couldn't get rover position")
data := rove.CommandsData{ data := rove.CommandData{
Id: a.Id.String(), Id: a.Id.String(),
Commands: []rove.Command{ Commands: []rove.Command{
{ {
@ -99,16 +99,16 @@ func TestHandleCommands(t *testing.T) {
b, err := json.Marshal(data) b, err := json.Marshal(data)
assert.NoError(t, err, "Error marshalling 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() 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) json.NewDecoder(response.Body).Decode(&status)
if status.Success != true { if status.Success != true {
t.Errorf("got false for /commands") t.Errorf("got false for /command")
} }
attrib, err := s.world.RoverAttributes(inst) attrib, err := s.world.RoverAttributes(inst)