Skip to content

Commit

Permalink
chore(client): deprecate New constructor
Browse files Browse the repository at this point in the history
Signed-off-by: lvlcn-t <[email protected]>
  • Loading branch information
lvlcn-t committed Oct 29, 2024
1 parent 023867e commit 56f3da2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ type restClient struct {

// New creates a new rest client with the given base URL.
// You can optionally provide a timeout for requests. If no timeout is provided, the [DefaultTimeout] will be used.
//
// Deprecated: Use [NewWithClient] instead.
func New(baseURL string, timeout ...time.Duration) (Client, error) {
if len(timeout) == 0 {
return NewWithClient(baseURL, nil)
Expand Down Expand Up @@ -340,7 +342,7 @@ func WithErrorHandler(errorHandler, successHandler ResponseHandler) RequestOptio
// defaultClient creates a new [restClient] without a base URL.
// Panics if the client cannot be created.
func defaultClient() Client {
c, err := New("")
c, err := NewWithClient("", nil)
if err != nil {
panic(fmt.Errorf("failed to create default client: %w", err))
}
Expand Down
12 changes: 10 additions & 2 deletions rest/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,34 @@ func TestDefaultClient_Do(t *testing.T) {
}
}

func TestNewClient(t *testing.T) {
func TestNewWithClient(t *testing.T) {
tests := []struct {
name string
baseURL string
client *http.Client
wantErr bool
}{
{
name: "valid base URL",
baseURL: "https://example.com",
client: nil,
},
{
name: "invalid base URL",
baseURL: "://:?https://example.com",
client: nil,
wantErr: true,
},
{
name: "custom client",
baseURL: "https://example.com",
client: http.DefaultClient,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := New(tt.baseURL, 5*time.Second)
_, err := NewWithClient(tt.baseURL, tt.client)
if (err != nil) != tt.wantErr {
t.Errorf("NewClient() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down

0 comments on commit 56f3da2

Please sign in to comment.