Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Overriding Protocol for On-Prem #169

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions examples/streaming/microphone/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,31 @@ func main() {
*/
// init library
client.Init(client.InitLib{
LogLevel: client.LogLevelDebug, // LogLevelDefault, LogLevelFull, LogLevelDebug, LogLevelTrace
LogLevel: client.LogLevelDefault, // LogLevelDefault, LogLevelFull, LogLevelDebug, LogLevelTrace
})

// Go context
ctx := context.Background()

// 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{}

Expand Down
38 changes: 33 additions & 5 deletions pkg/api/version/analyze-version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"net/url"
"regexp"
"strings"

"github.com/google/go-querystring/query"
klog "k8s.io/klog/v2"
Expand Down Expand Up @@ -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, "")
Expand All @@ -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
}
8 changes: 8 additions & 0 deletions pkg/api/version/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
38 changes: 33 additions & 5 deletions pkg/api/version/live-version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"net/url"
"regexp"
"strings"

"github.com/google/go-querystring/query"
klog "k8s.io/klog/v2"
Expand Down Expand Up @@ -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, "")
Expand All @@ -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
}
37 changes: 33 additions & 4 deletions pkg/api/version/manage-version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"net/url"
"regexp"
"strings"

klog "k8s.io/klog/v2"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
38 changes: 33 additions & 5 deletions pkg/api/version/prerecorded-version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"net/url"
"regexp"
"strings"

"github.com/google/go-querystring/query"
klog "k8s.io/klog/v2"
Expand Down Expand Up @@ -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, "")
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion pkg/client/interfaces/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// constants
const (
sdkVersion string = "v1.0.2"
sdkVersion string = "v1.1.0"
)

// connection agent
Expand Down
Loading