-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathoptions.go
86 lines (68 loc) · 2.02 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package misskey
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/yitsushi/go-misskey/core"
)
// ClientOption is a function that can be used to configure a client.
type ClientOption func(*Client) error
// ClientOptionError occures when something goes wrong with any of the requested
// options.
type ClientOptionError struct {
Message string
}
func (e ClientOptionError) Error() string {
return fmt.Sprintf("client options error: %s", e.Message)
}
// WithAPIToken configures the API token on the client.
func WithAPIToken(token string) ClientOption {
return func(client *Client) error {
client.Token = token
return nil
}
}
// WithBaseURL configures the base url of the Misskey instance.
//
// - Protocol: http, https
// - Domain: Well, that's the domain name
// - Path: Leave it empty, unless the target instance is not served from the
// root path. Important: Do not add a tailing slash.
func WithBaseURL(protocol, domain, path string) ClientOption {
return func(client *Client) error {
if domain == "" {
return ClientOptionError{Message: "undefined value: domain"}
}
if protocol == "" {
protocol = "https"
}
if path != "" && path[0] != '/' {
path = "/" + path
}
client.BaseURL = fmt.Sprintf("%s://%s%s", protocol, domain, path)
return nil
}
}
// WithLogLevel configures the logger to use the specified log level.
func WithLogLevel(level logrus.Level) ClientOption {
return func(client *Client) error {
client.LogLevel(level)
return nil
}
}
// WithSimpleConfig configures the client with similar logic as NewClient().
//
// The sole purpose of this to make it easier to migrate to the new function.
func WithSimpleConfig(baseURL, token string) ClientOption {
return func(client *Client) error {
client.BaseURL = baseURL
client.Token = token
return nil
}
}
// WithHTTPClient configures an HTTP Client instead of creating a new one.
func WithHTTPClient(httpClient core.HTTPClient) ClientOption {
return func(client *Client) error {
client.HTTPClient = httpClient
return nil
}
}