-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathclient.go
72 lines (60 loc) · 1.71 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
package client
import (
"context"
"fmt"
"net/http"
clientbuilder "github.com/celestiaorg/celestia-openrpc/builder"
"github.com/celestiaorg/celestia-openrpc/types/blob"
"github.com/celestiaorg/celestia-openrpc/types/da"
"github.com/celestiaorg/celestia-openrpc/types/das"
"github.com/celestiaorg/celestia-openrpc/types/fraud"
"github.com/celestiaorg/celestia-openrpc/types/header"
"github.com/celestiaorg/celestia-openrpc/types/node"
"github.com/celestiaorg/celestia-openrpc/types/p2p"
"github.com/celestiaorg/celestia-openrpc/types/share"
"github.com/celestiaorg/celestia-openrpc/types/state"
"github.com/filecoin-project/go-jsonrpc"
)
const AuthKey = "Authorization"
type Client struct {
Fraud fraud.API
Blob blob.API
Header header.API
State state.API
Share share.API
DAS das.API
P2P p2p.API
Node node.API
DA da.API
closer clientbuilder.MultiClientCloser
}
// Close closes the connections to all namespaces registered on the client.
func (c *Client) Close() {
c.closer.CloseAll()
}
func NewClient(ctx context.Context, addr string, token string) (*Client, error) {
var authHeader http.Header
if token != "" {
authHeader = http.Header{AuthKey: []string{fmt.Sprintf("Bearer %s", token)}}
}
var client Client
modules := map[string]interface{}{
"fraud": &client.Fraud,
"blob": &client.Blob,
"header": &client.Header,
"state": &client.State,
"share": &client.Share,
"das": &client.DAS,
"p2p": &client.P2P,
"node": &client.Node,
"da": &client.DA,
}
for name, module := range modules {
closer, err := jsonrpc.NewClient(ctx, addr, name, module, authHeader)
if err != nil {
return nil, err
}
client.closer.Register(closer)
}
return &client, nil
}