-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
143 lines (122 loc) · 3.08 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package sdk
import (
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
"connectrpc.com/connect"
"golang.org/x/net/http2"
)
type UtxorpcClient struct {
httpClient connect.HTTPClient
baseUrl string
headers map[string]string
Query QueryServiceClient
Submit SubmitServiceClient
Sync SyncServiceClient
Watch WatchServiceClient
}
type ClientOption func(*UtxorpcClient)
func WithBaseUrl(baseUrl string) ClientOption {
return func(u *UtxorpcClient) {
u.baseUrl = baseUrl
}
}
func WithHeaders(headers map[string]string) ClientOption {
return func(u *UtxorpcClient) {
u.headers = headers
}
}
func WithHttpClient(httpClient connect.HTTPClient) ClientOption {
return func(u *UtxorpcClient) {
u.httpClient = httpClient
}
}
func NewClient(options ...ClientOption) *UtxorpcClient {
u := &UtxorpcClient{}
for _, option := range options {
option(u)
}
if u.httpClient == nil {
u.httpClient = createHttpClient()
}
u.Query = u.NewQueryServiceClient()
u.Submit = u.NewSubmitServiceClient()
u.Sync = u.NewSyncServiceClient()
u.Watch = u.NewWatchServiceClient()
return u
}
func (u *UtxorpcClient) reset() {
u.Query = u.NewQueryServiceClient()
u.Submit = u.NewSubmitServiceClient()
u.Sync = u.NewSyncServiceClient()
u.Watch = u.NewWatchServiceClient()
}
func (u *UtxorpcClient) HTTPClient() connect.HTTPClient {
return u.httpClient
}
func (u *UtxorpcClient) SetURL(baseUrl string) {
u.baseUrl = baseUrl
u.reset()
}
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 (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)
}