-
Notifications
You must be signed in to change notification settings - Fork 0
/
lenovo.go
76 lines (62 loc) · 1.45 KB
/
lenovo.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
67
68
69
70
71
72
73
74
75
76
package lenovo
import (
"errors"
"net/http"
"net/http/cookiejar"
"golang.org/x/net/publicsuffix"
)
var (
ErrNoClientID = errors.New("no ClientID set")
)
type ClientOptionFunc func(*Client) error
type Client struct {
c *http.Client
id string
}
// NewClient creates a new client to work with Lenovo eSupport.
func NewClient(options ...ClientOptionFunc) (*Client, error) {
c := &Client{
c: http.DefaultClient,
}
// Run the options on it
for _, option := range options {
if err := option(c); err != nil {
return nil, err
}
}
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
return nil, err
}
c.c.Jar = jar
if c.id == "" {
return nil, ErrNoClientID
}
return c, nil
}
// SetHttpClient can be used to specify the http.Client to use when making
// HTTP requests to Lenovo eSupport.
func SetHttpClient(httpClient *http.Client) ClientOptionFunc {
return func(c *Client) error {
if httpClient != nil {
c.c = httpClient
} else {
c.c = http.DefaultClient
}
return nil
}
}
// SetClientID defines the ClientID which is needed to authenticate with
// Lenovo eSupport.
func SetClientID(id string) ClientOptionFunc {
return func(c *Client) error {
c.id = id
return nil
}
}
// sendRequest sends a http.Request and append the ClientID for
// authentication.
func (c *Client) sendRequest(req *http.Request) (*http.Response, error) {
req.Header.Add("ClientID", c.id)
return c.c.Do(req)
}