-
Notifications
You must be signed in to change notification settings - Fork 28
/
client.go
343 lines (297 loc) · 10 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright 2023 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"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
klog "k8s.io/klog/v2"
version "github.com/deepgram/deepgram-go-sdk/pkg/api/version"
interfaces "github.com/deepgram/deepgram-go-sdk/pkg/client/interfaces"
rest "github.com/deepgram/deepgram-go-sdk/pkg/client/rest"
)
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{
Client: rest.New(options),
cOptions: 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
*/
func (c *Client) DoStream(ctx context.Context, src io.Reader, options interfaces.PreRecordedTranscriptionOptions, resBody interface{}) error {
klog.V(6).Infof("prerecorded.DoStream() ENTER\n")
// obtain URL for the REST API call
URI, err := version.GetPrerecordedAPI(ctx, c.cOptions.Host, c.cOptions.ApiVersion, c.cOptions.Path, options)
if err != nil {
klog.V(1).Infof("version.GetPrerecordedAPI failed. Err: %v\n", err)
klog.V(6).Infof("prerecorded.DoStream() LEAVE\n")
return err
}
req, err := http.NewRequestWithContext(ctx, "POST", URI, src)
if err != nil {
klog.V(1).Infof("http.NewRequestWithContext failed. Err: %v\n", err)
klog.V(6).Infof("prerecorded.DoStream() LEAVE\n")
return err
}
klog.V(4).Infof("%s %s\n", req.Method, URI)
if headers, ok := ctx.Value(interfaces.HeadersContext{}).(http.Header); ok {
for k, v := range headers {
for _, v := range v {
klog.V(3).Infof("Custom Header: %s = %s\n", k, v)
req.Header.Add(k, v)
}
}
}
req.Header.Set("Host", c.cOptions.Host)
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "token "+c.cOptions.ApiKey)
req.Header.Set("User-Agent", interfaces.DgAgent)
err = c.HttpClient.Do(ctx, req, func(res *http.Response) error {
switch res.StatusCode {
case http.StatusOK:
case http.StatusCreated:
case http.StatusNoContent:
case http.StatusBadRequest:
klog.V(4).Infof("HTTP Error Code: %d\n", res.StatusCode)
detail, errBody := io.ReadAll(res.Body)
if err != nil {
klog.V(4).Infof("io.ReadAll failed. Err: %e\n", errBody)
klog.V(6).Infof("prerecorded.DoStream() LEAVE\n")
return &interfaces.StatusError{res}
}
klog.V(6).Infof("prerecorded.DoStream() LEAVE\n")
return fmt.Errorf("%s: %s", res.Status, bytes.TrimSpace(detail))
default:
return &interfaces.StatusError{res}
}
if resBody == nil {
klog.V(1).Infof("resBody == nil\n")
klog.V(6).Infof("prerecorded.DoStream() LEAVE\n")
return nil
}
switch b := resBody.(type) {
case *interfaces.RawResponse:
klog.V(3).Infof("RawResponse\n")
klog.V(6).Infof("prerecorded.DoStream() LEAVE\n")
return res.Write(b)
case io.Writer:
klog.V(3).Infof("io.Writer\n")
klog.V(6).Infof("prerecorded.DoStream() LEAVE\n")
_, err := io.Copy(b, res.Body)
return err
default:
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)
}
})
if err != nil {
klog.V(1).Infof("err = c.Client.Do failed. Err: %v\n", err)
klog.V(6).Infof("prerecorded.DoStream() LEAVE\n")
return err
}
klog.V(3).Infof("prerecorded.DoStream() Succeeded\n")
klog.V(6).Infof("prerecorded.DoStream() LEAVE\n")
return nil
}
// 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, url string, options interfaces.PreRecordedTranscriptionOptions, resBody interface{}) error {
klog.V(6).Infof("prerecorded.DoURL() ENTER\n")
klog.V(4).Infof("apiURI: %s\n", url)
// checks
validURL := IsUrl(url)
if !validURL {
klog.V(1).Infof("Invalid URL: %s\n", url)
klog.V(6).Infof("prerecorded.DoURL() LEAVE\n")
return ErrInvalidInput
}
// obtain URL
URI, err := version.GetPrerecordedAPI(ctx, c.cOptions.Host, c.cOptions.ApiVersion, c.cOptions.Path, options)
if err != nil {
klog.V(1).Infof("version.GetPrerecordedAPI failed. Err: %v\n", err)
klog.V(6).Infof("prerecorded.DoURL() LEAVE\n")
return err
}
var buf bytes.Buffer
err = json.NewEncoder(&buf).Encode(urlSource{Url: url})
if 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 := http.NewRequestWithContext(ctx, "POST", URI, &buf)
if err != nil {
klog.V(1).Infof("http.NewRequestWithContext failed. Err: %v\n", err)
klog.V(6).Infof("prerecorded.DoURL() LEAVE\n")
return err
}
klog.V(4).Infof("%s %s\n", req.Method, URI)
if headers, ok := ctx.Value(interfaces.HeadersContext{}).(http.Header); ok {
for k, v := range headers {
for _, v := range v {
klog.V(3).Infof("Custom Header: %s = %s\n", k, v)
req.Header.Add(k, v)
}
}
}
req.Header.Set("Host", c.cOptions.Host)
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "token "+c.cOptions.ApiKey)
req.Header.Set("User-Agent", interfaces.DgAgent)
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 {
switch res.StatusCode {
case http.StatusOK:
case http.StatusCreated:
case http.StatusNoContent:
case http.StatusBadRequest:
klog.V(4).Infof("HTTP Error Code: %d\n", res.StatusCode)
detail, errBody := io.ReadAll(res.Body)
if err != nil {
klog.V(1).Infof("io.ReadAll failed. Err: %e\n", errBody)
klog.V(6).Infof("prerecorded.DoURL() LEAVE\n")
return &interfaces.StatusError{res}
}
klog.V(6).Infof("prerecorded.DoURL() LEAVE\n")
return fmt.Errorf("%s: %s", res.Status, bytes.TrimSpace(detail))
default:
return &interfaces.StatusError{res}
}
if resBody == nil {
klog.V(1).Infof("resBody == nil\n")
klog.V(6).Infof("prerecorded.DoURL() LEAVE\n")
return nil
}
switch b := resBody.(type) {
case *interfaces.RawResponse:
klog.V(3).Infof("RawResponse\n")
klog.V(6).Infof("prerecorded.DoURL() LEAVE\n")
return res.Write(b)
case io.Writer:
klog.V(3).Infof("io.Writer\n")
klog.V(6).Infof("prerecorded.DoURL() LEAVE\n")
_, err := io.Copy(b, res.Body)
return err
default:
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)
}
})
if err != nil {
klog.V(1).Infof("err = c.Client.Do failed. Err: %v\n", err)
klog.V(6).Infof("prerecorded.DoURL() LEAVE\n")
return err
}
klog.V(3).Infof("prerecorded.DoURL() Succeeded\n")
klog.V(6).Infof("prerecorded.DoURL() LEAVE\n")
return nil
}