diff --git a/examples/prerecorded/file/main.go b/examples/prerecorded/file/main.go index f471768c..6f2c80ac 100644 --- a/examples/prerecorded/file/main.go +++ b/examples/prerecorded/file/main.go @@ -39,7 +39,9 @@ func main() { } // create a Deepgram client - c := client.NewWithDefaults() + c := client.New("", interfaces.ClientOptions{ + Host: "https://api.deepgram.com", + }) dg := prerecorded.New(c) // example on how to send a custom header diff --git a/examples/prerecorded/intent/main.go b/examples/prerecorded/intent/main.go index 7c09e7fb..818a915b 100644 --- a/examples/prerecorded/intent/main.go +++ b/examples/prerecorded/intent/main.go @@ -24,7 +24,7 @@ const ( func main() { // init library client.Init(client.InitLib{ - LogLevel: client.LogLevelStandard, // LogLevelStandard / LogLevelFull / LogLevelTrace + LogLevel: client.LogLevelDefault, // LogLevelStandard / LogLevelFull / LogLevelTrace }) // Go context diff --git a/examples/prerecorded/sentiment/main.go b/examples/prerecorded/sentiment/main.go index 1e11a209..8a52b14f 100644 --- a/examples/prerecorded/sentiment/main.go +++ b/examples/prerecorded/sentiment/main.go @@ -24,7 +24,7 @@ const ( func main() { // init library client.Init(client.InitLib{ - LogLevel: client.LogLevelStandard, // LogLevelStandard / LogLevelFull / LogLevelTrace + LogLevel: client.LogLevelDefault, // LogLevelStandard / LogLevelFull / LogLevelTrace }) // Go context diff --git a/examples/prerecorded/stream/main.go b/examples/prerecorded/stream/main.go index 6751624f..65224753 100644 --- a/examples/prerecorded/stream/main.go +++ b/examples/prerecorded/stream/main.go @@ -24,7 +24,7 @@ const ( func main() { // init library client.Init(client.InitLib{ - LogLevel: client.LogLevelFull, + LogLevel: client.LogLevelDefault, // LogLevelStandard / LogLevelFull / LogLevelTrace }) // context diff --git a/examples/prerecorded/summary/main.go b/examples/prerecorded/summary/main.go index 2286f964..6f58fb61 100644 --- a/examples/prerecorded/summary/main.go +++ b/examples/prerecorded/summary/main.go @@ -24,7 +24,7 @@ const ( func main() { // init library client.Init(client.InitLib{ - LogLevel: client.LogLevelStandard, // LogLevelStandard / LogLevelFull / LogLevelTrace + LogLevel: client.LogLevelDefault, // LogLevelStandard / LogLevelFull / LogLevelTrace }) // Go context diff --git a/examples/prerecorded/topic/main.go b/examples/prerecorded/topic/main.go index 269ffa78..387c93e3 100644 --- a/examples/prerecorded/topic/main.go +++ b/examples/prerecorded/topic/main.go @@ -24,7 +24,7 @@ const ( func main() { // init library client.Init(client.InitLib{ - LogLevel: client.LogLevelStandard, // LogLevelStandard / LogLevelFull / LogLevelTrace + LogLevel: client.LogLevelDefault, // LogLevelStandard / LogLevelFull / LogLevelTrace }) // Go context diff --git a/examples/streaming/microphone/main.go b/examples/streaming/microphone/main.go index 3e8952b9..d419a531 100644 --- a/examples/streaming/microphone/main.go +++ b/examples/streaming/microphone/main.go @@ -70,8 +70,13 @@ func main() { // Go context ctx := context.Background() + // client options + cOptions := interfaces.ClientOptions{ + EnableKeepAlive: true, + } + // set the Transcription options - options := interfaces.LiveTranscriptionOptions{ + tOptions := interfaces.LiveTranscriptionOptions{ Model: "nova-2", Language: "en-US", Punctuate: true, @@ -93,7 +98,7 @@ func main() { callback := MyCallback{} // create a Deepgram client - dgClient, err := client.NewWithDefaults(ctx, options, callback) + dgClient, err := client.New(ctx, "", cOptions, tOptions, callback) if err != nil { fmt.Println("ERROR creating LiveTranscription connection:", err) return diff --git a/pkg/api/version/prerecorded-version.go b/pkg/api/version/prerecorded-version.go index 1b7b81eb..5c1872fd 100644 --- a/pkg/api/version/prerecorded-version.go +++ b/pkg/api/version/prerecorded-version.go @@ -49,7 +49,7 @@ func GetPrerecordedAPI(ctx context.Context, host, version, path string, options host = common.DefaultHost } - r, err := regexp.Compile("^(wss|ws)://(.+)$") + r, err := regexp.Compile("^(https|http)://(.+)$") if err != nil { klog.V(1).Infof("regexp.Compile err: %v\n", err) return "", err diff --git a/pkg/client/live/client.go b/pkg/client/live/client.go index 03b27c25..cc7b4d3c 100644 --- a/pkg/client/live/client.go +++ b/pkg/client/live/client.go @@ -31,7 +31,7 @@ Notes: - The Deepgram API KEY is read from the environment variable DEEPGRAM_API_KEY */ func NewForDemo(ctx context.Context, options interfaces.LiveTranscriptionOptions) (*Client, error) { - return New(ctx, "", &interfaces.ClientOptions{}, options, nil) + return New(ctx, "", interfaces.ClientOptions{}, options, nil) } /* @@ -42,7 +42,7 @@ Notes: - The callback handler is set to the default handler which just prints all messages to the console */ func NewWithDefaults(ctx context.Context, options interfaces.LiveTranscriptionOptions, callback msginterfaces.LiveMessageCallback) (*Client, error) { - return New(ctx, "", &interfaces.ClientOptions{}, options, callback) + return New(ctx, "", interfaces.ClientOptions{}, options, callback) } /* @@ -55,7 +55,7 @@ Input parameters: - tOptions: LiveTranscriptionOptions which allows overriding things like language, model, etc. - callback: LiveMessageCallback which is a callback that allows you to perform actions based on the transcription */ -func New(ctx context.Context, apiKey string, cOptions *interfaces.ClientOptions, tOptions interfaces.LiveTranscriptionOptions, callback msginterfaces.LiveMessageCallback) (*Client, error) { +func New(ctx context.Context, apiKey string, cOptions interfaces.ClientOptions, tOptions interfaces.LiveTranscriptionOptions, callback msginterfaces.LiveMessageCallback) (*Client, error) { klog.V(6).Infof("live.New() ENTER\n") if apiKey != "" { diff --git a/pkg/client/live/types.go b/pkg/client/live/types.go index ea78c322..12b4dff2 100644 --- a/pkg/client/live/types.go +++ b/pkg/client/live/types.go @@ -17,7 +17,7 @@ import ( // Client is a struct representing the websocket client connection type Client struct { - cOptions *interfaces.ClientOptions + cOptions interfaces.ClientOptions tOptions interfaces.LiveTranscriptionOptions sendBuf chan []byte diff --git a/pkg/client/prerecorded/client.go b/pkg/client/prerecorded/client.go index daf4b4e4..1196e50c 100644 --- a/pkg/client/prerecorded/client.go +++ b/pkg/client/prerecorded/client.go @@ -17,6 +17,7 @@ import ( "net/http" "net/url" "os" + "strings" klog "k8s.io/klog/v2" @@ -36,7 +37,7 @@ Notes: - The Deepgram API KEY is read from the environment variable DEEPGRAM_API_KEY */ func NewWithDefaults() *Client { - return New("", &interfaces.ClientOptions{}) + return New("", interfaces.ClientOptions{}) } /* @@ -47,7 +48,7 @@ Input parameters: - apiKey: string containing the Deepgram API key - options: ClientOptions which allows overriding things like hostname, version of the API, etc. */ -func New(apiKey string, options *interfaces.ClientOptions) *Client { +func New(apiKey string, options interfaces.ClientOptions) *Client { if apiKey != "" { options.ApiKey = apiKey } @@ -183,7 +184,14 @@ func (c *Client) DoStream(ctx context.Context, src io.Reader, options interfaces _, err := io.Copy(b, res.Body) return err default: - d := json.NewDecoder(res.Body) + resultStr, errRead := io.ReadAll(res.Body) + if errRead != nil { + klog.V(1).Infof("io.ReadAll failed. Err: %v\n", errRead) + klog.V(6).Infof("prerecorded.DoStream() LEAVE\n") + return errRead + } + klog.V(5).Infof("json.NewDecoder Raw:\n\n%s\n\n", resultStr) + d := json.NewDecoder(strings.NewReader(string(resultStr))) klog.V(3).Infof("json.NewDecoder\n") klog.V(6).Infof("prerecorded.DoStream() LEAVE\n") return d.Decode(resBody) @@ -309,7 +317,14 @@ func (c *Client) DoURL(ctx context.Context, url string, options interfaces.PreRe _, err := io.Copy(b, res.Body) return err default: - d := json.NewDecoder(res.Body) + resultStr, errRead := io.ReadAll(res.Body) + if errRead != nil { + klog.V(1).Infof("io.ReadAll failed. Err: %v\n", errRead) + klog.V(6).Infof("prerecorded.DoURL() LEAVE\n") + return errRead + } + klog.V(5).Infof("json.NewDecoder Raw:\n\n%s\n\n", resultStr) + d := json.NewDecoder(strings.NewReader(string(resultStr))) klog.V(3).Infof("json.NewDecoder\n") klog.V(6).Infof("prerecorded.DoURL() LEAVE\n") return d.Decode(resBody) @@ -395,7 +410,14 @@ func (c *Client) Do(ctx context.Context, req *http.Request, resBody interface{}) _, err := io.Copy(b, res.Body) return err default: - d := json.NewDecoder(res.Body) + resultStr, errRead := io.ReadAll(res.Body) + if errRead != nil { + klog.V(1).Infof("io.ReadAll failed. Err: %v\n", errRead) + klog.V(6).Infof("prerecorded.Do() LEAVE\n") + return errRead + } + klog.V(5).Infof("json.NewDecoder Raw:\n\n%s\n\n", resultStr) + d := json.NewDecoder(strings.NewReader(string(resultStr))) klog.V(3).Infof("json.NewDecoder\n") klog.V(6).Infof("prerecorded.Do() LEAVE\n") return d.Decode(resBody) diff --git a/pkg/client/prerecorded/types.go b/pkg/client/prerecorded/types.go index 7e8557fa..cc51b574 100644 --- a/pkg/client/prerecorded/types.go +++ b/pkg/client/prerecorded/types.go @@ -13,5 +13,5 @@ import ( type Client struct { *rest.Client - cOptions *interfaces.ClientOptions + cOptions interfaces.ClientOptions } diff --git a/pkg/client/rest/http.go b/pkg/client/rest/http.go index 542bb6fe..2ef2567e 100644 --- a/pkg/client/rest/http.go +++ b/pkg/client/rest/http.go @@ -15,7 +15,7 @@ import ( ) // New allocated a Simple HTTP client -func NewHTTPClient(options *interfaces.ClientOptions) *HttpClient { +func NewHTTPClient(options interfaces.ClientOptions) *HttpClient { tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: options.SkipServerAuth}, } diff --git a/pkg/client/rest/rest.go b/pkg/client/rest/rest.go index 4582b95f..ff544435 100644 --- a/pkg/client/rest/rest.go +++ b/pkg/client/rest/rest.go @@ -23,11 +23,11 @@ import ( // NewWithDefaults creates a REST client with default options func NewWithDefaults() *Client { - return New(&interfaces.ClientOptions{}) + return New(interfaces.ClientOptions{}) } // New REST client -func New(options *interfaces.ClientOptions) *Client { +func New(options interfaces.ClientOptions) *Client { err := options.Parse() if err != nil { klog.V(1).Infof("options.Parse failed. Err: %v\n", err) diff --git a/pkg/client/rest/types.go b/pkg/client/rest/types.go index 8005aca9..a3fcd464 100644 --- a/pkg/client/rest/types.go +++ b/pkg/client/rest/types.go @@ -17,12 +17,12 @@ type HttpClient struct { d *debugContainer UserAgent string - options *interfaces.ClientOptions + options interfaces.ClientOptions } // Client which extends HttpClient to support REST type Client struct { *HttpClient - Options *interfaces.ClientOptions + Options interfaces.ClientOptions } diff --git a/tests/prerecorded_test.go b/tests/prerecorded_test.go index 454c71bd..b960c3b6 100644 --- a/tests/prerecorded_test.go +++ b/tests/prerecorded_test.go @@ -123,7 +123,7 @@ func TestPrerecordedFromURL(t *testing.T) { httpmock.RegisterResponder("POST", betaEndPoint, preRecordedFromURLHandler) t.Run("Test Basic PreRecordedFromURL", func(t *testing.T) { - c := client.New(MockAPIKey, &interfaces.ClientOptions{}) + c := client.New(MockAPIKey, interfaces.ClientOptions{}) httpmock.ActivateNonDefault(&c.Client.HttpClient.Client) dg := prerecorded.New(c) _, err := dg.FromURL( @@ -137,7 +137,7 @@ func TestPrerecordedFromURL(t *testing.T) { }) t.Run("Test PreRecordedFromURL with summarize v1", func(t *testing.T) { - c := client.New(MockAPIKey, &interfaces.ClientOptions{}) + c := client.New(MockAPIKey, interfaces.ClientOptions{}) httpmock.ActivateNonDefault(&c.Client.HttpClient.Client) dg := prerecorded.New(c) _, err := dg.FromURL( @@ -153,7 +153,7 @@ func TestPrerecordedFromURL(t *testing.T) { }) t.Run("Test PreRecordedFromURL with summarize v2", func(t *testing.T) { - c := client.New(MockAPIKey, &interfaces.ClientOptions{}) + c := client.New(MockAPIKey, interfaces.ClientOptions{}) httpmock.ActivateNonDefault(&c.Client.HttpClient.Client) dg := prerecorded.New(c) _, err := dg.FromURL(