Refactor APIs to take an /{accountid}/ prefix

This commit is contained in:
Marc Di Luzio 2020-06-05 23:08:59 +01:00
parent 9ae1f50f46
commit cade908ed2
6 changed files with 92 additions and 121 deletions

View file

@ -1,6 +1,8 @@
package rove
import (
"path"
"github.com/mdiluz/rove/pkg/game"
)
@ -43,18 +45,18 @@ type RegisterResponse struct {
}
// ==============================
// API: /spawn method: POST
// API: /{account}/spawn method: POST
// Spawn spawns the rover for an account
// Responds with the position of said rover
func (s Server) Spawn(d SpawnData) (r SpawnResponse, err error) {
err = s.POST("spawn", d, &r)
func (s Server) Spawn(account string, d SpawnData) (r SpawnResponse, err error) {
err = s.POST(path.Join(account, "spawn"), d, &r)
return
}
// SpawnData is the data to be sent for the spawn command
type SpawnData struct {
Id string `json:"id"`
// Empty for now, reserved for data
}
// SpawnResponse is the data to respond with on a spawn command
@ -67,17 +69,16 @@ type SpawnResponse struct {
}
// ==============================
// API: /command method: POST
// API: /{account}/command method: POST
// 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)
func (s Server) Command(account string, d CommandData) (r CommandResponse, err error) {
err = s.POST(path.Join(account, "command"), d, &r)
return
}
// CommandData is a set of commands to execute in order
type CommandData struct {
Id string `json:"id"`
Commands []Command `json:"commands"`
}
@ -104,19 +105,14 @@ type Command struct {
}
// ================
// API: /radar POST
// API: /{account}/radar method: GET
// Radar queries the current radar for the user
func (s Server) Radar(d RadarData) (r RadarResponse, err error) {
err = s.POST("radar", d, &r)
func (s Server) Radar(account string) (r RadarResponse, err error) {
err = s.GET(path.Join(account, "radar"), &r)
return
}
// RadarData describes the input data to request an accounts current radar
type RadarData struct {
Id string `json:"id"`
}
// RadarResponse describes the response to a /radar call
type RadarResponse struct {
Success bool `json:"success"`
@ -127,19 +123,14 @@ type RadarResponse struct {
}
// ================
// API: /rover POST
// API: /{account}/rover method: GET
// Rover queries the current state of the rover
func (s Server) Rover(d RoverData) (r RoverResponse, err error) {
err = s.POST("rover", d, &r)
func (s Server) Rover(account string) (r RoverResponse, err error) {
err = s.GET(path.Join(account, "rover"), &r)
return
}
// RoverData describes the input data to request rover status
type RoverData struct {
Id string `json:"id"`
}
// RoverResponse includes information about the rover in question
type RoverResponse struct {
Success bool `json:"success"`

View file

@ -47,10 +47,8 @@ func TestServer_Spawn(t *testing.T) {
assert.True(t, r1.Success)
assert.NotZero(t, len(r1.Id))
s := SpawnData{
Id: r1.Id,
}
r2, err := server.Spawn(s)
s := SpawnData{}
r2, err := server.Spawn(r1.Id, s)
assert.NoError(t, err)
assert.True(t, r2.Success)
}
@ -64,15 +62,12 @@ func TestServer_Command(t *testing.T) {
assert.True(t, r1.Success)
assert.NotZero(t, len(r1.Id))
s := SpawnData{
Id: r1.Id,
}
r2, err := server.Spawn(s)
s := SpawnData{}
r2, err := server.Spawn(r1.Id, s)
assert.NoError(t, err)
assert.True(t, r2.Success)
c := CommandData{
Id: r1.Id,
Commands: []Command{
{
Command: CommandMove,
@ -81,7 +76,7 @@ func TestServer_Command(t *testing.T) {
},
},
}
r3, err := server.Command(c)
r3, err := server.Command(r1.Id, c)
assert.NoError(t, err)
assert.True(t, r3.Success)
}
@ -95,17 +90,12 @@ func TestServer_Radar(t *testing.T) {
assert.True(t, r1.Success)
assert.NotZero(t, len(r1.Id))
s := SpawnData{
Id: r1.Id,
}
r2, err := server.Spawn(s)
s := SpawnData{}
r2, err := server.Spawn(r1.Id, s)
assert.NoError(t, err)
assert.True(t, r2.Success)
r := RadarData{
Id: r1.Id,
}
r3, err := server.Radar(r)
r3, err := server.Radar(r1.Id)
assert.NoError(t, err)
assert.True(t, r3.Success)
}
@ -119,17 +109,12 @@ func TestServer_Rover(t *testing.T) {
assert.True(t, r1.Success)
assert.NotZero(t, len(r1.Id))
s := SpawnData{
Id: r1.Id,
}
r2, err := server.Spawn(s)
s := SpawnData{}
r2, err := server.Spawn(r1.Id, s)
assert.NoError(t, err)
assert.True(t, r2.Success)
r := RoverData{
Id: r1.Id,
}
r3, err := server.Rover(r)
r3, err := server.Rover(r1.Id)
assert.NoError(t, err)
assert.True(t, r3.Success)
}