Skip to content

Commit 9210973

Browse files
Add base framework for functional configuration of Client. (#5)
## What changes are proposed in this pull request? This PR adds the base framework for functional configuration of modular API Clients (either REST or gRPC). The principle behind that framework is to build a strong abstraction between the configuration surface (i.e. the functional options) and the actual config (which is internal). This allows to easily evolve and extend the configuration surface without introducing breaking changes. **Why functional options and not method on a struct with private members?** It is more idiomatic and modular while ensuring that each clients has its own immutable config. ## How is this tested? No test yet, this PR is purely meant as a "demo" to be extended in an actual implementation (e.g. by implementing a specific roundtripper for the HTTP client).
1 parent ad5da7d commit 9210973

3 files changed

Lines changed: 91 additions & 0 deletions

File tree

‎databricks/internal/internal.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package internal
2+
3+
import (
4+
"net/http"
5+
"time"
6+
7+
"github.com/databricks/sdk-go/auth"
8+
"github.com/databricks/sdk-go/core/log"
9+
)
10+
11+
type ClientOptions struct {
12+
HTTPClient *http.Client
13+
Credentials auth.Credentials
14+
Timeout time.Duration
15+
Logger log.Logger
16+
Debug bool
17+
}

‎databricks/options/options.go‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package options
2+
3+
import (
4+
"net/http"
5+
"time"
6+
7+
"github.com/databricks/sdk-go/auth"
8+
"github.com/databricks/sdk-go/core/log"
9+
"github.com/databricks/sdk-go/databricks/internal"
10+
)
11+
12+
// ClientOption is an option to configure Databricks API clients.
13+
type ClientOption func(*internal.ClientOptions) error
14+
15+
// WithHTTPClient returns a ClientOption to use a specific HTTP Client when
16+
// making HTTP requests.
17+
func WithHTTPClient(c *http.Client) ClientOption {
18+
return func(o *internal.ClientOptions) error {
19+
o.HTTPClient = c
20+
return nil
21+
}
22+
}
23+
24+
// WithCredentials returns a ClientOption to set a specific credentials.
25+
func WithCredentials(c auth.Credentials) ClientOption {
26+
return func(o *internal.ClientOptions) error {
27+
o.Credentials = c
28+
return nil
29+
}
30+
}
31+
32+
// WithTimeout returns a ClientOption to set the overall API call timeout to
33+
// the given duration by default.
34+
func WithTimeout(d time.Duration) ClientOption {
35+
return func(o *internal.ClientOptions) error {
36+
o.Timeout = d
37+
return nil
38+
}
39+
}
40+
41+
// WithLogger returns a ClientOption to use the provided logger instead of
42+
// the default logger.
43+
func WithLogger(l log.Logger) ClientOption {
44+
return func(o *internal.ClientOptions) error {
45+
o.Logger = l
46+
return nil
47+
}
48+
}

‎databricks/transport/http.go‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package transport
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"github.com/databricks/sdk-go/databricks/internal"
8+
"github.com/databricks/sdk-go/databricks/options"
9+
)
10+
11+
func NewHTTPClient(ctx context.Context, opts ...options.ClientOption) (*http.Client, error) {
12+
copts := &internal.ClientOptions{}
13+
for _, opt := range opts {
14+
if err := opt(copts); err != nil {
15+
return nil, err
16+
}
17+
}
18+
19+
if copts.HTTPClient == nil {
20+
copts.HTTPClient = http.DefaultClient
21+
}
22+
23+
// TODO: Actually use the options...
24+
25+
return copts.HTTPClient, nil
26+
}

0 commit comments

Comments
 (0)