-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
123 lines (92 loc) · 2.59 KB
/
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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package helheim_go
import (
"fmt"
"sync"
)
type Client interface {
NewSession(options CreateSessionOptions) (Session, error)
DeleteSession(sessionId int) error
GetBalance() (*BalanceResponse, error)
GetHelheim() Helheim
NewHttpClient(sessionOptions CreateSessionOptions, options ...HttpClientOption) (HttpClient, error)
SetLogger(logger Logger)
}
type client struct {
logger Logger
helheim Helheim
}
var clientContainer = struct {
sync.Mutex
instance Client
}{}
func ProvideClient(apiKey string, discover bool, withAutoReAuth bool, logger Logger) (Client, error) {
clientContainer.Lock()
defer clientContainer.Unlock()
if clientContainer.instance != nil {
return clientContainer.instance, nil
}
instance, err := NewClient(apiKey, discover, withAutoReAuth, logger)
if err != nil {
return nil, err
}
clientContainer.instance = instance
return clientContainer.instance, nil
}
func NewClient(apiKey string, discover bool, withAutoReAuth bool, logger Logger) (Client, error) {
if logger == nil {
logger = NewNoopLogger()
}
h, err := newHelheim(apiKey, discover, withAutoReAuth, logger)
if err != nil {
logger.Error("failed to create helheim client: %w", err)
return nil, err
}
logger.Info("created new helheim client")
return &client{
logger: logger,
helheim: h,
}, nil
}
func (c *client) NewHttpClient(sessionOptions CreateSessionOptions, options ...HttpClientOption) (HttpClient, error) {
s, err := c.NewSession(sessionOptions)
if err != nil {
c.logger.Error("failed to create default session for helheim http client: %w", err)
return nil, err
}
return newHttpClient(c.logger, s, options...), nil
}
func (c *client) NewSession(options CreateSessionOptions) (Session, error) {
s, err := newSession(c.logger, c.helheim, options)
if err != nil {
c.logger.Error("failed to create session: %w", err)
return nil, err
}
c.logger.Info("created new session with id: %d", s.GetSessionId())
return s, nil
}
func (c *client) GetBalance() (*BalanceResponse, error) {
b, err := c.helheim.GetBalance()
if err != nil {
c.logger.Error("failed to retrieve balance: %w", err)
return nil, err
}
return b, nil
}
func (c *client) SetLogger(logger Logger) {
c.logger = logger
c.helheim.SetLogger(logger)
}
func (c *client) GetHelheim() Helheim {
return c.helheim
}
func (c *client) DeleteSession(sessionId int) error {
resp, err := c.helheim.DeleteSession(sessionId)
if err != nil {
c.logger.Error("failed to delete session: %w", err)
return err
}
if resp != nil && resp.Error != false {
return fmt.Errorf("failed to delete session %d", sessionId)
}
return nil
}