-
Notifications
You must be signed in to change notification settings - Fork 12
/
revai.go
244 lines (199 loc) · 5.15 KB
/
revai.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
package revai
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
"github.com/google/go-querystring/query"
)
const (
defaultBaseURL = "https://api.rev.ai"
defaultUserAgent = "revai-go-client"
)
const (
XSubripHeader = "application/x-subrip"
TextVTTHeader = "text/vtt"
TextPlainHeader = "text/plain"
RevTranscriptJSONHeader = "application/vnd.rev.transcript.v1.0+json"
)
type service struct {
client *Client
}
// A Client manages communication with the Rev.ai API.
type Client struct {
HTTPClient *http.Client
BaseURL *url.URL
UserAgent string
APIKey string
common service
// Services used for talking to different parts of the Rev.ai API.
Job *JobService
Account *AccountService
Caption *CaptionService
Transcript *TranscriptService
CustomVocabulary *CustomVocabularyService
Stream *StreamService
}
type ClientOption func(*Client)
// HTTPClient sets the http client for the rev.ai client
func HTTPClient(httpClient *http.Client) func(*Client) {
return func(c *Client) {
c.HTTPClient = httpClient
}
}
// UserAgent sets the user agent for the rev.ai client
func UserAgent(userAgent string) func(*Client) {
return func(c *Client) {
c.UserAgent = userAgent
}
}
// BaseURL sets the base url for the rev.ai client
func BaseURL(u *url.URL) func(*Client) {
return func(c *Client) {
c.BaseURL = u
}
}
// NewClient creates a new client and sets defaults. It then updates the client with any options passed in.
func NewClient(apiKey string, opts ...ClientOption) *Client {
baseURL, _ := url.Parse(defaultBaseURL)
c := &Client{
HTTPClient: &http.Client{},
BaseURL: baseURL,
APIKey: apiKey,
UserAgent: defaultUserAgent,
}
for _, option := range opts {
option(c)
}
c.common.client = c
c.Job = (*JobService)(&c.common)
c.Account = (*AccountService)(&c.common)
c.Caption = (*CaptionService)(&c.common)
c.Transcript = (*TranscriptService)(&c.common)
c.CustomVocabulary = (*CustomVocabularyService)(&c.common)
c.Stream = (*StreamService)(&c.common)
return c
}
func (c *Client) newRequest(method string, path string, body interface{}) (*http.Request, error) {
rel := &url.URL{Path: path}
pr, pw := io.Pipe()
go func() {
defer pw.Close()
if body != nil {
if method == http.MethodPost {
if err := json.NewEncoder(pw).Encode(body); err != nil {
pw.CloseWithError(err)
return
}
}
}
}()
if method == http.MethodGet {
v, err := query.Values(body)
if err != nil {
return nil, err
}
rel.RawQuery = v.Encode()
}
u := c.BaseURL.ResolveReference(rel)
req, err := http.NewRequest(method, u.String(), pr)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Bearer "+c.APIKey)
return req, nil
}
func (c *Client) newMultiPartRequest(mw *multipart.Writer, path string, body io.Reader) (*http.Request, error) {
rel := &url.URL{Path: path}
u := c.BaseURL.ResolveReference(rel)
req, err := http.NewRequest(http.MethodPost, u.String(), body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", mw.FormDataContentType())
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Bearer "+c.APIKey)
return req, nil
}
func (c *Client) doJSON(ctx context.Context, req *http.Request, v interface{}) error {
req = req.WithContext(ctx)
resp, err := c.HTTPClient.Do(req)
if err != nil {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, resp.Body); err != nil {
return err
}
return &ErrBadStatusCode{
OriginalBody: buf.String(),
Code: resp.StatusCode,
}
}
if v == nil {
return nil
}
if err := json.NewDecoder(resp.Body).Decode(v); err != nil {
return fmt.Errorf("failed decoding response %w", err)
}
return nil
}
func (c *Client) do(ctx context.Context, req *http.Request) (*http.Response, error) {
req = req.WithContext(ctx)
resp, err := c.HTTPClient.Do(req)
if err != nil {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
return nil, err
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, resp.Body); err != nil {
return nil, err
}
return nil, &ErrBadStatusCode{
OriginalBody: buf.String(),
Code: resp.StatusCode,
}
}
return resp, nil
}
func makeReaderPart(mw *multipart.Writer, partName, filename string, partValue io.Reader) error {
part, err := mw.CreateFormFile(partName, filename)
if err != nil {
return err
}
if _, err := io.Copy(part, partValue); err != nil {
return err
}
return nil
}
func makeStringPart(mw *multipart.Writer, partName string, partValue string) error {
part, err := mw.CreateFormField(partName)
if err != nil {
return err
}
if _, err := io.Copy(part, strings.NewReader(partValue)); err != nil {
return err
}
return nil
}