databricks-sdk-golang/databricks.go

88 lines
2.1 KiB
Go
Raw Permalink Normal View History

2019-05-24 06:12:50 +00:00
package databricks
2019-05-27 21:29:27 +00:00
import (
2019-05-28 03:47:17 +00:00
"crypto/tls"
"encoding/base64"
"fmt"
"net/http"
"net/url"
"time"
2019-05-27 21:29:27 +00:00
)
2019-05-28 03:47:17 +00:00
// DBClientOption is used to configure the DataBricks Client
type DBClientOption struct {
User string
Password string
Host string
Token string
DefaultHeaders map[string]string
InsecureSkipVerify bool
TimeoutSeconds int
2020-01-09 10:12:41 +00:00
client http.Client
2019-05-27 21:29:27 +00:00
}
2020-01-09 10:12:41 +00:00
// Init initializes the client
func (o *DBClientOption) Init() {
2019-05-28 03:47:17 +00:00
if o.TimeoutSeconds == 0 {
o.TimeoutSeconds = 10
}
2020-01-09 10:12:41 +00:00
o.client = http.Client{
2019-05-28 03:47:17 +00:00
Timeout: time.Duration(time.Duration(o.TimeoutSeconds) * time.Second),
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: o.InsecureSkipVerify,
},
},
}
2020-01-09 10:12:41 +00:00
}
func (o *DBClientOption) getHTTPClient() http.Client {
return o.client
2019-05-27 21:29:27 +00:00
}
2019-05-28 03:47:17 +00:00
func (o *DBClientOption) getAuthHeader() map[string]string {
auth := make(map[string]string)
if o.User != "" && o.Password != "" {
encodedAuth := []byte(o.User + ":" + o.Password)
userHeaderData := "Basic " + base64.StdEncoding.EncodeToString(encodedAuth)
auth["Authorization"] = userHeaderData
2019-05-28 04:23:24 +00:00
auth["Content-Type"] = "application/json"
2019-05-28 03:47:17 +00:00
} else if o.Token != "" {
auth["Authorization"] = "Bearer " + o.Token
2019-05-28 04:23:24 +00:00
auth["Content-Type"] = "application/json"
2019-05-28 03:47:17 +00:00
}
return auth
2019-05-27 21:29:27 +00:00
}
2019-05-28 03:47:17 +00:00
func (o *DBClientOption) getUserAgentHeader() map[string]string {
return map[string]string{
2019-05-28 04:23:24 +00:00
"User-Agent": fmt.Sprintf("databricks-sdk-golang-%s", SdkVersion),
2019-05-28 03:47:17 +00:00
}
2019-05-24 06:12:50 +00:00
}
2019-05-28 03:47:17 +00:00
func (o *DBClientOption) getDefaultHeaders() map[string]string {
auth := o.getAuthHeader()
userAgent := o.getUserAgentHeader()
defaultHeaders := make(map[string]string)
for k, v := range auth {
defaultHeaders[k] = v
}
for k, v := range o.DefaultHeaders {
defaultHeaders[k] = v
}
for k, v := range userAgent {
defaultHeaders[k] = v
}
return defaultHeaders
}
func (o *DBClientOption) getRequestURI(path string) (string, error) {
parsedURI, err := url.Parse(o.Host)
if err != nil {
return "", err
}
requestURI := fmt.Sprintf("%s://%s/api/%s%s", parsedURI.Scheme, parsedURI.Host, APIVersion, path)
return requestURI, nil
2019-05-28 04:23:24 +00:00
}