1
0
Fork 0

[golang] fixed golang api client design mistakes

This commit is contained in:
Julien Dessaux 2024-05-28 13:13:13 +02:00
parent d0f6c4343e
commit 0d00bf9fd2
Signed by: adyxax
GPG key ID: F92E51B86E07177E
9 changed files with 268 additions and 149 deletions

View file

@ -1,5 +1,11 @@
package api
import (
"encoding/json"
"fmt"
"net/url"
)
type RegisterMessage struct {
//agent
//contract
@ -8,13 +14,25 @@ type RegisterMessage struct {
Token string `json:"token"`
}
func (c *Client) Register(faction, symbol string) (APIMessage[RegisterMessage, any], error) {
func (c *Client) Register(faction, symbol string) (*RegisterMessage, error) {
type RegisterRequest struct {
Faction string `json:"faction"`
Symbol string `json:"symbol"`
}
return Send[RegisterMessage](c, "POST", "/register", RegisterRequest{
uriRef := url.URL{Path: "register"}
msg, err := c.Send("POST", &uriRef, RegisterRequest{
Faction: faction,
Symbol: symbol,
})
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
}