summaryrefslogtreecommitdiff
path: root/golang/pkg/api
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--golang/pkg/api/agents.go25
-rw-r--r--golang/pkg/api/api.go21
-rw-r--r--golang/pkg/api/register.go27
-rw-r--r--golang/pkg/api/ships.go46
4 files changed, 37 insertions, 82 deletions
diff --git a/golang/pkg/api/agents.go b/golang/pkg/api/agents.go
index db5b7db..a2d5b6a 100644
--- a/golang/pkg/api/agents.go
+++ b/golang/pkg/api/agents.go
@@ -1,32 +1,17 @@
package api
import (
- "encoding/json"
- "fmt"
"net/url"
-)
-type AgentMessage struct {
- AccountID string `json:"accountId"`
- Credits int `json:"credits"`
- Headquarters string `json:"headquarters"`
- ShipCount int `json:"shipCount"`
- StartingFaction string `json:"startingFaction"`
- Symbol string `json:"symbol"`
-}
+ "git.adyxax.org/adyxax/spacetraders/v2/pkg/model"
+)
-func (c *Client) MyAgent() (*AgentMessage, error) {
+func (c *Client) MyAgent() (*model.Agent, error) {
uriRef := url.URL{Path: "my/agent"}
- msg, err := c.Send("GET", &uriRef, nil)
+ var response model.Agent
+ err := c.Send("GET", &uriRef, nil, &response)
if err != nil {
return nil, err
}
- if msg.Error != nil {
- return nil, msg.Error
- }
- var response AgentMessage
- if err := json.Unmarshal(msg.Data, &response); err != nil {
- return nil, fmt.Errorf("failed to unmarshal agent data: %w", err)
- }
return &response, nil
}
diff --git a/golang/pkg/api/api.go b/golang/pkg/api/api.go
index 7aee2fa..472d9e5 100644
--- a/golang/pkg/api/api.go
+++ b/golang/pkg/api/api.go
@@ -43,7 +43,7 @@ type Response struct {
Err error
}
-func (c *Client) Send(method string, uriRef *url.URL, payload any) (*APIMessage, error) {
+func (c *Client) Send(method string, uriRef *url.URL, payload any, response any) error {
responseChannel := make(chan *Response)
c.requestsChannel <- &Request{
method: method,
@@ -53,7 +53,24 @@ func (c *Client) Send(method string, uriRef *url.URL, payload any) (*APIMessage,
uri: c.baseURI.ResolveReference(uriRef),
}
res := <-responseChannel
- return res.Message, res.Err
+ if res.Err != nil {
+ return res.Err
+ }
+ err := res.Message.Error
+ if err != nil {
+ switch err.Code {
+ case 4214:
+ e := decodeShipInTransitError(err.Data)
+ time.Sleep(e.SecondsToArrival.Duration() * time.Second)
+ return c.Send(method, uriRef, payload, response)
+ default:
+ return err
+ }
+ }
+ if err := json.Unmarshal(res.Message.Data, &response); err != nil {
+ return fmt.Errorf("failed to unmarshal message: %w", err)
+ }
+ return nil
}
func queueProcessor(client *Client) {
diff --git a/golang/pkg/api/register.go b/golang/pkg/api/register.go
index 4f45cd1..d6c185c 100644
--- a/golang/pkg/api/register.go
+++ b/golang/pkg/api/register.go
@@ -1,38 +1,25 @@
package api
import (
- "encoding/json"
- "fmt"
"net/url"
-)
-type RegisterMessage struct {
- //agent
- //contract
- //faction
- //ship
- Token string `json:"token"`
-}
+ "git.adyxax.org/adyxax/spacetraders/v2/pkg/model"
+)
-func (c *Client) Register(faction, symbol string) (*RegisterMessage, error) {
+func (c *Client) Register(faction, symbol string) (*model.Register, error) {
type RegisterRequest struct {
Faction string `json:"faction"`
Symbol string `json:"symbol"`
}
uriRef := url.URL{Path: "register"}
- msg, err := c.Send("POST", &uriRef, RegisterRequest{
+ payload := RegisterRequest{
Faction: faction,
Symbol: symbol,
- })
+ }
+ var response model.Register
+ err := c.Send("POST", &uriRef, payload, &response)
if err != nil {
return nil, err
}
- if msg.Error != nil {
- return nil, msg.Error
- }
- var response RegisterMessage
- if err := json.Unmarshal(msg.Data, &response); err != nil {
- return nil, fmt.Errorf("failed to unmarshal register data: %w", err)
- }
return &response, nil
}
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
}