Simplify the APIs to return http status codes

This commit is contained in:
Marc Di Luzio 2020-06-12 18:58:38 +01:00
parent 663cd77c94
commit 6cfc9444f3
9 changed files with 128 additions and 156 deletions

View file

@ -40,8 +40,7 @@ type RegisterData struct {
// RegisterResponse describes the response to a register request
type RegisterResponse struct {
Success bool `json:"success"`
Error string `json:"error,omitempty"`
// Placeholder for future information
}
// ==============================
@ -60,8 +59,7 @@ type CommandData struct {
// CommandResponse is the response to be sent back
type CommandResponse struct {
Success bool `json:"success"`
Error string `json:"error,omitempty"`
// Placeholder for future information
}
// ================
@ -75,9 +73,6 @@ func (s Server) Radar(account string) (r RadarResponse, err error) {
// RadarResponse describes the response to a /radar call
type RadarResponse struct {
Success bool `json:"success"`
Error string `json:"error,omitempty"`
// The set of positions for nearby non-empty tiles
Range int `json:"range"`
Tiles []atlas.Tile `json:"tiles"`
@ -94,9 +89,6 @@ func (s Server) Rover(account string) (r RoverResponse, err error) {
// RoverResponse includes information about the rover in question
type RoverResponse struct {
Success bool `json:"success"`
Error string `json:"error,omitempty"`
// The current position of this rover
Attributes game.RoverAttributes `json:"attributes"`
}

View file

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
@ -22,7 +23,11 @@ func (s Server) Get(path string, out interface{}) error {
return err
} else if resp.StatusCode != http.StatusOK {
return fmt.Errorf("http.Get returned status %d: %s", resp.StatusCode, resp.Status)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body to code %d", resp.StatusCode)
}
return fmt.Errorf("http returned status %d: %s", resp.StatusCode, string(body))
} else {
return json.NewDecoder(resp.Body).Decode(out)
@ -56,7 +61,11 @@ func (s Server) Post(path string, in, out interface{}) error {
return err
} else if resp.StatusCode != http.StatusOK {
return fmt.Errorf("http returned status %d", resp.StatusCode)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response body to code %d", resp.StatusCode)
}
return fmt.Errorf("http returned status %d: %s", resp.StatusCode, string(body))
} else {
return json.NewDecoder(resp.Body).Decode(out)