summaryrefslogtreecommitdiff
path: root/src/cardmarket/http.go
blob: 4789fdf038c4c293647d870ad1b08dd9ff1648c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package cardmarket

import (
	"context"
	"encoding/json"
	"io"
	"log/slog"
)

func (c *ApiClient) get(ctx context.Context, endpoint string, respBody any) error {
	slog.DebugContext(ctx, "requesting entity info...")
	resp, err := c.httpClient.GetWithContext(ctx, c.baseURL+endpoint)
	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
}