Skip to content

Commit

Permalink
refactor: improve REST API design
Browse files Browse the repository at this point in the history
  • Loading branch information
xjasonlyu committed Dec 6, 2023
1 parent 396a620 commit 3542913
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
34 changes: 22 additions & 12 deletions game-service/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,30 @@ import (
"game-service/game"
)

type userUri struct {
Username string `form:"username" binding:"required"`
}

type startQuery struct {
Username string `form:"username" binding:"required"`
Zone string `form:"zone"`
Location dto.PointDTO `form:"location"`
}

func handleGameStart(app *game.App) gin.HandlerFunc {
return func(c *gin.Context) {
uri := &userUri{}
if err := c.ShouldBindUri(uri); err != nil {
abortWithStatusMessage(c, http.StatusBadRequest, err)
return
}

query := &startQuery{}
if err := c.ShouldBindJSON(query); err != nil {
if err := c.ShouldBindQuery(query); err != nil {
abortWithStatusMessage(c, http.StatusBadRequest, err)
return
}

player, err := getPlayerDTO(c, query.Username)
player, err := getPlayerDTO(c, uri.Username)
if err != nil {
abortWithStatusMessage(c, http.StatusInternalServerError, err)
return
Expand All @@ -50,19 +59,15 @@ func handleGameStart(app *game.App) gin.HandlerFunc {
}
}

type stopQuery struct {
Username string `form:"username" binding:"required"`
}

func handleGameStop(app *game.App) gin.HandlerFunc {
return func(c *gin.Context) {
query := &stopQuery{}
if err := c.ShouldBindJSON(query); err != nil {
uri := &userUri{}
if err := c.ShouldBindUri(uri); err != nil {
abortWithStatusMessage(c, http.StatusBadRequest, err)
return
}

player, err := getPlayerDTO(c, query.Username)
player, err := getPlayerDTO(c, uri.Username)
if err != nil {
abortWithStatusMessage(c, http.StatusInternalServerError, err)
return
Expand All @@ -83,21 +88,26 @@ func handleGameStop(app *game.App) gin.HandlerFunc {
}

type actionQuery struct {
Username string `form:"username" binding:"required"`
Action string `form:"action"`
Type string `form:"type"`
Location dto.PointDTO `form:"location"`
}

func handleGameAction(app *game.App) gin.HandlerFunc {
return func(c *gin.Context) {
uri := &userUri{}
if err := c.ShouldBindUri(uri); err != nil {
abortWithStatusMessage(c, http.StatusBadRequest, err)
return
}

query := &actionQuery{}
if err := c.ShouldBindJSON(query); err != nil {
abortWithStatusMessage(c, http.StatusBadRequest, err)
return
}

player, err := getPlayerDTO(c, query.Username)
player, err := getPlayerDTO(c, uri.Username)
if err != nil {
abortWithStatusMessage(c, http.StatusInternalServerError, err)
return
Expand Down
6 changes: 3 additions & 3 deletions game-service/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func New(app *game.App) *gin.Engine {

gm := r.Group("/game")
{
gm.POST("/start", handleGameStart(app))
gm.POST("/stop", handleGameStop(app))
gm.POST("/action", handleGameAction(app))
gm.GET("/:username", handleGameStart(app))
gm.POST("/:username", handleGameStop(app))
gm.DELETE("/:username", handleGameAction(app))
}

return r
Expand Down
18 changes: 7 additions & 11 deletions simulator-script/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ def simulate(base: str):

x, y = (12, 13)

with session.post(url=urljoin(base, '/game/start'), json={
"username": "leon",
with session.get(url=urljoin(base, '/game/leon'), params={
"zone": "mac",
"location": {
"x": x,
"y": y,
}
# "location": {
# "x": x,
# "y": y,
# }
}) as r:
print(f'>>> Starting game in Zone: MAC ({x}, {y})')
pprint(data := r.json())
Expand All @@ -58,8 +57,7 @@ def simulate(base: str):
time.sleep(2)
x, y = x + random.randint(1, 10), y + random.randint(1, 10)
action = random.choice(('Escaping', 'Fighting', 'Sheltering'))
with session.post(url=urljoin(base, '/game/action'), json={
"username": "leon",
with session.post(url=urljoin(base, '/game/leon'), json={
"action": action,
"type": "Cardio",
"location": {
Expand All @@ -72,9 +70,7 @@ def simulate(base: str):
pprint(r.json())
time.sleep(1)

with session.post(url=urljoin(base, '/game/stop'), json={
"username": "leon"
}) as r:
with session.delete(url=urljoin(base, '/game/leon')) as r:
print('Stopping game...')
pprint(r.json())
time.sleep(1)
Expand Down

0 comments on commit 3542913

Please sign in to comment.