forked from revdotcom/revai-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
caption.go
60 lines (48 loc) · 1.19 KB
/
caption.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
package revai
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
)
// CaptionService provides access to the caption related functions
// in the Rev.ai API.
type CaptionService service
// Caption output for a transcription job
type Caption struct {
Value string
}
// GetCaptionParams specifies the parameters to the
// CaptionService.Get method.
type GetCaptionParams struct {
JobID string
Accept string
}
// Get returns the caption output for a transcription job.
// https://www.rev.ai/docs#tag/Captions
func (s *CaptionService) Get(ctx context.Context, params *GetCaptionParams) (*Caption, error) {
urlPath := "/speechtotext/v1/jobs/" + params.JobID + "/captions"
accept := params.Accept
if accept != TextVTTHeader {
accept = XSubripHeader
}
req, err := s.client.newRequest(http.MethodGet, urlPath, nil)
if err != nil {
return nil, fmt.Errorf("failed creating request %w", err)
}
req.Header.Add("Accept", accept)
resp, err := s.client.do(ctx, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, resp.Body); err != nil {
return nil, err
}
caption := Caption{
Value: buf.String(),
}
return &caption, nil
}