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
golang/pkg/api

View file

@ -1,32 +1,17 @@
package api
import (
"encoding/json"
"fmt"
"net/url"
"git.adyxax.org/adyxax/spacetraders/v2/pkg/model"
)
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"`
}
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
}