@@ -23,6 +23,7 @@ import (
23
23
"fmt"
24
24
"io"
25
25
"net/http"
26
+ "net/url"
26
27
"sync"
27
28
"time"
28
29
@@ -121,7 +122,11 @@ func NewBeaconLightApi(url string, customHeaders map[string]string) *BeaconLight
121
122
}
122
123
123
124
func (api * BeaconLightApi ) httpGet (path string ) ([]byte , error ) {
124
- req , err := http .NewRequest ("GET" , api .url + path , nil )
125
+ uri , err := api .buildURL (path , nil )
126
+ if err != nil {
127
+ return nil , err
128
+ }
129
+ req , err := http .NewRequest ("GET" , uri , nil )
125
130
if err != nil {
126
131
return nil , err
127
132
}
@@ -545,9 +550,13 @@ func (api *BeaconLightApi) StartHeadListener(listener HeadEventListener) func()
545
550
// established. It can only return nil when the context is canceled.
546
551
func (api * BeaconLightApi ) startEventStream (ctx context.Context , listener * HeadEventListener ) * eventsource.Stream {
547
552
for retry := true ; retry ; retry = ctxSleep (ctx , 5 * time .Second ) {
548
- path := "/eth/v1/events?topics=head&topics=light_client_finality_update&topics=light_client_optimistic_update"
549
553
log .Trace ("Sending event subscription request" )
550
- req , err := http .NewRequestWithContext (ctx , "GET" , api .url + path , nil )
554
+ uri , err := api .buildURL ("/eth/v1/events" , map [string ][]string {"topics" : {"head" , "light_client_finality_update" , "light_client_optimistic_update" }})
555
+ if err != nil {
556
+ listener .OnError (fmt .Errorf ("error creating event subscription URL: %v" , err ))
557
+ continue
558
+ }
559
+ req , err := http .NewRequestWithContext (ctx , "GET" , uri , nil )
551
560
if err != nil {
552
561
listener .OnError (fmt .Errorf ("error creating event subscription request: %v" , err ))
553
562
continue
@@ -576,3 +585,15 @@ func ctxSleep(ctx context.Context, timeout time.Duration) (ok bool) {
576
585
return false
577
586
}
578
587
}
588
+
589
+ func (api * BeaconLightApi ) buildURL (path string , params url.Values ) (string , error ) {
590
+ uri , err := url .Parse (api .url )
591
+ if err != nil {
592
+ return "" , err
593
+ }
594
+ uri = uri .JoinPath (path )
595
+ if params != nil {
596
+ uri .RawQuery = params .Encode ()
597
+ }
598
+ return uri .String (), nil
599
+ }
0 commit comments