diff options
Diffstat (limited to 'src/cardmarket/http.go')
| -rw-r--r-- | src/cardmarket/http.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/cardmarket/http.go b/src/cardmarket/http.go new file mode 100644 index 0000000..4789fdf --- /dev/null +++ b/src/cardmarket/http.go @@ -0,0 +1,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 +} |
