diff --git a/jsonrpc/client.go b/jsonrpc/client.go index 5561ed60..42a8bdfa 100644 --- a/jsonrpc/client.go +++ b/jsonrpc/client.go @@ -16,16 +16,33 @@ type endpoints struct { n *Net d *Debug } +type Config struct { + headers map[string]string +} + +type ConfigOption func(*Config) + +func WithHeaders(headers map[string]string) ConfigOption { + return func(c *Config) { + for k, v := range headers { + c.headers[k] = v + } + } +} + +func NewClient(addr string, opts ...ConfigOption) (*Client, error) { + config := &Config{headers: map[string]string{}} + for _, opt := range opts { + opt(config) + } -// NewClient creates a new client -func NewClient(addr string) (*Client, error) { c := &Client{} c.endpoints.w = &Web3{c} c.endpoints.e = &Eth{c} c.endpoints.n = &Net{c} c.endpoints.d = &Debug{c} - t, err := transport.NewTransport(addr) + t, err := transport.NewTransport(addr, config.headers) if err != nil { return nil, err } @@ -33,7 +50,7 @@ func NewClient(addr string) (*Client, error) { return c, nil } -// Close closes the tranport +// Close closes the transport func (c *Client) Close() error { return c.transport.Close() } diff --git a/jsonrpc/transport/http.go b/jsonrpc/transport/http.go index 597831c1..9bcefeff 100644 --- a/jsonrpc/transport/http.go +++ b/jsonrpc/transport/http.go @@ -11,12 +11,14 @@ import ( type HTTP struct { addr string client *fasthttp.Client + headers map[string]string } -func newHTTP(addr string) *HTTP { +func newHTTP(addr string, headers map[string]string) *HTTP { return &HTTP{ addr: addr, client: &fasthttp.Client{}, + headers: headers, } } @@ -53,6 +55,9 @@ func (h *HTTP) Call(method string, out interface{}, params ...interface{}) error req.SetRequestURI(h.addr) req.Header.SetMethod("POST") req.Header.SetContentType("application/json") + for k, v := range h.headers { + req.Header.Add(k, v) + } req.SetBody(raw) if err := h.client.Do(req, res); err != nil { diff --git a/jsonrpc/transport/transport.go b/jsonrpc/transport/transport.go index a99214ee..10e7390e 100644 --- a/jsonrpc/transport/transport.go +++ b/jsonrpc/transport/transport.go @@ -29,9 +29,9 @@ const ( ) // NewTransport creates a new transport object -func NewTransport(url string) (Transport, error) { +func NewTransport(url string, headers map[string]string) (Transport, error) { if strings.HasPrefix(url, wsPrefix) || strings.HasPrefix(url, wssPrefix) { - t, err := newWebsocket(url) + t, err := newWebsocket(url, headers) if err != nil { return nil, err } @@ -45,5 +45,5 @@ func NewTransport(url string) (Transport, error) { } return t, nil } - return newHTTP(url), nil + return newHTTP(url, headers), nil } diff --git a/jsonrpc/transport/websocket.go b/jsonrpc/transport/websocket.go index 6fbad1fa..ca02b3a1 100644 --- a/jsonrpc/transport/websocket.go +++ b/jsonrpc/transport/websocket.go @@ -12,7 +12,11 @@ import ( "github.com/umbracle/go-web3/jsonrpc/codec" ) -func newWebsocket(url string) (Transport, error) { +func newWebsocket(url string, headers map[string]string) (Transport, error) { + wsHeaders := http.Header{} + for k, v := range headers { + wsHeaders.Add(k, v) + } wsConn, _, err := websocket.DefaultDialer.Dial(url, http.Header{}) if err != nil { return nil, err