allow using a custom http client.

fixes nostr:nevent1qvzqqqqx25pzqm8ksn7p6aak225sed38vlzngtuwl50tf0e8ahzuzkhpmuahzgzdqyd8wumn8ghj7cmpvd5x2v3wwpexjmtpdshxuet59amrzqg4waehxw309aex2mrp0yhxgctdw4eju6t09uqzq8r9r4par63whq6px0af5uxtkkx0psydtamq6rdcva248l27l2szensns3
This commit is contained in:
fiatjaf
2025-12-18 12:01:30 -03:00
parent 4d7f7ce25d
commit 97424e363a
4 changed files with 26 additions and 54 deletions

View File

@@ -114,11 +114,20 @@ func (r *Relay) IsConnected() bool { return !r.closed.Load() }
// The given context here is only used during the connection phase. The long-living
// relay connection will be based on the context given to NewRelay().
func (r *Relay) Connect(ctx context.Context) error {
return r.ConnectWithTLS(ctx, nil)
return r.ConnectWithClient(ctx, nil)
}
// ConnectWithTLS is like Connect(), but takes a special tls.Config if you need that.
func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error {
return r.ConnectWithClient(ctx, &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
})
}
// ConnectWithClient is like Connect(), but takes a special *http.Client if you need that.
func (r *Relay) ConnectWithClient(ctx context.Context, client *http.Client) error {
if r.connectionContext == nil || r.Subscriptions == nil {
return fmt.Errorf("relay must be initialized with a call to NewRelay()")
}
@@ -127,7 +136,7 @@ func (r *Relay) ConnectWithTLS(ctx context.Context, tlsConfig *tls.Config) error
return fmt.Errorf("invalid relay URL '%s'", r.URL)
}
if err := r.newConnection(ctx, tlsConfig); err != nil {
if err := r.newConnection(ctx, client); err != nil {
return fmt.Errorf("error opening websocket to '%s': %w", r.URL, err)
}