From 393de5d26d8a16781fcfe3d4e9c9e3750167ba37 Mon Sep 17 00:00:00 2001 From: David vonThenen <12752197+dvonthenen@users.noreply.github.com> Date: Tue, 23 Jan 2024 06:58:05 -0800 Subject: [PATCH] Support Override Protocol for On-Prem --- examples/streaming/microphone/main.go | 20 +++++++++----- pkg/api/version/analyze-version.go | 38 ++++++++++++++++++++++---- pkg/api/version/constants.go | 8 ++++++ pkg/api/version/live-version.go | 38 ++++++++++++++++++++++---- pkg/api/version/manage-version.go | 37 ++++++++++++++++++++++--- pkg/api/version/prerecorded-version.go | 38 ++++++++++++++++++++++---- pkg/client/interfaces/utils.go | 2 +- 7 files changed, 154 insertions(+), 27 deletions(-) diff --git a/examples/streaming/microphone/main.go b/examples/streaming/microphone/main.go index f1212af4..3e8952b9 100644 --- a/examples/streaming/microphone/main.go +++ b/examples/streaming/microphone/main.go @@ -64,7 +64,7 @@ func main() { */ // init library client.Init(client.InitLib{ - LogLevel: client.LogLevelDebug, // LogLevelDefault, LogLevelFull, LogLevelDebug, LogLevelTrace + LogLevel: client.LogLevelDefault, // LogLevelDefault, LogLevelFull, LogLevelDebug, LogLevelTrace }) // Go context @@ -72,17 +72,23 @@ func main() { // set the Transcription options options := interfaces.LiveTranscriptionOptions{ - Model: "nova-2", - Language: "en-US", - Punctuate: true, - Encoding: "linear16", - Channels: 1, - SampleRate: 16000, + Model: "nova-2", + Language: "en-US", + Punctuate: true, + Encoding: "linear16", + Channels: 1, + SampleRate: 16000, + SmartFormat: true, // To get UtteranceEnd, the following must be set: // InterimResults: true, // UtteranceEndMs: "1000", } + // example on how to send a custom parameter + // params := make(map[string][]string, 0) + // params["dictation"] = []string{"true"} + // ctx = interfaces.WithCustomParameters(ctx, params) + // implement your own callback callback := MyCallback{} diff --git a/pkg/api/version/analyze-version.go b/pkg/api/version/analyze-version.go index c25534f9..d530fcc5 100644 --- a/pkg/api/version/analyze-version.go +++ b/pkg/api/version/analyze-version.go @@ -10,6 +10,7 @@ import ( "fmt" "net/url" "regexp" + "strings" "github.com/google/go-querystring/query" klog "k8s.io/klog/v2" @@ -42,22 +43,49 @@ func GetAnalyzeAPI(ctx context.Context, host, version, path string, options inte path = ReadPath } + // handle protocol and host + protocol := APIProtocol if host == "" { host = common.DefaultHost } + + r, err := regexp.Compile("^(https|http)://(.+)$") + if err != nil { + klog.V(1).Infof("regexp.Compile err: %v\n", err) + return "", err + } + + match := r.MatchString(host) + klog.V(3).Infof("host decompose... match: %t\n", match) + if match { + matches := r.FindStringSubmatch(host) + for _, match := range matches { + klog.V(5).Infof("match: %s\n", match) + } + protocol = matches[1] + host = matches[2] + + slash := strings.Index(host, "/") + if slash > 0 { + host = host[:slash] + } + } + klog.V(3).Infof("protocol: %s\n", protocol) + klog.V(3).Infof("host: %s\n", host) + + // handle version and path if version == "" { version = ReadAPIVersion } - r, err := regexp.Compile("^(v[0-9]+|%%s)/") + r, err = regexp.Compile("^(v[0-9]+|%%s)/") if err != nil { klog.V(1).Infof("regexp.Compile err: %v\n", err) return "", err } - match := r.MatchString(path) - klog.V(3).Infof("match: %t\n", match) - + match = r.MatchString(path) + klog.V(3).Infof("path decompose - match: %t\n", match) if match { // version = r.FindStringSubmatch(path)[0] path = r.ReplaceAllString(path, "") @@ -78,7 +106,7 @@ func GetAnalyzeAPI(ctx context.Context, host, version, path string, options inte fullpath := fmt.Sprintf("%%s/%s", path) completeFullpath := fmt.Sprintf(fullpath, append([]interface{}{version}, args...)...) - u := url.URL{Scheme: "https", Host: host, Path: completeFullpath, RawQuery: q.Encode()} + u := url.URL{Scheme: protocol, Host: host, Path: completeFullpath, RawQuery: q.Encode()} return u.String(), nil } diff --git a/pkg/api/version/constants.go b/pkg/api/version/constants.go index 83594fef..8e713657 100644 --- a/pkg/api/version/constants.go +++ b/pkg/api/version/constants.go @@ -6,6 +6,14 @@ package version import "errors" +const ( + // APIProtocol default protocol + APIProtocol string = "https" + + // WSProtocol default protocol + WSProtocol string = "wss" +) + var ( // ErrInvalidPath invalid path ErrInvalidPath = errors.New("invalid path") diff --git a/pkg/api/version/live-version.go b/pkg/api/version/live-version.go index a31179c7..43b0bc6d 100644 --- a/pkg/api/version/live-version.go +++ b/pkg/api/version/live-version.go @@ -10,6 +10,7 @@ import ( "fmt" "net/url" "regexp" + "strings" "github.com/google/go-querystring/query" klog "k8s.io/klog/v2" @@ -42,22 +43,49 @@ func GetLiveAPI(ctx context.Context, host, version, path string, options interfa path = LivePath } + // handle protocol and host + protocol := WSProtocol if host == "" { host = common.DefaultHost } + + r, err := regexp.Compile("^(wss|ws)://(.+)$") + if err != nil { + klog.V(1).Infof("regexp.Compile err: %v\n", err) + return "", err + } + + match := r.MatchString(host) + klog.V(3).Infof("host decompose... match: %t\n", match) + if match { + matches := r.FindStringSubmatch(host) + for _, match := range matches { + klog.V(5).Infof("match: %s\n", match) + } + protocol = matches[1] + host = matches[2] + + slash := strings.Index(host, "/") + if slash > 0 { + host = host[:slash] + } + } + klog.V(3).Infof("protocol: %s\n", protocol) + klog.V(3).Infof("host: %s\n", host) + + // handle version and path if version == "" { version = LiveAPIVersion } - r, err := regexp.Compile("^(v[0-9]+|%%s)/") + r, err = regexp.Compile("^(v[0-9]+|%%s)/") if err != nil { klog.V(1).Infof("regexp.Compile err: %v\n", err) return "", err } - match := r.MatchString(path) - klog.V(3).Infof("match: %t\n", match) - + match = r.MatchString(path) + klog.V(3).Infof("path decompose - match: %t\n", match) if match { // version = r.FindStringSubmatch(path)[0] path = r.ReplaceAllString(path, "") @@ -78,7 +106,7 @@ func GetLiveAPI(ctx context.Context, host, version, path string, options interfa fullpath := fmt.Sprintf("%%s/%s", path) completeFullpath := fmt.Sprintf(fullpath, append([]interface{}{version}, args...)...) - u := url.URL{Scheme: "wss", Host: host, Path: completeFullpath, RawQuery: q.Encode()} + u := url.URL{Scheme: protocol, Host: host, Path: completeFullpath, RawQuery: q.Encode()} return u.String(), nil } diff --git a/pkg/api/version/manage-version.go b/pkg/api/version/manage-version.go index 5f2e3fbb..84d3e50b 100644 --- a/pkg/api/version/manage-version.go +++ b/pkg/api/version/manage-version.go @@ -10,6 +10,7 @@ import ( "fmt" "net/url" "regexp" + "strings" klog "k8s.io/klog/v2" @@ -69,20 +70,48 @@ func GetManageAPI(ctx context.Context, host, version, path string, vals interfac return "", ErrInvalidPath } + // handle protocol and host + protocol := APIProtocol if host == "" { host = common.DefaultHost } + + r, err := regexp.Compile("^(https|http)://(.+)$") + if err != nil { + klog.V(1).Infof("regexp.Compile err: %v\n", err) + return "", err + } + + match := r.MatchString(host) + klog.V(3).Infof("host decompose... match: %t\n", match) + if match { + matches := r.FindStringSubmatch(host) + for _, match := range matches { + klog.V(5).Infof("match: %s\n", match) + } + protocol = matches[1] + host = matches[2] + + slash := strings.Index(host, "/") + if slash > 0 { + host = host[:slash] + } + } + klog.V(3).Infof("protocol: %s\n", protocol) + klog.V(3).Infof("host: %s\n", host) + + // handle version and path if version == "" { version = ManageAPIVersion } - r, err := regexp.Compile("^(v[0-9]+|%%s)/") + r, err = regexp.Compile("^(v[0-9]+|%%s)/") if err != nil { klog.V(1).Infof("regexp.Compile err: %v\n", err) return "", err } - match := r.MatchString(path) + match = r.MatchString(path) klog.V(3).Infof("match: %t\n", match) if match { @@ -114,9 +143,9 @@ func GetManageAPI(ctx context.Context, host, version, path string, vals interfac var u url.URL if q != nil { - u = url.URL{Scheme: "https", Host: host, Path: completeFullpath, RawQuery: q.Encode()} + u = url.URL{Scheme: protocol, Host: host, Path: completeFullpath, RawQuery: q.Encode()} } else { - u = url.URL{Scheme: "https", Host: host, Path: completeFullpath} + u = url.URL{Scheme: protocol, Host: host, Path: completeFullpath} } return u.String(), nil diff --git a/pkg/api/version/prerecorded-version.go b/pkg/api/version/prerecorded-version.go index 85f6dd09..1b7b81eb 100644 --- a/pkg/api/version/prerecorded-version.go +++ b/pkg/api/version/prerecorded-version.go @@ -10,6 +10,7 @@ import ( "fmt" "net/url" "regexp" + "strings" "github.com/google/go-querystring/query" klog "k8s.io/klog/v2" @@ -42,22 +43,49 @@ func GetPrerecordedAPI(ctx context.Context, host, version, path string, options path = PrerecordedPath } + // handle protocol and host + protocol := APIProtocol if host == "" { host = common.DefaultHost } + + r, err := regexp.Compile("^(wss|ws)://(.+)$") + if err != nil { + klog.V(1).Infof("regexp.Compile err: %v\n", err) + return "", err + } + + match := r.MatchString(host) + klog.V(3).Infof("host decompose... match: %t\n", match) + if match { + matches := r.FindStringSubmatch(host) + for _, match := range matches { + klog.V(5).Infof("match: %s\n", match) + } + protocol = matches[1] + host = matches[2] + + slash := strings.Index(host, "/") + if slash > 0 { + host = host[:slash] + } + } + klog.V(3).Infof("protocol: %s\n", protocol) + klog.V(3).Infof("host: %s\n", host) + + // handle version and path if version == "" { version = PrerecordedAPIVersion } - r, err := regexp.Compile("^(v[0-9]+|%%s)/") + r, err = regexp.Compile("^(v[0-9]+|%%s)/") if err != nil { klog.V(1).Infof("regexp.Compile err: %v\n", err) return "", err } - match := r.MatchString(path) - klog.V(3).Infof("match: %t\n", match) - + match = r.MatchString(path) + klog.V(3).Infof("path decompose - match: %t\n", match) if match { // version = r.FindStringSubmatch(path)[0] path = r.ReplaceAllString(path, "") @@ -78,7 +106,7 @@ func GetPrerecordedAPI(ctx context.Context, host, version, path string, options fullpath := fmt.Sprintf("%%s/%s", path) completeFullpath := fmt.Sprintf(fullpath, append([]interface{}{version}, args...)...) - u := url.URL{Scheme: "https", Host: host, Path: completeFullpath, RawQuery: q.Encode()} + u := url.URL{Scheme: protocol, Host: host, Path: completeFullpath, RawQuery: q.Encode()} return u.String(), nil } diff --git a/pkg/client/interfaces/utils.go b/pkg/client/interfaces/utils.go index 8f81eca3..980d0c9b 100644 --- a/pkg/client/interfaces/utils.go +++ b/pkg/client/interfaces/utils.go @@ -15,7 +15,7 @@ import ( // constants const ( - sdkVersion string = "v1.0.2" + sdkVersion string = "v1.1.0" ) // connection agent