-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
45 lines (35 loc) · 876 Bytes
/
client.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
package moonshot
import (
"github.com/northes/go-moonshot/internal/httpx"
"github.com/northes/go-moonshot/internal/httpx/tools"
)
type Client struct {
cfg *Config
}
// NewClient creates a new client
func NewClient(key string) (*Client, error) {
cfg := NewConfig(
WithAPIKey(key),
)
if err := cfg.preCheck(); err != nil {
return nil, err
}
return NewClientWithConfig(cfg)
}
// NewClientWithConfig creates a new client with a custom configuration
func NewClientWithConfig(cfg *Config) (*Client, error) {
if cfg == nil {
cfg = newConfigDefault()
}
if err := cfg.preCheck(); err != nil {
return nil, err
}
c := &Client{
cfg: cfg,
}
return c, nil
}
// HTTPClient returns a new http client
func (c *Client) HTTPClient() *httpx.Client {
return httpx.NewClient(c.cfg.Host).AddHeader(tools.AuthorizationHeaderKey, tools.ToBearToken(c.cfg.APIKey))
}