Skip to content

Commit

Permalink
Merge pull request #17 from ethpandaops/feat/header-support
Browse files Browse the repository at this point in the history
feat(api): add support for headers in HTTP requests
  • Loading branch information
samcm authored Jun 19, 2023
2 parents 58d9da9 + 3b67da8 commit 28255cd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
61 changes: 44 additions & 17 deletions pkg/beacon/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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"
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/beacon/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/beacon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 28255cd

Please sign in to comment.