-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
295 lines (246 loc) · 7.67 KB
/
client.go
File metadata and controls
295 lines (246 loc) · 7.67 KB
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
// Package elevenlabs provides a Go client for the ElevenLabs API.
//
// The client wraps the ogen-generated API client with a higher-level
// interface that handles authentication and provides convenient methods
// for common operations.
package elevenlabs
import (
"net/http"
"os"
"time"
"github.com/plexusone/elevenlabs-go/internal/api"
)
// Version is the SDK version.
const Version = "0.3.0"
// DefaultBaseURL is the default ElevenLabs API base URL.
const DefaultBaseURL = "https://api.elevenlabs.io"
// DefaultModelID is the recommended model for text-to-speech.
const DefaultModelID = "eleven_multilingual_v2"
// Client is the main ElevenLabs client for interacting with the API.
type Client struct {
apiClient *api.Client
apiKey string
baseURL string
// Service accessors
tts *TextToSpeechService
voices *VoicesService
models *ModelsService
history *HistoryService
user *UserService
dubbing *DubbingService
soundEffects *SoundEffectsService
pronunciation *PronunciationService
projects *ProjectsService
speechToText *SpeechToTextService
forcedAlignment *ForcedAlignmentService
audioIsolation *AudioIsolationService
textToDialogue *TextToDialogueService
voiceDesign *VoiceDesignService
music *MusicService
// Real-time services
webSocketTTS *WebSocketTTSService
webSocketSTT *WebSocketSTTService
twilio *TwilioService
phoneNumbers *PhoneNumberService
speechToSpeech *SpeechToSpeechService
}
// NewClient creates a new ElevenLabs client with the given options.
func NewClient(opts ...Option) (*Client, error) {
options := defaultClientOptions()
for _, opt := range opts {
opt(options)
}
// Try environment variable if API key not set
if options.apiKey == "" {
options.apiKey = os.Getenv("ELEVENLABS_API_KEY")
}
// Create HTTP client with auth headers
httpClient := options.httpClient
if httpClient == nil {
httpClient = &http.Client{
Timeout: options.timeout,
}
}
// Wrap with auth transport
authClient := &authHTTPClient{
client: httpClient,
apiKey: options.apiKey,
}
// Create the ogen client
apiClient, err := api.NewClient(
options.baseURL,
api.WithClient(authClient),
)
if err != nil {
return nil, err
}
c := &Client{
apiClient: apiClient,
apiKey: options.apiKey,
baseURL: options.baseURL,
}
// Initialize services
c.tts = &TextToSpeechService{client: c}
c.voices = &VoicesService{client: c}
c.models = &ModelsService{client: c}
c.history = &HistoryService{client: c}
c.user = &UserService{client: c}
c.dubbing = &DubbingService{client: c}
c.soundEffects = &SoundEffectsService{client: c}
c.pronunciation = &PronunciationService{client: c}
c.projects = &ProjectsService{client: c}
c.speechToText = &SpeechToTextService{client: c}
c.forcedAlignment = &ForcedAlignmentService{client: c}
c.audioIsolation = &AudioIsolationService{client: c}
c.textToDialogue = &TextToDialogueService{client: c}
c.voiceDesign = &VoiceDesignService{client: c}
c.music = &MusicService{client: c}
// Initialize real-time services
c.webSocketTTS = &WebSocketTTSService{client: c}
c.webSocketSTT = &WebSocketSTTService{client: c}
c.twilio = &TwilioService{client: c}
c.phoneNumbers = &PhoneNumberService{client: c}
c.speechToSpeech = &SpeechToSpeechService{client: c}
return c, nil
}
// authHTTPClient wraps an http.Client to add authentication headers.
type authHTTPClient struct {
client *http.Client
apiKey string
}
// Do implements ht.Client interface.
func (c *authHTTPClient) Do(req *http.Request) (*http.Response, error) {
// Add authentication header
if c.apiKey != "" {
req.Header.Set("xi-api-key", c.apiKey)
}
// Add SDK version headers
req.Header.Set("X-ElevenLabs-SDK-Version", Version)
req.Header.Set("X-ElevenLabs-SDK-Lang", "go")
return c.client.Do(req) //nolint:gosec // G704: HTTP client library, URL is caller-controlled by design
}
// API returns the underlying ogen-generated API client for advanced usage.
// Use this when you need access to API endpoints not covered by the
// high-level wrapper methods.
func (c *Client) API() *api.Client {
return c.apiClient
}
// TextToSpeech returns the text-to-speech service.
func (c *Client) TextToSpeech() *TextToSpeechService {
return c.tts
}
// Voices returns the voices service.
func (c *Client) Voices() *VoicesService {
return c.voices
}
// Models returns the models service.
func (c *Client) Models() *ModelsService {
return c.models
}
// History returns the history service.
func (c *Client) History() *HistoryService {
return c.history
}
// User returns the user service.
func (c *Client) User() *UserService {
return c.user
}
// Dubbing returns the dubbing service.
func (c *Client) Dubbing() *DubbingService {
return c.dubbing
}
// SoundEffects returns the sound effects service.
func (c *Client) SoundEffects() *SoundEffectsService {
return c.soundEffects
}
// Pronunciation returns the pronunciation dictionary service.
func (c *Client) Pronunciation() *PronunciationService {
return c.pronunciation
}
// Projects returns the projects (Studio) service.
func (c *Client) Projects() *ProjectsService {
return c.projects
}
// SpeechToText returns the speech-to-text transcription service.
func (c *Client) SpeechToText() *SpeechToTextService {
return c.speechToText
}
// ForcedAlignment returns the forced alignment service.
func (c *Client) ForcedAlignment() *ForcedAlignmentService {
return c.forcedAlignment
}
// AudioIsolation returns the audio isolation service.
func (c *Client) AudioIsolation() *AudioIsolationService {
return c.audioIsolation
}
// TextToDialogue returns the text-to-dialogue service.
func (c *Client) TextToDialogue() *TextToDialogueService {
return c.textToDialogue
}
// VoiceDesign returns the voice design/generation service.
func (c *Client) VoiceDesign() *VoiceDesignService {
return c.voiceDesign
}
// Music returns the music composition service.
func (c *Client) Music() *MusicService {
return c.music
}
// WebSocketTTS returns the WebSocket text-to-speech service for real-time streaming.
func (c *Client) WebSocketTTS() *WebSocketTTSService {
return c.webSocketTTS
}
// WebSocketSTT returns the WebSocket speech-to-text service for real-time transcription.
func (c *Client) WebSocketSTT() *WebSocketSTTService {
return c.webSocketSTT
}
// Twilio returns the Twilio phone integration service.
func (c *Client) Twilio() *TwilioService {
return c.twilio
}
// PhoneNumbers returns the phone number management service.
func (c *Client) PhoneNumbers() *PhoneNumberService {
return c.phoneNumbers
}
// SpeechToSpeech returns the speech-to-speech voice conversion service.
func (c *Client) SpeechToSpeech() *SpeechToSpeechService {
return c.speechToSpeech
}
// clientOptions holds the options for creating a Client.
type clientOptions struct {
apiKey string
baseURL string
httpClient *http.Client
timeout time.Duration
}
func defaultClientOptions() *clientOptions {
return &clientOptions{
baseURL: DefaultBaseURL,
timeout: 120 * time.Second, // TTS can take a while
}
}
// Option is a functional option for configuring the Client.
type Option func(*clientOptions)
// WithAPIKey sets the API key for authentication.
func WithAPIKey(apiKey string) Option {
return func(o *clientOptions) {
o.apiKey = apiKey
}
}
// WithBaseURL sets the API base URL.
func WithBaseURL(baseURL string) Option {
return func(o *clientOptions) {
o.baseURL = baseURL
}
}
// WithHTTPClient sets a custom HTTP client.
func WithHTTPClient(client *http.Client) Option {
return func(o *clientOptions) {
o.httpClient = client
}
}
// WithTimeout sets the request timeout.
func WithTimeout(timeout time.Duration) Option {
return func(o *clientOptions) {
o.timeout = timeout
}
}