Skip to content

Commit 65bde57

Browse files
Allow WithServerHostname to specify protocol (#153)
Updated WithServerHostname to allow prefixing the hostname with 'https:' or 'http:' to specify which protocol to use. Github issue #140 Signed-off-by: Raymond Cypher <[email protected]>
2 parents 45520d9 + 2b29d2e commit 65bde57

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

connector.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,37 @@ func withUserConfig(ucfg config.UserConfig) connOption {
109109
// WithServerHostname sets up the server hostname. Mandatory.
110110
func WithServerHostname(host string) connOption {
111111
return func(c *config.Config) {
112-
if host == "localhost" {
113-
c.Protocol = "http"
112+
protocol, hostname := parseHostName(host)
113+
if protocol != "" {
114+
c.Protocol = protocol
114115
}
115-
c.Host = host
116+
117+
c.Host = hostname
116118
}
117119
}
118120

121+
func parseHostName(host string) (protocol, hostname string) {
122+
hostname = host
123+
if strings.HasPrefix(host, "https") {
124+
hostname = strings.TrimPrefix(host, "https")
125+
protocol = "https"
126+
} else if strings.HasPrefix(host, "http") {
127+
hostname = strings.TrimPrefix(host, "http")
128+
protocol = "http"
129+
}
130+
131+
if protocol != "" {
132+
hostname = strings.TrimPrefix(hostname, ":")
133+
hostname = strings.TrimPrefix(hostname, "//")
134+
}
135+
136+
if hostname == "localhost" && protocol == "" {
137+
protocol = "http"
138+
}
139+
140+
return
141+
}
142+
119143
// WithPort sets up the server port. Mandatory.
120144
func WithPort(port int) connOption {
121145
return func(c *config.Config) {

connector_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,37 @@ func TestNewConnector(t *testing.T) {
130130
assert.Nil(t, err)
131131
assert.Equal(t, expectedCfg, coni.cfg)
132132
})
133+
134+
t.Run("Connector test WithServerHostname", func(t *testing.T) {
135+
cases := []struct {
136+
hostname, host, protocol string
137+
}{
138+
{"databricks-host", "databricks-host", "https"},
139+
{"http://databricks-host", "databricks-host", "http"},
140+
{"https://databricks-host", "databricks-host", "https"},
141+
{"http:databricks-host", "databricks-host", "http"},
142+
{"https:databricks-host", "databricks-host", "https"},
143+
{"htt://databricks-host", "htt://databricks-host", "https"},
144+
{"localhost", "localhost", "http"},
145+
{"http:localhost", "localhost", "http"},
146+
{"https:localhost", "localhost", "https"},
147+
}
148+
149+
for i := range cases {
150+
c := cases[i]
151+
con, err := NewConnector(
152+
WithServerHostname(c.hostname),
153+
)
154+
assert.Nil(t, err)
155+
156+
coni, ok := con.(*connector)
157+
require.True(t, ok)
158+
userConfig := coni.cfg.UserConfig
159+
require.Equal(t, c.protocol, userConfig.Protocol)
160+
require.Equal(t, c.host, userConfig.Host)
161+
}
162+
163+
})
133164
}
134165

135166
type mockRoundTripper struct{}

doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Use sql.OpenDB() to create a database handle via a new connector object created
7070
7171
Supported functional options include:
7272
73-
- WithServerHostname(<hostname> string): Sets up the server hostname. Mandatory
73+
- WithServerHostname(<hostname> string): Sets up the server hostname. The hostname can be prefixed with "http:" or "https:" to specify a protocol to use. Mandatory
7474
- WithPort(<port> int): Sets up the server port. Mandatory
7575
- WithAccessToken(<my_token> string): Sets up the Personal Access Token. Mandatory
7676
- WithHTTPPath(<http_path> string): Sets up the endpoint to the warehouse. Mandatory

0 commit comments

Comments
 (0)