Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add mockable http client to simple client #209

Merged
merged 3 commits into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions client/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import (

var (
// 503 error type informing rollup to failover to other DA location
ErrServiceUnavailable = fmt.Errorf("eigenda service is unavailable")
ErrServiceUnavailable = fmt.Errorf("eigenda service is temporarily unavailable")
)

// TODO: Add support for custom http client option
type Config struct {
URL string
URL string // EigenDA proxy REST API URL
}

type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
Comment on lines +20 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really needed here? How are you mocking the client? with some mock library?
Can we use https://pkg.go.dev/net/http/httptest#Server.Client instead to simplify the code and not have this interface (don't think its typical to have an interface for an http client in golang, but not 100% sure)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its easiest for me to just bind it to an interface for mocking - done this across quite a few projects in the past

}

// SimpleCommitmentClient implements a simple client for the eigenda-proxy
Expand All @@ -25,14 +28,30 @@ type Config struct {
// so clients wanting to send op commitment mode data should use that client.
type SimpleCommitmentClient struct {
cfg *Config
httpClient *http.Client
httpClient HTTPClient
}

func New(cfg *Config) *SimpleCommitmentClient {
return &SimpleCommitmentClient{
type SimpleClientOption func(scc *SimpleCommitmentClient)

// WithHTTPClient ... Embeds custom http client type
func WithHTTPClient(client HTTPClient) SimpleClientOption {
return func(scc *SimpleCommitmentClient) {
scc.httpClient = client
}
}

// New ... Constructor
func New(cfg *Config, opts ...SimpleClientOption) *SimpleCommitmentClient {
scc := &SimpleCommitmentClient{
cfg,
http.DefaultClient,
}

for _, opt := range opts {
opt(scc)
}

return scc
}

// Health indicates if the server is operational; useful for event based awaits
Expand Down Expand Up @@ -80,7 +99,7 @@ func (c *SimpleCommitmentClient) GetData(ctx context.Context, comm []byte) ([]by
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("received error response, code=%d, msg = %s", resp.StatusCode, string(b))
return nil, fmt.Errorf("received error response when reading from eigenda-proxy, code=%d, msg = %s", resp.StatusCode, string(b))
}

return b, nil
Expand All @@ -106,16 +125,17 @@ func (c *SimpleCommitmentClient) SetData(ctx context.Context, b []byte) ([]byte,
return nil, err
}

// failover signal
if resp.StatusCode == http.StatusServiceUnavailable {
return nil, ErrServiceUnavailable
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to store data: %v, err = %s", resp.StatusCode, string(b))
return nil, fmt.Errorf("received error response when dispersing to eigenda-proxy, code=%d, err = %s", resp.StatusCode, string(b))
}

if len(b) == 0 {
return nil, fmt.Errorf("read certificate is empty")
return nil, fmt.Errorf("received an empty certificate")
}

return b, err
Expand Down
Loading