Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 32ea176

Browse files
authored
Sync from internal repo (2024-05-17) (#21)
* chore(sdk/go): sync types from spec (#4748) GitOrigin-RevId: 0df3574abf2960e614b73973b630d16de8185e14 * fix(sdk/go): update user agent format (#4882) GitOrigin-RevId: 768f0b8a58d923e6c997d5c647a33f29eb2672be * fix(sdk/go): unmarshal subtitles as byte slice (#4880) GitOrigin-RevId: f8658cc46919d4d65685cd135b00e301ab9b64d8 * chore(sdk/go): gofumpt files
1 parent 56d6173 commit 32ea176

File tree

6 files changed

+283
-18
lines changed

6 files changed

+283
-18
lines changed

assemblyai.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7+
"fmt"
78
"io"
89
"net/http"
910
"net/url"
1011
"os"
1112
)
1213

1314
const (
14-
version = "1.5.0"
15+
version = "1.5.1"
1516
defaultBaseURLScheme = "https"
1617
defaultBaseURLHost = "api.assemblyai.com"
17-
defaultUserAgent = "assemblyai-go/" + version
1818
)
1919

2020
// Client manages the communication with the AssemblyAI API.
@@ -41,7 +41,7 @@ func NewClientWithOptions(opts ...ClientOption) *Client {
4141
Scheme: defaultBaseURLScheme,
4242
Host: defaultBaseURLHost,
4343
},
44-
userAgent: defaultUserAgent,
44+
userAgent: fmt.Sprintf("AssemblyAI/1.0 (sdk=Go/%s)", version),
4545
httpClient: &http.Client{},
4646
apiKey: defaultAPIKey,
4747
}
@@ -157,7 +157,12 @@ func (c *Client) do(req *http.Request, v interface{}) (*http.Response, error) {
157157
}
158158

159159
if v != nil {
160-
err = json.NewDecoder(resp.Body).Decode(v)
160+
switch val := v.(type) {
161+
case *[]byte:
162+
*val, err = io.ReadAll(resp.Body)
163+
default:
164+
err = json.NewDecoder(resp.Body).Decode(v)
165+
}
161166
}
162167

163168
return resp, err

examples/realtime/realtime.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"log/slog"
67
"os"
78
"os/signal"
89
"syscall"
910

10-
"log/slog"
11-
1211
"github.com/AssemblyAI/assemblyai-go-sdk"
1312
"github.com/gordonklaus/portaudio"
1413
)
@@ -36,7 +35,6 @@ func (h *realtimeHandler) Error(err error) {
3635
}
3736

3837
func main() {
39-
4038
sigs := make(chan os.Signal, 1)
4139

4240
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

testdata/transcript/subtitles.srt

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
1
2+
00:00:05,400 --> 00:00:12,550
3+
Runner's knee runner's
4+
5+
2
6+
00:00:12,582 --> 00:00:16,086
7+
knee is a condition characterized by pain behind or around
8+
9+
3
10+
00:00:16,150 --> 00:00:19,422
11+
the kneecap. It is caused by overuse,
12+
13+
4
14+
00:00:19,518 --> 00:00:23,870
15+
muscle imbalance and inadequate stretching. Symptoms include
16+
17+
5
18+
00:00:23,982 --> 00:00:27,294
19+
pain under or around the kneecap, pain when
20+
21+
6
22+
00:00:27,334 --> 00:00:30,782
23+
walking sprained ankle one
24+
25+
7
26+
00:00:30,798 --> 00:00:33,470
27+
nil here in the 37th minute she is between two guatemalan
28+
29+
8
30+
00:00:33,502 --> 00:00:36,782
31+
defenders and then goes down and stays down and
32+
33+
9
34+
00:00:36,798 --> 00:00:40,134
35+
you will see why. The ligaments of the ankle holds the ankle
36+
37+
10
38+
00:00:40,174 --> 00:00:43,798
39+
bones and joint in position. They protect the ankle
40+
41+
11
42+
00:00:43,846 --> 00:00:46,870
43+
from abnormal movements such as twisting, turning and
44+
45+
12
46+
00:00:46,902 --> 00:00:50,710
47+
rolling of the foot. A sprained ankle happens when the
48+
49+
13
50+
00:00:50,742 --> 00:00:54,434
51+
foot twists, rolls or turns beyond its normal motions.
52+
53+
14
54+
00:00:55,484 --> 00:00:58,864
55+
If the force is too strong, the ligaments can tear.
56+
57+
15
58+
00:00:59,404 --> 00:01:02,844
59+
Symptoms include pain and difficulty moving the ankle,
60+
61+
16
62+
00:01:03,004 --> 00:01:05,784
63+
swelling around the ankle and bruising.
64+
65+
17
66+
00:01:08,004 --> 00:01:11,252
67+
Meniscus tear I think some of it was just
68+
69+
18
70+
00:01:11,308 --> 00:01:14,804
71+
being scared, but this guy doesn't like you. Want to go
72+
73+
19
74+
00:01:14,844 --> 00:01:18,756
75+
after Patrick each of your knees has two menisci c shaped
76+
77+
20
78+
00:01:18,780 --> 00:01:21,792
79+
pieces of cartilage that act like a cushion between your
80+
81+
21
82+
00:01:21,808 --> 00:01:23,604
83+
shin bone and your thigh bone.
84+
85+
22
86+
00:01:25,504 --> 00:01:28,888
87+
A meniscus tear happens when you forcibly twist or rotate
88+
89+
23
90+
00:01:28,936 --> 00:01:31,952
91+
your knee, especially when putting the pressure of your full
92+
93+
24
94+
00:01:32,008 --> 00:01:36,144
95+
weight on it, leading to a torn meniscus. Symptoms include
96+
97+
25
98+
00:01:36,264 --> 00:01:39,288
99+
stiffness and swelling, pain in your knee,
100+
101+
26
102+
00:01:39,456 --> 00:01:43,368
103+
catching or locking of your knee rotator
104+
105+
27
106+
00:01:43,416 --> 00:01:46,996
107+
cuff tear has a torn rotator cuff cuff go be traveling
108+
109+
28
110+
00:01:47,020 --> 00:01:50,676
111+
to Los Angeles today to be examined by teen doctors on the
112+
113+
29
114+
00:01:50,700 --> 00:01:54,156
115+
rotator cuff attaches the humerus to the shoulder blade and
116+
117+
30
118+
00:01:54,180 --> 00:01:56,344
119+
helps to lift and rotate your arm.
120+
121+
31
122+
00:01:58,084 --> 00:02:01,940
123+
A rotator cuff tear is caused by a fall onto your arm or
124+
125+
32
126+
00:02:01,972 --> 00:02:04,304
127+
if you lift a heavy object too fast,
128+
129+
33
130+
00:02:06,284 --> 00:02:09,420
131+
the tendon can partially or completely tear off of the
132+
133+
34
134+
00:02:09,452 --> 00:02:13,160
135+
humerus. Head symptoms
136+
137+
35
138+
00:02:13,192 --> 00:02:16,364
139+
include pain when lifting and lowering your arm,
140+
141+
36
142+
00:02:16,664 --> 00:02:19,364
143+
weakness when lifting or rotating your arm,
144+
145+
37
146+
00:02:19,664 --> 00:02:22,284
147+
pain when lying on the affected shoulder.
148+
149+
38
150+
00:02:25,544 --> 00:02:29,504
151+
ACL tear here's Rosario on the brake now and watch.
152+
153+
39
154+
00:02:29,664 --> 00:02:32,888
155+
Nerls go up with the left hand, block the shot, and then on
156+
157+
40
158+
00:02:32,936 --> 00:02:36,376
159+
landing, there came the the ACL runs diagonally in the
160+
161+
41
162+
00:02:36,400 --> 00:02:38,484
163+
middle of the knee and provides stability.
164+
165+
42
166+
00:02:40,034 --> 00:02:43,330
167+
Anterior cruciate ligament tear occurs when your foot
168+
169+
43
170+
00:02:43,362 --> 00:02:46,714
171+
is firmly planted on the ground and a sudden force hits your
172+
173+
44
174+
00:02:46,754 --> 00:02:49,574
175+
knee while your leg is straight or slightly bent.
176+
177+
45
178+
00:02:52,194 --> 00:02:55,282
179+
This can happen when you are changing direction rapidly,
180+
181+
46
182+
00:02:55,418 --> 00:02:58,294
183+
slowing down when running or landing from a jump.
184+
185+
47
186+
00:02:59,834 --> 00:03:02,690
187+
The ligament completely tears into two pieces,
188+
189+
48
190+
00:03:02,842 --> 00:03:04,574
191+
making the knee unstable.
192+
193+
49
194+
00:03:06,274 --> 00:03:09,826
195+
Symptoms include severe pain and tenderness in knee,
196+
197+
50
198+
00:03:10,010 --> 00:03:13,514
199+
loss of full range of motion, swelling around the knee.

transcript.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,15 @@ func (s *TranscriptService) GetSubtitles(ctx context.Context, transcriptID strin
187187
req.URL.RawQuery = values.Encode()
188188
}
189189

190-
resp, err := s.client.do(req, nil)
190+
var res []byte
191+
192+
resp, err := s.client.do(req, &res)
191193
if err != nil {
192194
return nil, err
193195
}
194196
defer resp.Body.Close()
195197

196-
return io.ReadAll(resp.Body)
198+
return res, nil
197199
}
198200

199201
// List returns a collection of transcripts based on a filter.

transcript_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package assemblyai
22

33
import (
4+
"bufio"
45
"bytes"
56
"context"
67
"encoding/json"
@@ -238,6 +239,43 @@ func TestTranscripts_SearchWords(t *testing.T) {
238239
require.Equal(t, want, results)
239240
}
240241

242+
func TestTranscripts_GetSubtitles(t *testing.T) {
243+
t.Parallel()
244+
245+
client, handler, teardown := setup()
246+
defer teardown()
247+
248+
handler.HandleFunc("/v2/transcript/"+fakeTranscriptID+"/srt", func(w http.ResponseWriter, r *http.Request) {
249+
require.Equal(t, "GET", r.Method)
250+
require.Equal(t, "chars_per_caption=60", r.URL.RawQuery)
251+
252+
writeFileResponse(t, w, "testdata/transcript/subtitles.srt")
253+
})
254+
255+
ctx := context.Background()
256+
257+
st, err := client.Transcripts.GetSubtitles(ctx, fakeTranscriptID, "srt", &TranscriptGetSubtitlesOptions{
258+
CharsPerCaption: 60,
259+
})
260+
require.NoError(t, err)
261+
262+
r := bufio.NewReader(bytes.NewReader(st))
263+
264+
var line []byte
265+
266+
line, _, err = r.ReadLine()
267+
require.NoError(t, err)
268+
require.Equal(t, []byte("1"), line)
269+
270+
line, _, err = r.ReadLine()
271+
require.NoError(t, err)
272+
require.Equal(t, []byte("00:00:05,400 --> 00:00:12,550"), line)
273+
274+
line, _, err = r.ReadLine()
275+
require.NoError(t, err)
276+
require.Equal(t, []byte("Runner's knee runner's"), line)
277+
}
278+
241279
func TestAPIError(t *testing.T) {
242280
t.Parallel()
243281

0 commit comments

Comments
 (0)