Compare commits

...

1 Commits

Author SHA1 Message Date
Kasey Kirkham
b1fc178b7d support env var to customize Accept header 2025-06-26 12:08:23 -05:00
3 changed files with 49 additions and 5 deletions

View File

@@ -6,6 +6,7 @@ import (
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
"os"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@@ -14,14 +15,17 @@ const (
MaxBodySize int64 = 1 << 23 // 8MB default, WithMaxBodySize can override MaxBodySize int64 = 1 << 23 // 8MB default, WithMaxBodySize can override
MaxBodySizeState int64 = 1 << 29 // 512MB MaxBodySizeState int64 = 1 << 29 // 512MB
MaxErrBodySize int64 = 1 << 17 // 128KB MaxErrBodySize int64 = 1 << 17 // 128KB
envNameOverrideAccept = "PRYSM_API_OVERRIDE_ACCEPT"
) )
// Client is a wrapper object around the HTTP client. // Client is a wrapper object around the HTTP client.
type Client struct { type Client struct {
hc *http.Client hc *http.Client
baseURL *url.URL baseURL *url.URL
token string token string
maxBodySize int64 maxBodySize int64
reqOverrides []ReqOption
} }
// NewClient constructs a new client with the provided options (ex WithTimeout). // NewClient constructs a new client with the provided options (ex WithTimeout).
@@ -40,9 +44,21 @@ func NewClient(host string, opts ...ClientOpt) (*Client, error) {
for _, o := range opts { for _, o := range opts {
o(c) o(c)
} }
c.appendAcceptOverride()
return c, nil return c, nil
} }
// appendAcceptOverride enables the Accept header to be customized at runtime via an environment variable.
// This is specified as an env var because it is a niche option that prysm may use for performance testing or debugging
// bug which users are unlikely to need. Using an env var keeps the set of user-facing flags cleaner.
func (c *Client) appendAcceptOverride() {
if accept := os.Getenv(envNameOverrideAccept); accept != "" {
c.reqOverrides = append(c.reqOverrides, func(req *http.Request) {
req.Header.Set("Accept", accept)
})
}
}
// Token returns the bearer token used for jwt authentication // Token returns the bearer token used for jwt authentication
func (c *Client) Token() string { func (c *Client) Token() string {
return c.token return c.token
@@ -55,6 +71,9 @@ func (c *Client) BaseURL() *url.URL {
// Do execute the request against the http client // Do execute the request against the http client
func (c *Client) Do(req *http.Request) (*http.Response, error) { func (c *Client) Do(req *http.Request) (*http.Response, error) {
for _, o := range c.reqOverrides {
o(req)
}
return c.hc.Do(req) return c.hc.Do(req)
} }
@@ -87,7 +106,7 @@ func (c *Client) Get(ctx context.Context, path string, opts ...ReqOption) ([]byt
for _, o := range opts { for _, o := range opts {
o(req) o(req)
} }
r, err := c.hc.Do(req) r, err := c.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -1,7 +1,10 @@
package client package client
import ( import (
"net/http"
"net/http/httptest"
"net/url" "net/url"
"os"
"testing" "testing"
"github.com/OffchainLabs/prysm/v6/testing/require" "github.com/OffchainLabs/prysm/v6/testing/require"
@@ -46,3 +49,23 @@ func TestBaseURL(t *testing.T) {
require.Equal(t, "www.offchainlabs.com", cl.BaseURL().Hostname()) require.Equal(t, "www.offchainlabs.com", cl.BaseURL().Hostname())
require.Equal(t, "3500", cl.BaseURL().Port()) require.Equal(t, "3500", cl.BaseURL().Port())
} }
func TestAcceptOverride(t *testing.T) {
name := "TestAcceptOverride"
orig := os.Getenv(envNameOverrideAccept)
defer func() {
os.Setenv(envNameOverrideAccept, orig)
}()
os.Setenv(envNameOverrideAccept, name)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, name, r.Header.Get("Accept"))
w.WriteHeader(200)
w.Write([]byte("ok"))
}))
defer srv.Close()
c, err := NewClient(srv.URL)
require.NoError(t, err)
b, err := c.Get(t.Context(), "/test")
require.NoError(t, err)
require.Equal(t, "ok", string(b))
}

View File

@@ -0,0 +1,2 @@
## Added
- env var to force beacon api client to use a custom accept header (eg force json responses).