diff options
author | Julien Dessaux | 2024-05-30 08:05:03 +0200 |
---|---|---|
committer | Julien Dessaux | 2024-05-30 08:05:03 +0200 |
commit | 4069b524b2c607dcf8fc1e378ae86077f8a89234 (patch) | |
tree | 3e42383cafb4a7f24252d4bc55b2481c3f9b014d /golang/pkg/api/ships.go | |
parent | [golang] added ships first api calls (diff) | |
download | spacetraders-4069b524b2c607dcf8fc1e378ae86077f8a89234.tar.gz spacetraders-4069b524b2c607dcf8fc1e378ae86077f8a89234.tar.bz2 spacetraders-4069b524b2c607dcf8fc1e378ae86077f8a89234.zip |
[golang] simplified the api design some more
Diffstat (limited to '')
-rw-r--r-- | golang/pkg/api/ships.go | 46 |
1 files changed, 6 insertions, 40 deletions
diff --git a/golang/pkg/api/ships.go b/golang/pkg/api/ships.go index afcf530..dc6a27c 100644 --- a/golang/pkg/api/ships.go +++ b/golang/pkg/api/ships.go @@ -1,11 +1,9 @@ package api import ( - "encoding/json" "fmt" "net/url" "path" - "time" "git.adyxax.org/adyxax/spacetraders/v2/pkg/model" ) @@ -18,41 +16,22 @@ func (c *Client) Dock(s *model.Ship) error { Nav model.Nav `json:"nav"` } uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "dock")} - msg, err := c.Send("POST", &uriRef, nil) + var response DockResponse + err := c.Send("POST", &uriRef, nil, &response) if err != nil { return fmt.Errorf("failed to dock ship %s: %w", s.Symbol, err) } - if msg.Error != nil { - switch msg.Error.Code { - case 4214: - e := decodeShipInTransitError(msg.Error.Data) - time.Sleep(e.SecondsToArrival.Duration() * time.Second) - return c.Dock(s) - default: - return msg.Error - } - } - var response DockResponse - if err := json.Unmarshal(msg.Data, &response); err != nil { - return fmt.Errorf("failed to unmarshal dock data: %w", err) - } s.Nav = response.Nav return nil } func (c *Client) MyShips() ([]model.Ship, error) { uriRef := url.URL{Path: "my/ships"} - msg, err := c.Send("GET", &uriRef, nil) + var response []model.Ship + err := c.Send("GET", &uriRef, nil, &response) if err != nil { return nil, fmt.Errorf("failed to get ships: %w", err) } - if msg.Error != nil { - return nil, fmt.Errorf("failed to get ships: %w", msg.Error) - } - var response []model.Ship - if err := json.Unmarshal(msg.Data, &response); err != nil { - return nil, fmt.Errorf("failed to unmarshal ships data: %w", err) - } return response, nil } @@ -64,24 +43,11 @@ func (c *Client) Orbit(s *model.Ship) error { Nav model.Nav `json:"nav"` } uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "orbit")} - msg, err := c.Send("POST", &uriRef, nil) + var response OrbitResponse + err := c.Send("POST", &uriRef, nil, &response) if err != nil { return fmt.Errorf("failed to orbit ship %s: %w", s.Symbol, err) } - if msg.Error != nil { - switch msg.Error.Code { - case 4214: - e := decodeShipInTransitError(msg.Error.Data) - time.Sleep(e.SecondsToArrival.Duration() * time.Second) - return c.Orbit(s) - default: - return msg.Error - } - } - var response OrbitResponse - if err := json.Unmarshal(msg.Data, &response); err != nil { - return fmt.Errorf("failed to unmarshal orbit data: %w", err) - } s.Nav = response.Nav return nil } |