summaryrefslogtreecommitdiff
path: root/src/cardmarket/http.go
blob: 010f884f3641aad694734158ebdd3b3912dcce64 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package cardmarket

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

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.PutWithContext(ctx, url, contentType, reqBody)
	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")

	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))
		return err
	}
	slog.DebugContext(ctx, "parsed body")

	return nil
}