1
0
Fork 0

[golang] simplified the api design some more

This commit is contained in:
Julien Dessaux 2024-05-30 08:05:03 +02:00
parent 860984057d
commit 4069b524b2
Signed by: adyxax
GPG key ID: F92E51B86E07177E
7 changed files with 56 additions and 86 deletions

View file

@ -84,9 +84,5 @@ func run(
slog.Info("agent", "agent", agent, "err", err) slog.Info("agent", "agent", agent, "err", err)
// ----- Get ships --------------------------------------------------------- // ----- Get ships ---------------------------------------------------------
ships, err := client.MyShips() ships, err := client.MyShips()
err = client.Dock(&ships[0])
slog.Info("dock", "ship", ships[0], "err", err)
err = client.Orbit(&ships[0])
slog.Info("orbit", "ship", ships[0], "err", err)
return nil return nil
} }

View file

@ -1,32 +1,17 @@
package api package api
import ( import (
"encoding/json"
"fmt"
"net/url" "net/url"
"git.adyxax.org/adyxax/spacetraders/v2/pkg/model"
) )
type AgentMessage struct { func (c *Client) MyAgent() (*model.Agent, error) {
AccountID string `json:"accountId"`
Credits int `json:"credits"`
Headquarters string `json:"headquarters"`
ShipCount int `json:"shipCount"`
StartingFaction string `json:"startingFaction"`
Symbol string `json:"symbol"`
}
func (c *Client) MyAgent() (*AgentMessage, error) {
uriRef := url.URL{Path: "my/agent"} 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 { if err != nil {
return nil, err 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 return &response, nil
} }

View file

@ -43,7 +43,7 @@ type Response struct {
Err error 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) responseChannel := make(chan *Response)
c.requestsChannel <- &Request{ c.requestsChannel <- &Request{
method: method, method: method,
@ -53,7 +53,24 @@ func (c *Client) Send(method string, uriRef *url.URL, payload any) (*APIMessage,
uri: c.baseURI.ResolveReference(uriRef), uri: c.baseURI.ResolveReference(uriRef),
} }
res := <-responseChannel 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) { func queueProcessor(client *Client) {

View file

@ -1,38 +1,25 @@
package api package api
import ( import (
"encoding/json"
"fmt"
"net/url" "net/url"
"git.adyxax.org/adyxax/spacetraders/v2/pkg/model"
) )
type RegisterMessage struct { func (c *Client) Register(faction, symbol string) (*model.Register, error) {
//agent
//contract
//faction
//ship
Token string `json:"token"`
}
func (c *Client) Register(faction, symbol string) (*RegisterMessage, error) {
type RegisterRequest struct { type RegisterRequest struct {
Faction string `json:"faction"` Faction string `json:"faction"`
Symbol string `json:"symbol"` Symbol string `json:"symbol"`
} }
uriRef := url.URL{Path: "register"} uriRef := url.URL{Path: "register"}
msg, err := c.Send("POST", &uriRef, RegisterRequest{ payload := RegisterRequest{
Faction: faction, Faction: faction,
Symbol: symbol, Symbol: symbol,
}) }
var response model.Register
err := c.Send("POST", &uriRef, payload, &response)
if err != nil { if err != nil {
return nil, err 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 return &response, nil
} }

View file

@ -1,11 +1,9 @@
package api package api
import ( import (
"encoding/json"
"fmt" "fmt"
"net/url" "net/url"
"path" "path"
"time"
"git.adyxax.org/adyxax/spacetraders/v2/pkg/model" "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"` Nav model.Nav `json:"nav"`
} }
uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "dock")} 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 { if err != nil {
return fmt.Errorf("failed to dock ship %s: %w", s.Symbol, err) 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 s.Nav = response.Nav
return nil return nil
} }
func (c *Client) MyShips() ([]model.Ship, error) { func (c *Client) MyShips() ([]model.Ship, error) {
uriRef := url.URL{Path: "my/ships"} 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 { if err != nil {
return nil, fmt.Errorf("failed to get ships: %w", err) 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 return response, nil
} }
@ -64,24 +43,11 @@ func (c *Client) Orbit(s *model.Ship) error {
Nav model.Nav `json:"nav"` Nav model.Nav `json:"nav"`
} }
uriRef := url.URL{Path: path.Join("my/ships", s.Symbol, "orbit")} 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 { if err != nil {
return fmt.Errorf("failed to orbit ship %s: %w", s.Symbol, err) 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 s.Nav = response.Nav
return nil return nil
} }

10
golang/pkg/model/agent.go Normal file
View file

@ -0,0 +1,10 @@
package model
type Agent 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"`
}

View file

@ -0,0 +1,9 @@
package model
type Register struct {
Agent Agent `json:"agent"`
//contract
//faction
Ship Ship `json:"ship"`
Token string `json:"token"`
}