summaryrefslogtreecommitdiff
path: root/src/cardmarket
diff options
context:
space:
mode:
Diffstat (limited to 'src/cardmarket')
-rw-r--r--src/cardmarket/game.go24
-rw-r--r--src/cardmarket/handlers/account.go (renamed from src/cardmarket/account.go)12
-rw-r--r--src/cardmarket/handlers/game.go26
-rw-r--r--src/cardmarket/handlers/vacation.go47
-rw-r--r--src/cardmarket/http.go88
5 files changed, 166 insertions, 31 deletions
diff --git a/src/cardmarket/game.go b/src/cardmarket/game.go
deleted file mode 100644
index 995162d..0000000
--- a/src/cardmarket/game.go
+++ /dev/null
@@ -1,24 +0,0 @@
-package cardmarket
-
-import (
- "context"
-)
-
-type Game struct {
- GameID uint32 `json:"idGame"`
- Name string `json:"name"`
- Abbreviation string `json:"abbreviation"`
- Links Links `json:"links"`
-}
-
-func (c *ApiClient) GetGames(ctx context.Context) ([]Game, error) {
- type responseBody struct {
- Game []Game `json:"game"`
- Links Links `json:"links"`
- }
- respBody := &responseBody{}
- if err := c.get(ctx, "/games", respBody); err != nil {
- return nil, err
- }
- return respBody.Game, nil
-}
diff --git a/src/cardmarket/account.go b/src/cardmarket/handlers/account.go
index bdef2bd..213766d 100644
--- a/src/cardmarket/account.go
+++ b/src/cardmarket/handlers/account.go
@@ -1,7 +1,9 @@
-package cardmarket
+package handlers
import (
"context"
+
+ "codeberg.org/dijitol/trading_post/src/cardmarket"
)
type Account struct {
@@ -57,13 +59,13 @@ type Account struct {
LossPercentage string `json:"lossPercentage"`
}
-func (c *ApiClient) GetAccountInformation(ctx context.Context) (Account, error) {
+func GetAccountInformation(ctx context.Context, c *cardmarket.ApiClient) (Account, error) {
type responseBody struct {
- Account Account `json:"account"`
- Links Links `json:"links"`
+ Account Account `json:"account"`
+ Links cardmarket.Links `json:"links"`
}
respBody := &responseBody{}
- if err := c.get(ctx, "/account", respBody); err != nil {
+ if err := c.Get(ctx, "/account", nil, respBody); err != nil {
return Account{}, err
}
return respBody.Account, nil
diff --git a/src/cardmarket/handlers/game.go b/src/cardmarket/handlers/game.go
new file mode 100644
index 0000000..99b9046
--- /dev/null
+++ b/src/cardmarket/handlers/game.go
@@ -0,0 +1,26 @@
+package handlers
+
+import (
+ "context"
+
+ "codeberg.org/dijitol/trading_post/src/cardmarket"
+)
+
+type Game struct {
+ GameID uint32 `json:"idGame"`
+ Name string `json:"name"`
+ Abbreviation string `json:"abbreviation"`
+ Links cardmarket.Links `json:"links"`
+}
+
+func GetGames(ctx context.Context, c *cardmarket.ApiClient) ([]Game, error) {
+ type responseBody struct {
+ Game []Game `json:"game"`
+ Links cardmarket.Links `json:"links"`
+ }
+ respBody := &responseBody{}
+ if err := c.Get(ctx, "/games", nil, respBody); err != nil {
+ return nil, err
+ }
+ return respBody.Game, nil
+}
diff --git a/src/cardmarket/handlers/vacation.go b/src/cardmarket/handlers/vacation.go
new file mode 100644
index 0000000..59528ed
--- /dev/null
+++ b/src/cardmarket/handlers/vacation.go
@@ -0,0 +1,47 @@
+package handlers
+
+import (
+ "context"
+
+ "codeberg.org/dijitol/trading_post/src/cardmarket"
+)
+
+func SetVacationState(
+ ctx context.Context,
+ c *cardmarket.ApiClient,
+ onVacation bool,
+ cancelOrders, relistItems *bool,
+) error {
+ params := make(map[string]string, 3)
+
+ if onVacation {
+ params["onVacation"] = "true"
+ } else {
+ params["onVacation"] = "false"
+ }
+
+ if cancelOrders != nil {
+ if *cancelOrders {
+ params["cancelOrders"] = "true"
+ if relistItems != nil && *relistItems {
+ params["relistItems"] = "true"
+ }
+ } else {
+ params["cancelOrders"] = "false"
+ params["relistItems"] = "false"
+ }
+ }
+
+ if err := c.Put(
+ ctx,
+ "/account/vacation",
+ params,
+ "",
+ nil,
+ nil,
+ ); err != nil {
+ return err
+ }
+
+ return nil
+}
diff --git a/src/cardmarket/http.go b/src/cardmarket/http.go
index 4789fdf..010f884 100644
--- a/src/cardmarket/http.go
+++ b/src/cardmarket/http.go
@@ -3,13 +3,85 @@ package cardmarket
import (
"context"
"encoding/json"
+ "errors"
"io"
"log/slog"
+ "strings"
)
-func (c *ApiClient) get(ctx context.Context, endpoint string, respBody any) error {
+func (c *ApiClient) Get(
+ ctx context.Context,
+ endpoint string,
+ params map[string]string,
+ respBody any,
+) error {
+ url := c.baseURL + endpoint
+
+ slog.DebugContext(ctx, "building query param string...")
+ parts := make([]string, len(params))
+ i := 0
+ for k, v := range params {
+ parts[i] = k + "=" + v
+ i++
+ }
+ query := strings.Join(parts, "&")
+ if query != "" {
+ url += "?" + query
+ }
+ slog.DebugContext(ctx, "built query param string")
+
+ slog.DebugContext(ctx, "requesting entity info...")
+ resp, err := c.httpClient.GetWithContext(ctx, url)
+ if err != nil {
+ slog.DebugContext(ctx, "failed to get entity from API")
+ return err
+ }
+ slog.DebugContext(ctx, "received entity info")
+
+ slog.DebugContext(ctx, "reading body...")
+ respBodyBuf, err := io.ReadAll(resp.Body)
+ if err != nil {
+ slog.DebugContext(ctx, "failed to read body")
+ return err
+ }
+ defer resp.Body.Close()
+ slog.DebugContext(ctx, "read body")
+
+ slog.DebugContext(ctx, "parsing body...", "respBodyBuf", string(respBodyBuf))
+ if err = json.Unmarshal(respBodyBuf, respBody); err != nil {
+ slog.DebugContext(ctx, "failed to unmarshal body: "+string(respBodyBuf))
+ return err
+ }
+ slog.DebugContext(ctx, "parsed body")
+
+ return nil
+}
+
+func (c *ApiClient) Put(
+ ctx context.Context,
+ endpoint string,
+ params map[string]string,
+ contentType string,
+ reqBody io.Reader,
+ respBody any,
+) error {
+ url := c.baseURL + endpoint
+
+ slog.DebugContext(ctx, "building query param string...")
+ parts := make([]string, len(params))
+ i := 0
+ for k, v := range params {
+ parts[i] = k + "=" + v
+ i++
+ }
+ query := strings.Join(parts, "&")
+ if query != "" {
+ url += "?" + query
+ }
+ slog.DebugContext(ctx, "built query param string")
+
slog.DebugContext(ctx, "requesting entity info...")
- resp, err := c.httpClient.GetWithContext(ctx, c.baseURL+endpoint)
+ resp, err := c.httpClient.PutWithContext(ctx, url, contentType, reqBody)
if err != nil {
slog.DebugContext(ctx, "failed to get entity from API")
return err
@@ -25,6 +97,18 @@ func (c *ApiClient) get(ctx context.Context, endpoint string, respBody any) erro
defer resp.Body.Close()
slog.DebugContext(ctx, "read body")
+ if resp.StatusCode < 200 || resp.StatusCode > 299 {
+ msg := "status code outside of successful range - " + resp.Status
+ if len(respBodyBuf) > 0 {
+ msg += " body=" + string(respBodyBuf)
+ }
+ return errors.New(msg)
+ }
+
+ if respBody == nil {
+ return nil
+ }
+
slog.DebugContext(ctx, "parsing body...", "respBodyBuf", string(respBodyBuf))
if err = json.Unmarshal(respBodyBuf, respBody); err != nil {
slog.DebugContext(ctx, "failed to unmarshal body: "+string(respBodyBuf))