-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (107 loc) · 3.31 KB
/
main.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
124
125
package sdk
import (
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
"connectrpc.com/connect"
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/query/queryconnect"
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/submit/submitconnect"
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/sync/syncconnect"
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/watch/watchconnect"
"golang.org/x/net/http2"
)
type UtxorpcClient struct {
httpClient connect.HTTPClient
baseUrl string
headers map[string]string
Query queryconnect.QueryServiceClient
Submit submitconnect.SubmitServiceClient
Sync syncconnect.SyncServiceClient
Watch watchconnect.WatchServiceClient
}
type ClientOption func(*UtxorpcClient)
func WithHeaders(headers map[string]string) ClientOption {
return func(client *UtxorpcClient) {
client.headers = headers
}
}
func NewClient(httpClient *http.Client, baseUrl string, options ...ClientOption) *UtxorpcClient {
client := &UtxorpcClient{
httpClient: httpClient,
baseUrl: baseUrl,
Query: queryconnect.NewQueryServiceClient(httpClient, baseUrl, connect.WithGRPC()),
Submit: submitconnect.NewSubmitServiceClient(httpClient, baseUrl, connect.WithGRPC()),
Sync: syncconnect.NewSyncServiceClient(httpClient, baseUrl, connect.WithGRPC()),
Watch: watchconnect.NewWatchServiceClient(httpClient, baseUrl, connect.WithGRPC()),
}
for _, option := range options {
option(client)
}
return client
}
func (u *UtxorpcClient) HTTPClient() connect.HTTPClient {
return u.httpClient
}
func (u *UtxorpcClient) URL() string {
return u.baseUrl
}
func createHttpClient() *http.Client {
return &http.Client{
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
},
Transport: &http2.Transport{
AllowHTTP: true,
DialTLS: func(network, addr string, tlsConfig *tls.Config) (net.Conn, error) {
// If you're also using this client for non-h2c traffic, you may want
// to delegate to tls.Dial if the network isn't TCP or the addr isn't
// in an allowlist.
// return net.Dial(network, addr)
// Establish a TLS connection using the custom TLS configuration
conn, err := tls.Dial(network, addr, tlsConfig)
if err != nil {
return nil, fmt.Errorf("failed to establish TLS connection: %w", err)
}
return conn, nil
},
},
}
}
func CreateUtxoRPCClient(baseUrl string, options ...ClientOption) *UtxorpcClient {
httpClient := createHttpClient()
return NewClient(httpClient, baseUrl, options...)
}
func (u *UtxorpcClient) Headers() map[string]string {
headers := u.headers
if headers == nil {
headers = make(map[string]string)
}
return headers
}
func (u *UtxorpcClient) SetHeader(key, value string) {
if u.headers == nil {
u.headers = make(map[string]string)
}
u.headers[key] = value
}
func (u *UtxorpcClient) SetHeaders(headers map[string]string) {
u.headers = headers
}
func (u *UtxorpcClient) RemoveHeader(key string) {
delete(u.headers, key)
}
func (u *UtxorpcClient) AddHeadersToRequest(req connect.AnyRequest) {
for key, value := range u.headers {
req.Header().Set(key, value)
}
}
func HandleError(err error) {
fmt.Println(connect.CodeOf(err))
if connectErr := new(connect.Error); errors.As(err, &connectErr) {
fmt.Println(connectErr.Message())
fmt.Println(connectErr.Details())
}
panic(err)
}