[golang] refactored the api client to reduce the amount of db pointer shuffling around
This commit is contained in:
parent
312ef2eb57
commit
40c4a8df15
9 changed files with 58 additions and 57 deletions
|
@ -6,10 +6,13 @@ import (
|
|||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"git.adyxax.org/adyxax/spacetraders/golang/pkg/database"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
baseURI *url.URL
|
||||
db *database.DB
|
||||
requestsChannel chan *Request
|
||||
ctx context.Context
|
||||
headers *http.Header
|
||||
|
@ -17,7 +20,7 @@ type Client struct {
|
|||
pq *PriorityQueue
|
||||
}
|
||||
|
||||
func NewClient(ctx context.Context) *Client {
|
||||
func NewClient(ctx context.Context, db *database.DB) *Client {
|
||||
baseURI, err := url.Parse("https://api.spacetraders.io/v2/")
|
||||
if err != nil {
|
||||
panic("baseURI failed to parse")
|
||||
|
@ -26,6 +29,7 @@ func NewClient(ctx context.Context) *Client {
|
|||
heap.Init(&pq)
|
||||
client := &Client{
|
||||
baseURI: baseURI,
|
||||
db: db,
|
||||
requestsChannel: make(chan *Request),
|
||||
ctx: ctx,
|
||||
headers: &http.Header{
|
||||
|
|
|
@ -5,11 +5,10 @@ import (
|
|||
"net/url"
|
||||
"path"
|
||||
|
||||
"git.adyxax.org/adyxax/spacetraders/golang/pkg/database"
|
||||
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
|
||||
)
|
||||
|
||||
func (c *Client) Accept(contract *model.Contract, db *database.DB) error {
|
||||
func (c *Client) Accept(contract *model.Contract) error {
|
||||
if contract.Accepted {
|
||||
return nil
|
||||
}
|
||||
|
@ -22,7 +21,7 @@ func (c *Client) Accept(contract *model.Contract, db *database.DB) error {
|
|||
if err := c.Send("POST", &uriRef, nil, &response); err != nil {
|
||||
return fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
if err := db.SaveAgent(response.Agent); err != nil {
|
||||
if err := c.db.SaveAgent(response.Agent); err != nil {
|
||||
return fmt.Errorf("failed to save agent: %w", err)
|
||||
}
|
||||
contract.Accepted = response.Contract.Accepted
|
||||
|
@ -30,7 +29,7 @@ func (c *Client) Accept(contract *model.Contract, db *database.DB) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Deliver(contract *model.Contract, ship *model.Ship, db *database.DB) error {
|
||||
func (c *Client) Deliver(contract *model.Contract, ship *model.Ship) error {
|
||||
deliver := contract.Terms.Deliver[0]
|
||||
var units int
|
||||
for _, cargoItem := range ship.Cargo.Inventory {
|
||||
|
@ -58,7 +57,7 @@ func (c *Client) Deliver(contract *model.Contract, ship *model.Ship, db *databas
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Fulfill(contract *model.Contract, db *database.DB) error {
|
||||
func (c *Client) Fulfill(contract *model.Contract) error {
|
||||
if contract.Fulfilled {
|
||||
return nil
|
||||
}
|
||||
|
@ -71,7 +70,7 @@ func (c *Client) Fulfill(contract *model.Contract, db *database.DB) error {
|
|||
if err := c.Send("POST", &uriRef, nil, &response); err != nil {
|
||||
return fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
if err := db.SaveAgent(response.Agent); err != nil {
|
||||
if err := c.db.SaveAgent(response.Agent); err != nil {
|
||||
return fmt.Errorf("failed to save agent: %w", err)
|
||||
}
|
||||
contract.Fulfilled = response.Contract.Fulfilled
|
||||
|
|
|
@ -6,7 +6,6 @@ import (
|
|||
"path"
|
||||
"time"
|
||||
|
||||
"git.adyxax.org/adyxax/spacetraders/golang/pkg/database"
|
||||
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
|
||||
)
|
||||
|
||||
|
@ -35,7 +34,7 @@ func (c *Client) MyShips() ([]model.Ship, error) {
|
|||
return ships, nil
|
||||
}
|
||||
|
||||
func (c *Client) Navigate(s *model.Ship, waypointSymbol string, db *database.DB) error {
|
||||
func (c *Client) Navigate(s *model.Ship, waypointSymbol string) error {
|
||||
if s.Nav.WaypointSymbol == waypointSymbol {
|
||||
return nil
|
||||
}
|
||||
|
@ -96,7 +95,7 @@ func (c *Client) orbit(s *model.Ship) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Purchase(s *model.Ship, cargoItem string, units int, db *database.DB) error {
|
||||
func (c *Client) Purchase(s *model.Ship, cargoItem string, units int) error {
|
||||
if err := c.dock(s); err != nil {
|
||||
return fmt.Errorf("failed to dock: %w", err)
|
||||
}
|
||||
|
@ -114,11 +113,11 @@ func (c *Client) Purchase(s *model.Ship, cargoItem string, units int, db *databa
|
|||
if err := c.Send("POST", &uriRef, purchaseRequest{cargoItem, units}, &response); err != nil {
|
||||
return fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
if err := db.SaveAgent(response.Agent); err != nil {
|
||||
if err := c.db.SaveAgent(response.Agent); err != nil {
|
||||
return fmt.Errorf("failed to save agent: %w", err)
|
||||
}
|
||||
s.Cargo = response.Cargo
|
||||
if err := db.AppendTransaction(response.Transaction); err != nil {
|
||||
if err := c.db.AppendTransaction(response.Transaction); err != nil {
|
||||
return fmt.Errorf("failed to append transaction: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
@ -141,17 +140,17 @@ func (c *Client) refuel(s *model.Ship, db *database.DB) error {
|
|||
if err := c.Send("POST", &uriRef, nil, &response); err != nil {
|
||||
return fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
if err := db.SaveAgent(response.Agent); err != nil {
|
||||
if err := c.db.SaveAgent(response.Agent); err != nil {
|
||||
return fmt.Errorf("failed to save agent: %w", err)
|
||||
}
|
||||
s.Fuel = response.Fuel
|
||||
if err := db.AppendTransaction(response.Transaction); err != nil {
|
||||
if err := c.db.AppendTransaction(response.Transaction); err != nil {
|
||||
return fmt.Errorf("failed to append transaction: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Sell(s *model.Ship, cargoItem string, units int, db *database.DB) error {
|
||||
func (c *Client) Sell(s *model.Ship, cargoItem string, units int) error {
|
||||
if err := c.dock(s); err != nil {
|
||||
return fmt.Errorf("failed to dock: %w", err)
|
||||
}
|
||||
|
@ -169,11 +168,11 @@ func (c *Client) Sell(s *model.Ship, cargoItem string, units int, db *database.D
|
|||
if err := c.Send("POST", &uriRef, sellRequest{cargoItem, units}, &response); err != nil {
|
||||
return fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
if err := db.SaveAgent(response.Agent); err != nil {
|
||||
if err := c.db.SaveAgent(response.Agent); err != nil {
|
||||
return fmt.Errorf("failed to save agent: %w", err)
|
||||
}
|
||||
s.Cargo = response.Cargo
|
||||
if err := db.AppendTransaction(response.Transaction); err != nil {
|
||||
if err := c.db.AppendTransaction(response.Transaction); err != nil {
|
||||
return fmt.Errorf("failed to append transaction: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -5,12 +5,11 @@ import (
|
|||
"net/url"
|
||||
"path"
|
||||
|
||||
"git.adyxax.org/adyxax/spacetraders/golang/pkg/database"
|
||||
"git.adyxax.org/adyxax/spacetraders/golang/pkg/model"
|
||||
)
|
||||
|
||||
func (c *Client) GetMarket(waypointSymbol string, db *database.DB) (*model.Market, error) {
|
||||
if market, err := db.LoadMarket(waypointSymbol); err == nil && market != nil {
|
||||
func (c *Client) GetMarket(waypointSymbol string) (*model.Market, error) {
|
||||
if market, err := c.db.LoadMarket(waypointSymbol); err == nil && market != nil {
|
||||
// TODO check last updated time
|
||||
return market, nil
|
||||
}
|
||||
|
@ -20,14 +19,14 @@ func (c *Client) GetMarket(waypointSymbol string, db *database.DB) (*model.Marke
|
|||
if err := c.Send("GET", &uriRef, nil, &market); err != nil {
|
||||
return nil, fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
if err := db.SaveMarket(&market); err != nil {
|
||||
if err := c.db.SaveMarket(&market); err != nil {
|
||||
return nil, fmt.Errorf("failed to save market %s: %w", market.Symbol, err)
|
||||
}
|
||||
return &market, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetShipyard(waypointSymbol string, db *database.DB) (*model.Shipyard, error) {
|
||||
if shipyard, err := db.LoadShipyard(waypointSymbol); err == nil && shipyard != nil &&
|
||||
func (c *Client) GetShipyard(waypointSymbol string) (*model.Shipyard, error) {
|
||||
if shipyard, err := c.db.LoadShipyard(waypointSymbol); err == nil && shipyard != nil &&
|
||||
(shipyard.Ships != nil) { // TODO || !IsThereAShipAtWaypoint(waypoint)) {
|
||||
// TODO check last updated time
|
||||
return shipyard, nil
|
||||
|
@ -38,14 +37,14 @@ func (c *Client) GetShipyard(waypointSymbol string, db *database.DB) (*model.Shi
|
|||
if err := c.Send("GET", &uriRef, nil, &shipyard); err != nil {
|
||||
return nil, fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
if err := db.SaveShipyard(&shipyard); err != nil {
|
||||
if err := c.db.SaveShipyard(&shipyard); err != nil {
|
||||
return nil, fmt.Errorf("failed to save shipyard %s: %w", shipyard.Symbol, err)
|
||||
}
|
||||
return &shipyard, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetSystem(systemSymbol string, db *database.DB) (*model.System, error) {
|
||||
if system, err := db.LoadSystem(systemSymbol); err == nil && system != nil {
|
||||
func (c *Client) GetSystem(systemSymbol string) (*model.System, error) {
|
||||
if system, err := c.db.LoadSystem(systemSymbol); err == nil && system != nil {
|
||||
return system, nil
|
||||
}
|
||||
uriRef := url.URL{Path: path.Join("systems", systemSymbol)}
|
||||
|
@ -53,14 +52,14 @@ func (c *Client) GetSystem(systemSymbol string, db *database.DB) (*model.System,
|
|||
if err := c.Send("GET", &uriRef, nil, &system); err != nil {
|
||||
return nil, fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
if err := db.SaveSystem(&system); err != nil {
|
||||
if err := c.db.SaveSystem(&system); err != nil {
|
||||
return nil, fmt.Errorf("failed to save system %s: %w", system.Symbol, err)
|
||||
}
|
||||
return &system, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetWaypoint(waypointSymbol string, db *database.DB) (*model.Waypoint, error) {
|
||||
if waypoint, err := db.LoadWaypoint(waypointSymbol); err == nil && waypoint != nil {
|
||||
func (c *Client) GetWaypoint(waypointSymbol string) (*model.Waypoint, error) {
|
||||
if waypoint, err := c.db.LoadWaypoint(waypointSymbol); err == nil && waypoint != nil {
|
||||
// TODO check last updated time
|
||||
return waypoint, nil
|
||||
}
|
||||
|
@ -70,14 +69,14 @@ func (c *Client) GetWaypoint(waypointSymbol string, db *database.DB) (*model.Way
|
|||
if err := c.Send("GET", &uriRef, nil, &waypoint); err != nil {
|
||||
return nil, fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
if err := db.SaveWaypoint(&waypoint); err != nil {
|
||||
if err := c.db.SaveWaypoint(&waypoint); err != nil {
|
||||
return nil, fmt.Errorf("failed to save waypoint %s: %w", waypoint.Symbol, err)
|
||||
}
|
||||
return &waypoint, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListWaypointsInSystem(systemSymbol string, db *database.DB) ([]model.Waypoint, error) {
|
||||
if waypoints, err := db.LoadWaypointsInSystem(systemSymbol); err == nil && waypoints != nil {
|
||||
func (c *Client) ListWaypointsInSystem(systemSymbol string) ([]model.Waypoint, error) {
|
||||
if waypoints, err := c.db.LoadWaypointsInSystem(systemSymbol); err == nil && waypoints != nil {
|
||||
// TODO check last updated time
|
||||
return waypoints, nil
|
||||
}
|
||||
|
@ -87,7 +86,7 @@ func (c *Client) ListWaypointsInSystem(systemSymbol string, db *database.DB) ([]
|
|||
return nil, fmt.Errorf("failed API request: %w", err)
|
||||
}
|
||||
for _, waypoint := range waypoints {
|
||||
if err := db.SaveWaypoint(&waypoint); err != nil {
|
||||
if err := c.db.SaveWaypoint(&waypoint); err != nil {
|
||||
return nil, fmt.Errorf("failed to save waypoint %s: %w", waypoint.Symbol, err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue