-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
78 lines (64 loc) · 1.8 KB
/
Copy pathclient.go
File metadata and controls
78 lines (64 loc) · 1.8 KB
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
// Package spotifyprivateapi provides a client for interacting with Spotify's private API.
package spotifyprivateapi
import (
"context"
"log/slog"
"os"
internalhttp "github.com/FrostBreker/spotify-private-api/internal/http"
)
// Client is the main client for interacting with the Spotify API.
type Client struct {
httpClient internalhttp.Doer
logger *slog.Logger
debug bool
}
// Option is a functional option for configuring a Client.
type Option func(*Client)
// WithHTTPDoer sets a custom HTTP doer interface for the Client.
// This is useful for testing with mock HTTP clients.
func WithHTTPDoer(doer internalhttp.Doer) Option {
return func(c *Client) {
c.httpClient = doer
}
}
// WithLogger sets a custom logger for the Client.
func WithLogger(logger *slog.Logger) Option {
return func(c *Client) {
c.logger = logger
}
}
// WithDebug enables debug logging.
func WithDebug(debug bool) Option {
return func(c *Client) {
c.debug = debug
}
}
// NewClient creates a new Spotify client with the given options.
func NewClient(opts ...Option) *Client {
c := &Client{
httpClient: internalhttp.NewDefaultClient(),
logger: slog.New(slog.NewTextHandler(os.Stdout, nil)),
debug: false,
}
for _, opt := range opts {
opt(c)
}
if c.debug {
c.logger.Info("Spotify client initialized")
}
return c
}
// log logs a message if debug mode is enabled.
func (c *Client) log(level slog.Level, msg string, args ...any) {
if c.debug {
c.logger.Log(context.Background(), level, msg, args...)
}
}
// logInfo logs an info message if debug mode is enabled.
func (c *Client) logInfo(msg string, args ...any) {
c.log(slog.LevelInfo, msg, args...)
}
// logError logs an error message if debug mode is enabled.
func (c *Client) logError(msg string, args ...any) {
c.log(slog.LevelError, msg, args...)
}