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

Commit 52add84

Browse files
authored
Sync from internal repo (2024-10-21) (#31)
* fix(sdk/go): migrate from nhooyr.io/websocket to coder/websocket (#6735) GitOrigin-RevId: a2afc9dd6887e79cfacc4e443dccf58245f6f648 * feat(sdk/go): add support for multichannel (#6815) GitOrigin-RevId: a0cbddc3866fd5a1d8525907fe2d280cf9e940b5
1 parent 878ef53 commit 52add84

20 files changed

+460
-185
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ linters-settings:
1616
main:
1717
allow:
1818
- $gostd
19-
- nhooyr.io/websocket
19+
- github.com/coder/websocket
2020
- github.com/google
2121
- github.com/stretchr/testify
2222
- github.com/cenkalti/backoff

README.md

Lines changed: 80 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ A Go client library for accessing [AssemblyAI](https://assemblyai.com).
1717
## Overview
1818

1919
- [AssemblyAI Go SDK](#assemblyai-go-sdk)
20-
- [Overview](#overview)
21-
- [Documentation](#documentation)
22-
- [Quickstart](#quickstart)
23-
- [Installation](#installation)
24-
- [Examples](#examples)
25-
- [Core Transcription](#core-transcription)
26-
- [Audio Intelligence](#audio-intelligence)
27-
- [Real-Time Transcription](#real-time-transcription)
28-
- [Playgrounds](#playgrounds)
20+
- [Overview](#overview)
21+
- [Documentation](#documentation)
22+
- [Quickstart](#quickstart)
23+
- [Installation](#installation)
24+
- [Examples](#examples)
25+
- [Core Transcription](#core-transcription)
26+
- [Audio Intelligence](#audio-intelligence)
27+
- [Real-Time Transcription](#real-time-transcription)
28+
- [Playgrounds](#playgrounds)
29+
- [Tips and tricks](#tips-and-tricks)
30+
- [Inspect API errors](#inspect-api-errors)
2931

3032
## Documentation
3133

@@ -54,28 +56,28 @@ Before you begin, you need to have your API key. If you don't have one yet, [**s
5456
package main
5557

5658
import (
57-
"context"
58-
"log"
59-
"os"
59+
"context"
60+
"log"
61+
"os"
6062

61-
"github.com/AssemblyAI/assemblyai-go-sdk"
63+
"github.com/AssemblyAI/assemblyai-go-sdk"
6264
)
6365

6466
func main() {
65-
apiKey := os.Getenv("ASSEMBLYAI_API_KEY")
67+
apiKey := os.Getenv("ASSEMBLYAI_API_KEY")
6668

67-
ctx := context.Background()
69+
ctx := context.Background()
6870

69-
audioURL := "https://example.org/audio.mp3"
71+
audioURL := "https://example.org/audio.mp3"
7072

71-
client := assemblyai.NewClient(apiKey)
73+
client := assemblyai.NewClient(apiKey)
7274

73-
transcript, err := client.Transcripts.TranscribeFromURL(ctx, audioURL, nil)
74-
if err != nil {
75-
log.Fatal("Something bad happened:", err)
76-
}
75+
transcript, err := client.Transcripts.TranscribeFromURL(ctx, audioURL, nil)
76+
if err != nil {
77+
log.Fatal("Something bad happened:", err)
78+
}
7779

78-
log.Println(*transcript.Text)
80+
log.Println(*transcript.Text)
7981
}
8082
```
8183

@@ -87,32 +89,32 @@ func main() {
8789
package main
8890

8991
import (
90-
"context"
91-
"log"
92-
"os"
92+
"context"
93+
"log"
94+
"os"
9395

94-
"github.com/AssemblyAI/assemblyai-go-sdk"
96+
"github.com/AssemblyAI/assemblyai-go-sdk"
9597
)
9698

9799
func main() {
98-
apiKey := os.Getenv("ASSEMBLYAI_API_KEY")
100+
apiKey := os.Getenv("ASSEMBLYAI_API_KEY")
99101

100-
ctx := context.Background()
102+
ctx := context.Background()
101103

102-
client := assemblyai.NewClient(apiKey)
104+
client := assemblyai.NewClient(apiKey)
103105

104-
f, err := os.Open("./my-local-audio-file.wav")
105-
if err != nil {
106-
log.Fatal("Couldn't open audio file:", err)
107-
}
108-
defer f.Close()
106+
f, err := os.Open("./my-local-audio-file.wav")
107+
if err != nil {
108+
log.Fatal("Couldn't open audio file:", err)
109+
}
110+
defer f.Close()
109111

110-
transcript, err := client.Transcripts.TranscribeFromReader(ctx, f, nil)
111-
if err != nil {
112-
log.Fatal("Something bad happened:", err)
113-
}
112+
transcript, err := client.Transcripts.TranscribeFromReader(ctx, f, nil)
113+
if err != nil {
114+
log.Fatal("Something bad happened:", err)
115+
}
114116

115-
log.Println(*transcript.Text)
117+
log.Println(*transcript.Text)
116118
}
117119
```
118120

@@ -127,36 +129,36 @@ func main() {
127129
package main
128130

129131
import (
130-
"context"
131-
"log"
132-
"os"
132+
"context"
133+
"log"
134+
"os"
133135

134-
"github.com/AssemblyAI/assemblyai-go-sdk"
136+
"github.com/AssemblyAI/assemblyai-go-sdk"
135137
)
136138

137139
func main() {
138-
apiKey := os.Getenv("ASSEMBLYAI_API_KEY")
140+
apiKey := os.Getenv("ASSEMBLYAI_API_KEY")
139141

140-
ctx := context.Background()
142+
ctx := context.Background()
141143

142-
audioURL := "https://example.org/audio.mp3"
144+
audioURL := "https://example.org/audio.mp3"
143145

144-
client := assemblyai.NewClient(apiKey)
146+
client := assemblyai.NewClient(apiKey)
145147

146-
opts := &assemblyai.TranscriptParams{
147-
EntityDetection: assemblyai.Bool(true),
148-
}
148+
opts := &assemblyai.TranscriptParams{
149+
EntityDetection: assemblyai.Bool(true),
150+
}
149151

150-
transcript, err := client.Transcripts.TranscribeFromURL(ctx, audioURL, opts)
151-
if err != nil {
152-
log.Fatal("Something bad happened:", err)
153-
}
152+
transcript, err := client.Transcripts.TranscribeFromURL(ctx, audioURL, opts)
153+
if err != nil {
154+
log.Fatal("Something bad happened:", err)
155+
}
154156

155-
for _, entity := range transcript.Entities {
156-
log.Println(*entity.Text)
157-
log.Println(entity.EntityType)
158-
log.Printf("Timestamp: %v - %v", *entity.Start, *entity.End)
159-
}
157+
for _, entity := range transcript.Entities {
158+
log.Println(*entity.Text)
159+
log.Println(entity.EntityType)
160+
log.Printf("Timestamp: %v - %v", *entity.Start, *entity.End)
161+
}
160162
}
161163
```
162164

@@ -172,3 +174,22 @@ Visit one of our Playgrounds:
172174

173175
- [LeMUR Playground](https://www.assemblyai.com/playground/v2/source)
174176
- [Transcription Playground](https://www.assemblyai.com/playground)
177+
178+
## Tips and tricks
179+
180+
### Inspect API errors
181+
182+
If you receive an API error, you can inspect the HTTP response returned by the API for more details:
183+
184+
```go
185+
transcript, err := client.Transcripts.TranscribeFromURL(ctx, audioURL, nil)
186+
if err != nil {
187+
var apierr aai.APIError
188+
if errors.As(err, &apierr) {
189+
// apierr.Response is the *http.Response from the API call.
190+
fmt.Println(apierr.Response.StatusCode)
191+
} else {
192+
// err is not an API error.
193+
}
194+
}
195+
```

assemblyai.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9+
"mime"
910
"net/http"
1011
"net/url"
1112
"os"
1213
)
1314

1415
const (
15-
version = "1.8.1"
16+
version = "1.9.0"
1617
defaultBaseURLScheme = "https"
1718
defaultBaseURLHost = "api.assemblyai.com"
1819
)
@@ -137,33 +138,58 @@ func (c *Client) newRequest(ctx context.Context, method, path string, body io.Re
137138
return req, err
138139
}
139140

140-
func (c *Client) do(req *http.Request, v interface{}) (*http.Response, error) {
141+
func (c *Client) do(req *http.Request, v interface{}) error {
141142
resp, err := c.httpClient.Do(req)
142143
if err != nil {
143-
return nil, err
144+
return err
144145
}
145146
defer resp.Body.Close()
146147

147-
if resp.StatusCode != http.StatusOK {
148+
contentType := resp.Header.Get("Content-Type")
149+
150+
mimeType, _, err := mime.ParseMediaType(contentType)
151+
if err != nil {
152+
return err
153+
}
154+
155+
isJSONResponse := mimeType == "application/json"
156+
isAPIError := resp.StatusCode < 200 || resp.StatusCode >= 400
157+
158+
if isAPIError {
159+
var buf bytes.Buffer
160+
161+
_, err := io.Copy(&buf, resp.Body)
162+
if err != nil {
163+
return err
164+
}
165+
148166
var apierr APIError
149167

150-
if err := json.NewDecoder(resp.Body).Decode(&apierr); err != nil {
151-
return nil, err
168+
if isJSONResponse {
169+
if err := json.Unmarshal(buf.Bytes(), &apierr); err != nil {
170+
return err
171+
}
152172
}
153173

174+
// Reset response body so that clients can read it again.
175+
resp.Body = io.NopCloser(bytes.NewBuffer(buf.Bytes()))
176+
154177
apierr.Status = resp.StatusCode
178+
apierr.Response = resp
155179

156-
return nil, apierr
180+
return apierr
157181
}
158182

159183
if v != nil {
160184
switch val := v.(type) {
161185
case *[]byte:
162186
*val, err = io.ReadAll(resp.Body)
163187
default:
164-
err = json.NewDecoder(resp.Body).Decode(v)
188+
if isJSONResponse {
189+
err = json.NewDecoder(resp.Body).Decode(v)
190+
}
165191
}
166192
}
167193

168-
return resp, err
194+
return err
169195
}

assemblyai_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/http"
55
"net/http/httptest"
66
"os"
7+
"path/filepath"
78
"testing"
89

910
"github.com/stretchr/testify/require"
@@ -22,6 +23,10 @@ func setup() (*Client, *http.ServeMux, func()) {
2223
func writeFileResponse(t *testing.T, w http.ResponseWriter, filename string) {
2324
t.Helper()
2425

26+
if filepath.Ext(filename) == ".json" {
27+
w.Header().Set("Content-Type", "application/json")
28+
}
29+
2530
b, err := os.ReadFile(filename)
2631
require.NoError(t, err)
2732

errors.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package assemblyai
22

3+
import "net/http"
4+
35
// APIError represents an error returned by the AssemblyAI API.
46
type APIError struct {
57
Status int `json:"-"`
68
Message string `json:"error"`
9+
10+
Response *http.Response `json:"-"`
711
}
812

913
// Error returns the API error message.

examples/realtime/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/AssemblyAI/DeepLearning/assemblyai/developer_tools/go/examples/realtime
1+
module github.com/AssemblyAI/assemblyai-go-sdk/examples/realtime
22

33
go 1.21
44

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module github.com/AssemblyAI/assemblyai-go-sdk/examples/upload-with-progress
2+
3+
go 1.21
4+
5+
require (
6+
github.com/AssemblyAI/assemblyai-go-sdk v1.5.1
7+
github.com/schollz/progressbar/v3 v3.14.6
8+
)
9+
10+
require (
11+
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
12+
github.com/google/go-querystring v1.1.0 // indirect
13+
github.com/klauspost/compress v1.10.3 // indirect
14+
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
15+
github.com/rivo/uniseg v0.4.7 // indirect
16+
golang.org/x/sys v0.22.0 // indirect
17+
golang.org/x/term v0.22.0 // indirect
18+
nhooyr.io/websocket v1.8.7 // indirect
19+
)

0 commit comments

Comments
 (0)