-
Notifications
You must be signed in to change notification settings - Fork 28
/
client.go
213 lines (175 loc) · 6.01 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright 2023-2024 Deepgram SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT
/*
This package provides the prerecorded client implementation for the Deepgram API
*/
package prerecorded
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"os"
klog "k8s.io/klog/v2"
version "github.com/deepgram/deepgram-go-sdk/pkg/api/version"
common "github.com/deepgram/deepgram-go-sdk/pkg/client/common"
interfaces "github.com/deepgram/deepgram-go-sdk/pkg/client/interfaces"
)
type urlSource struct {
URL string `json:"url"`
}
/*
NewWithDefaults creates a new prerecorded client with all default options
Notes:
- The Deepgram API KEY is read from the environment variable DEEPGRAM_API_KEY
*/
func NewWithDefaults() *Client {
return New("", &interfaces.ClientOptions{})
}
/*
New creates a new prerecorded client with the specified options
Input parameters:
- ctx: context.Context object
- 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 {
if apiKey != "" {
options.APIKey = apiKey
}
err := options.Parse()
if err != nil {
klog.V(1).Infof("options.Parse() failed. Err: %v\n", err)
return nil
}
c := Client{
common.New(apiKey, options),
}
return &c
}
/*
DoFile posts a file capturing a conversation to a given REST endpoint
Input parameters:
- filePath: string containing the path to the file to be posted
- req: PreRecordedTranscriptionOptions which allows overriding things like language, etc.
Output parameters:
- resBody: interface{} which will be populated with the response from the server
*/
func (c *Client) DoFile(ctx context.Context, filePath string, req *interfaces.PreRecordedTranscriptionOptions, resBody interface{}) error {
klog.V(6).Infof("prerecorded.DoFile() ENTER\n")
// file?
fileInfo, err := os.Stat(filePath)
if err != nil || errors.Is(err, os.ErrNotExist) {
klog.V(1).Infof("File %s does not exist. Err : %v\n", filePath, err)
klog.V(6).Infof("prerecorded.DoFile() LEAVE\n")
return err
}
if fileInfo.IsDir() || fileInfo.Size() == 0 {
klog.V(1).Infof("%s is a directory not a file\n", filePath)
klog.V(6).Infof("prerecorded.DoFile() LEAVE\n")
return ErrInvalidInput
}
file, err := os.Open(filePath)
if err != nil {
klog.V(1).Infof("os.Open(%s) failed. Err : %v\n", filePath, err)
klog.V(6).Infof("prerecorded.DoFile() LEAVE\n")
return err
}
defer file.Close()
klog.V(6).Infof("prerecorded.DoFile() LEAVE\n")
return c.DoStream(ctx, file, req, resBody)
}
/*
DoStream posts a stream capturing a conversation to a given REST endpoint
Input parameters:
- src: io.Reader containing the stream to be posted
- req: PreRecordedTranscriptionOptions which allows overriding things like language, etc.
Output parameters:
- resBody: interface{} which will be populated with the response from the server
*/
// DoStream initiates a streaming API request.
func (c *Client) DoStream(ctx context.Context, src io.Reader, options *interfaces.PreRecordedTranscriptionOptions, resBody interface{}) error {
klog.V(6).Infof("prerecorded.DoStream() ENTER\n")
uri, err := version.GetPrerecordedAPI(ctx, c.Options.Host, c.Options.APIVersion, c.Options.Path, options)
if err != nil {
klog.V(1).Infof("GetPrerecordedAPI failed. Err: %v\n", err)
klog.V(6).Infof("prerecorded.DoStream() LEAVE\n")
return err
}
req, err := c.SetupRequest(ctx, "POST", uri, src)
if err != nil {
klog.V(1).Infof("SetupRequest failed. Err: %v\n", err)
klog.V(6).Infof("prerecorded.DoStream() LEAVE\n")
return err
}
err = c.HTTPClient.Do(ctx, req, func(res *http.Response) error {
_, err := c.HandleResponse(res, nil, resBody)
return err
})
if err != nil {
klog.V(1).Infof("HTTPClient.Do() failed. Err: %v\n", err)
} else {
klog.V(4).Infof("DoStream successful\n")
}
klog.V(6).Infof("prerecorded.DoStream() LEAVE\n")
return err
}
// IsURL returns true if a string is of a URL format
func IsURL(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}
/*
DoURL posts a URL capturing a conversation to a given REST endpoint
Input parameters:
- url: string containing the URL to be posted
- req: PreRecordedTranscriptionOptions which allows overriding things like language, etc.
Output parameters:
- resBody: interface{} which will be populated with the response from the server
*/
func (c *Client) DoURL(ctx context.Context, audioURL string, options *interfaces.PreRecordedTranscriptionOptions, resBody interface{}) error {
klog.V(6).Infof("prerecorded.DoURL() ENTER\n")
if !IsURL(audioURL) {
klog.V(1).Infof("Invalid URL: %s\n", audioURL)
klog.V(6).Infof("prerecorded.DoURL() LEAVE\n")
return ErrInvalidInput
}
uri, err := version.GetPrerecordedAPI(ctx, c.Options.Host, c.Options.APIVersion, c.Options.Path, options)
if err != nil {
klog.V(1).Infof("GetPrerecordedAPI failed. Err: %v\n", err)
klog.V(6).Infof("prerecorded.DoURL() LEAVE\n")
return err
}
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(urlSource{URL: audioURL}); err != nil {
klog.V(1).Infof("json.NewEncoder().Encode() failed. Err: %v\n", err)
klog.V(6).Infof("prerecorded.DoURL() LEAVE\n")
return err
}
req, err := c.SetupRequest(ctx, "POST", uri, &buf)
if err != nil {
klog.V(1).Infof("SetupRequest failed. Err: %v\n", err)
klog.V(6).Infof("prerecorded.DoURL() LEAVE\n")
return err
}
switch req.Method {
case http.MethodPost, http.MethodPatch, http.MethodPut:
klog.V(3).Infof("Content-Type = application/json\n")
req.Header.Set("Content-Type", "application/json")
}
err = c.HTTPClient.Do(ctx, req, func(res *http.Response) error {
_, err := c.HandleResponse(res, nil, resBody)
return err
})
if err != nil {
klog.V(1).Infof("HTTPClient.Do() failed. Err: %v\n", err)
} else {
klog.V(4).Infof("DoURL successful\n")
}
klog.V(6).Infof("prerecorded.DoURL() LEAVE\n")
return err
}