diff --git a/pkg/beacon/api/api.go b/pkg/beacon/api/api.go index b894098..75ac928 100644 --- a/pkg/beacon/api/api.go +++ b/pkg/beacon/api/api.go @@ -23,17 +23,19 @@ type ConsensusClient interface { } type consensusClient struct { - url string - log logrus.FieldLogger - client http.Client + url string + log logrus.FieldLogger + client http.Client + headers map[string]string } // NewConsensusClient creates a new ConsensusClient. -func NewConsensusClient(ctx context.Context, log logrus.FieldLogger, url string, client http.Client) ConsensusClient { +func NewConsensusClient(ctx context.Context, log logrus.FieldLogger, url string, client http.Client, headers map[string]string) ConsensusClient { return &consensusClient{ - url: url, - log: log, - client: client, + url: url, + log: log, + client: client, + headers: headers, } } @@ -48,7 +50,17 @@ func (c *consensusClient) post(ctx context.Context, path string, body map[string return nil, err } - rsp, err := c.client.Post(c.url, "application/json", bytes.NewBuffer(jsonData)) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.url+path, bytes.NewBuffer(jsonData)) + if err != nil { + return nil, err + } + + // Set headers from c.headers + for k, v := range c.headers { + req.Header.Set(k, v) + } + + rsp, err := c.client.Do(req) if err != nil { return nil, err } @@ -74,7 +86,17 @@ func (c *consensusClient) post(ctx context.Context, path string, body map[string //nolint:unparam // ctx will probably be used in the future func (c *consensusClient) get(ctx context.Context, path string) (json.RawMessage, error) { - rsp, err := c.client.Get(c.url + path) + req, err := http.NewRequestWithContext(ctx, "GET", c.url+path, nil) + if err != nil { + return nil, err + } + + // Set headers from c.headers + for k, v := range c.headers { + req.Header.Set(k, v) + } + + rsp, err := c.client.Do(req) if err != nil { return nil, err } @@ -98,7 +120,6 @@ func (c *consensusClient) get(ctx context.Context, path string) (json.RawMessage return resp.Data, nil } -//nolint:unparam // ctx will probably be used in the future func (c *consensusClient) getRaw(ctx context.Context, path string, contentType string) ([]byte, error) { if contentType == "" { contentType = "application/json" @@ -108,13 +129,19 @@ func (c *consensusClient) getRaw(ctx context.Context, path string, contentType s return nil, err } - rsp, err := c.client.Do(&http.Request{ - Method: "GET", - URL: u, - Header: map[string][]string{ - "Accept": {contentType}, - }, - }) + req, err := http.NewRequestWithContext(ctx, "GET", u.String(), nil) + if err != nil { + return nil, err + } + + // Set headers from c.headers + for k, v := range c.headers { + req.Header.Set(k, v) + } + + req.Header.Set("Accept", contentType) + + rsp, err := c.client.Do(req) if err != nil { return nil, err } diff --git a/pkg/beacon/bootstrap.go b/pkg/beacon/bootstrap.go index 8769f3e..da431ba 100644 --- a/pkg/beacon/bootstrap.go +++ b/pkg/beacon/bootstrap.go @@ -33,6 +33,7 @@ func (n *node) ensureClients(ctx context.Context) error { ehttp.WithAddress(n.config.Addr), ehttp.WithLogLevel(zerolog.Disabled), ehttp.WithTimeout(timeout), + ehttp.WithExtraHeaders(n.config.Headers), ) if err != nil { failures++ @@ -57,7 +58,7 @@ func (n *node) ensureClients(ctx context.Context) error { Timeout: timeout, } - n.api = api.NewConsensusClient(ctx, n.log, n.config.Addr, httpClient) + n.api = api.NewConsensusClient(ctx, n.log, n.config.Addr, httpClient, n.config.Headers) break } diff --git a/pkg/beacon/config.go b/pkg/beacon/config.go index 39aebf1..d8fa09b 100644 --- a/pkg/beacon/config.go +++ b/pkg/beacon/config.go @@ -6,4 +6,6 @@ type Config struct { Name string `yaml:"name"` // Address is the address of the node. Addr string `yaml:"addr"` + // Headers are the headers to send with every request. + Headers map[string]string `yaml:"headers"` }