From 4069b524b2c607dcf8fc1e378ae86077f8a89234 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 30 May 2024 08:05:03 +0200 Subject: [golang] simplified the api design some more --- golang/pkg/api/ships.go | 46 ++++++---------------------------------------- 1 file changed, 6 insertions(+), 40 deletions(-) (limited to 'golang/pkg/api/ships.go') 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 } -- cgit v1.2.3