forked from jsgoecke/tesla
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathclient_options.go
69 lines (60 loc) · 1.49 KB
/
client_options.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
package tesla
import (
"encoding/json"
"os"
"golang.org/x/oauth2"
)
// ClientOption can be passed when creating the client
type ClientOption func(c *Client) error
// WithToken provides an oauth2.Token to the client for auth.
func WithToken(t *oauth2.Token) ClientOption {
return func(c *Client) error {
c.token = t
return nil
}
}
func loadToken(path string) (*oauth2.Token, error) {
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
tok := new(oauth2.Token)
if err := json.Unmarshal(b, tok); err != nil {
return nil, err
}
return tok, nil
}
// WithTokenFile reads a JSON serialized oauth2.Token struct from disk and provides it
// to the client for auth.
func WithTokenFile(path string) ClientOption {
t, err := loadToken(path)
if err != nil {
return func(c *Client) error {
return err
}
}
return WithToken(t)
}
// WithBaseURL provides a method to set the base URL for standard API calls to differ
// from the default.
func WithBaseURL(url string) ClientOption {
return func(c *Client) error {
c.baseURL = url
return nil
}
}
// WithStreamingURL provides a method to set the base URL for streaming API calls to differ
// from the default.
func WithStreamingURL(url string) ClientOption {
return func(c *Client) error {
c.streamingURL = url
return nil
}
}
// WithOAuth2Config allows a consumer to provide a different configuration from the default.
func WithOAuth2Config(oc *oauth2.Config) ClientOption {
return func(c *Client) error {
c.oc = oc
return nil
}
}