-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingram.go
66 lines (54 loc) · 1.12 KB
/
ingram.go
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
package ingram
import (
"github.com/go-playground/validator/v10"
)
const apiEndpoint = "https://api.ingrammicro.com"
type Ingram struct {
clientID string
clientSecret string
isSandbox bool
endpoint string
token *Token
validate *validator.Validate
logger Logger
}
type OptionFunc func(i *Ingram) error
func WithOAuthCredentials(clientID, clientSecret string) OptionFunc {
return func(i *Ingram) error {
i.clientID = clientID
i.clientSecret = clientSecret
return nil
}
}
func EnableSandbox() OptionFunc {
return func(i *Ingram) error {
i.isSandbox = true
return nil
}
}
func WithLogger(logger Logger) OptionFunc {
return func(i *Ingram) error {
i.logger = logger
return nil
}
}
func New(options ...OptionFunc) (*Ingram, error) {
i := &Ingram{
validate: validator.New(),
}
for _, v := range options {
err := v(i)
if err != nil {
return nil, err
}
}
i.endpoint = apiEndpoint
if i.isSandbox {
i.endpoint += "/sandbox"
}
return i, nil
}
// Logger specifies the interface for all log operations.
type Logger interface {
Printf(format string, v ...interface{})
}