Skip to content

Commit

Permalink
Merge pull request #1 from kakkoyun/config_client
Browse files Browse the repository at this point in the history
Add api.Config validation to prevent confusion
  • Loading branch information
yolossn committed Apr 21, 2022
2 parents b5f1e9e + 33ac472 commit 3087288
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion api/client.go
Expand Up @@ -17,6 +17,7 @@ package api
import (
"bytes"
"context"
"errors"
"net"
"net/http"
"net/url"
Expand All @@ -41,7 +42,7 @@ type Config struct {
Address string

// Client is used by the Client to drive HTTP requests. If not provided,
// a new one is created.
// a new one based on the provided RoundTripper (or DefaultRoundTripper) will be used.
Client *http.Client

// RoundTripper is used by the Client to drive HTTP requests. If not
Expand All @@ -65,6 +66,13 @@ func (cfg *Config) client() http.Client {
return *cfg.Client
}

func (cfg *Config) validate() error {
if cfg.Client != nil && cfg.RoundTripper != nil {
return errors.New("api.Config.RoundTripper and api.Config.Client are mutually exclusive")
}
return nil
}

// Client is the interface for an API client.
type Client interface {
URL(ep string, args map[string]string) *url.URL
Expand All @@ -81,6 +89,10 @@ func NewClient(cfg Config) (Client, error) {
}
u.Path = strings.TrimRight(u.Path, "/")

if err := cfg.validate(); err != nil {
return nil, err
}

return &httpClient{
endpoint: u,
client: cfg.client(),
Expand Down

0 comments on commit 3087288

Please sign in to comment.