[golang] simplified the api design some more
This commit is contained in:
parent
860984057d
commit
4069b524b2
7 changed files with 56 additions and 86 deletions
golang/pkg/api
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue