Skip to content

Commit 8b5ac85

Browse files
com.rest.elevenlabs 3.1.0 (#49)
- updated authentication - updated unit tests
1 parent 85f3ac9 commit 8b5ac85

16 files changed

+196
-272
lines changed

Documentation~/README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ The recommended installation method is though the unity package manager and [Ope
4848

4949
### Table of Contents
5050

51-
- [Authentication](#authentication)
51+
- [Authentication](#authentication) :construction:
5252
- [API Proxy](#api-proxy)
5353
- [Editor Dashboard](#editor-dashboard)
5454
- [Speech Synthesis Dashboard](#speech-synthesis-dashboard)
@@ -57,7 +57,7 @@ The recommended installation method is though the unity package manager and [Ope
5757
- [Voice Cloning Dashboard](#voice-cloning-dashboard)
5858
- [History](#history)
5959
- [Text to Speech](#text-to-speech)
60-
- [Stream Text To Speech](#stream-text-to-speech) :new:
60+
- [Stream Text To Speech](#stream-text-to-speech)
6161
- [Voices](#voices)
6262
- [Get All Voices](#get-all-voices)
6363
- [Get Default Voice Settings](#get-default-voice-settings)
@@ -67,13 +67,13 @@ The recommended installation method is though the unity package manager and [Ope
6767
- [Edit Voice](#edit-voice)
6868
- [Delete Voice](#delete-voice)
6969
- [Samples](#samples)
70-
- [Download Voice Sample](#download-voice-sample) :new:
70+
- [Download Voice Sample](#download-voice-sample)
7171
- [Delete Voice Sample](#delete-voice-sample)
7272
- [History](#history)
7373
- [Get History](#get-history)
7474
- [Get History Item](#get-history-item)
75-
- [Download History Audio](#download-history-audio) :new:
76-
- [Download History Items](#download-history-items) :new:
75+
- [Download History Audio](#download-history-audio)
76+
- [Download History Items](#download-history-items)
7777
- [Delete History Item](#delete-history-item)
7878
- [User](#user)
7979
- [Get User Info](#get-user-info)
@@ -83,8 +83,10 @@ The recommended installation method is though the unity package manager and [Ope
8383

8484
There are 4 ways to provide your API keys, in order of precedence:
8585

86-
1. [Pass keys directly with constructor](#pass-keys-directly-with-constructor)
87-
2. [Unity Scriptable Object](#unity-scriptable-object)
86+
:warning: We recommended using the environment variables to load the API key instead of having it hard coded in your source. It is not recommended use this method in production, but only for accepting user credentials, local testing and quick start scenarios.
87+
88+
1. [Pass keys directly with constructor](#pass-keys-directly-with-constructor) :warning:
89+
2. [Unity Scriptable Object](#unity-scriptable-object) :warning:
8890
3. [Load key from configuration file](#load-key-from-configuration-file)
8991
4. [Use System Environment Variables](#use-system-environment-variables)
9092

@@ -125,7 +127,7 @@ To create a configuration file, create a new text file named `.elevenlabs` and c
125127
You can also load the file directly with known path by calling a static method in Authentication:
126128

127129
```csharp
128-
var api = new ElevenLabsClient(ElevenLabsAuthentication.Default.LoadFromDirectory("your/path/to/.elevenlabs"));;
130+
var api = new ElevenLabsClient(new ElevenLabsAuthentication().LoadFromDirectory("your/path/to/.elevenlabs"));;
129131
```
130132

131133
#### Use System Environment Variables
@@ -135,7 +137,7 @@ Use your system's environment variables specify an api key to use.
135137
- Use `ELEVEN_LABS_API_KEY` for your api key.
136138

137139
```csharp
138-
var api = new ElevenLabsClient(ElevenLabsAuthentication.Default.LoadFromEnvironment());
140+
var api = new ElevenLabsClient(new ElevenLabsAuthentication().LoadFromEnvironment());
139141
```
140142

141143
### [API Proxy](https://github.com/RageAgainstThePixel/ElevenLabs-DotNet/main/ElevenLabs-DotNet-Proxy/README.md)

Runtime/ElevenLabsAuthentication.cs

Lines changed: 41 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace ElevenLabs
1111
/// <summary>
1212
/// Represents authentication for ElevenLabs
1313
/// </summary>
14-
public sealed class ElevenLabsAuthentication : AbstractAuthentication<ElevenLabsAuthentication, ElevenLabsAuthInfo>
14+
public sealed class ElevenLabsAuthentication : AbstractAuthentication<ElevenLabsAuthentication, ElevenLabsAuthInfo, ElevenLabsConfiguration>
1515
{
1616
internal const string CONFIG_FILE = ".elevenlabs";
1717
private const string ELEVEN_LABS_API_KEY = nameof(ELEVEN_LABS_API_KEY);
@@ -23,30 +23,35 @@ public sealed class ElevenLabsAuthentication : AbstractAuthentication<ElevenLabs
2323
public static implicit operator ElevenLabsAuthentication(string apiKey) => new ElevenLabsAuthentication(apiKey);
2424

2525
/// <summary>
26-
/// Instantiates a new Authentication object that will load the default config.
26+
/// Instantiates an empty Authentication object.
2727
/// </summary>
28-
public ElevenLabsAuthentication()
29-
{
30-
if (cachedDefault != null) { return; }
31-
32-
cachedDefault = (LoadFromAsset<ElevenLabsConfiguration>() ??
33-
LoadFromDirectory()) ??
34-
LoadFromDirectory(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)) ??
35-
LoadFromEnvironment();
36-
Info = cachedDefault?.Info;
37-
}
28+
public ElevenLabsAuthentication() { }
3829

3930
/// <summary>
4031
/// Instantiates a new Authentication object with the given <paramref name="apiKey"/>, which may be <see langword="null"/>.
4132
/// </summary>
4233
/// <param name="apiKey">The API key, required to access the API endpoint.</param>
43-
public ElevenLabsAuthentication(string apiKey) => Info = new ElevenLabsAuthInfo(apiKey);
34+
public ElevenLabsAuthentication(string apiKey)
35+
{
36+
Info = new ElevenLabsAuthInfo(apiKey);
37+
cachedDefault = this;
38+
}
4439

4540
/// <summary>
4641
/// Instantiates a new Authentication object with the given <paramref name="authInfo"/>, which may be <see langword="null"/>.
4742
/// </summary>
4843
/// <param name="authInfo"></param>
49-
public ElevenLabsAuthentication(ElevenLabsAuthInfo authInfo) => this.Info = authInfo;
44+
public ElevenLabsAuthentication(ElevenLabsAuthInfo authInfo)
45+
{
46+
Info = authInfo;
47+
cachedDefault = this;
48+
}
49+
50+
/// <summary>
51+
/// Instantiates a new Authentication object with the given <see cref="configuration"/>.
52+
/// </summary>
53+
/// <param name="configuration"><see cref="ElevenLabsConfiguration"/>.</param>
54+
public ElevenLabsAuthentication(ElevenLabsConfiguration configuration) : this(configuration.ApiKey) { }
5055

5156
/// <inheritdoc />
5257
public override ElevenLabsAuthInfo Info { get; }
@@ -60,23 +65,21 @@ public ElevenLabsAuthentication()
6065
/// </summary>
6166
public static ElevenLabsAuthentication Default
6267
{
63-
get => cachedDefault ?? new ElevenLabsAuthentication();
68+
get => cachedDefault ?? new ElevenLabsAuthentication().LoadDefault();
6469
internal set => cachedDefault = value;
6570
}
6671

67-
[Obsolete("Use ElevenLabsAuthentication.Info.ApiKey")]
68-
public string ApiKey => Info.ApiKey;
69-
7072
/// <inheritdoc />
71-
public override ElevenLabsAuthentication LoadFromAsset<T>()
72-
=> Resources.LoadAll<T>(string.Empty)
73-
.Where(asset => asset != null)
74-
.Where(asset => asset is ElevenLabsConfiguration config &&
75-
!string.IsNullOrWhiteSpace(config.ApiKey))
76-
.Select(asset => asset is ElevenLabsConfiguration config
77-
? new ElevenLabsAuthentication(config.ApiKey)
78-
: null)
79-
.FirstOrDefault();
73+
public override ElevenLabsAuthentication LoadFromAsset(ElevenLabsConfiguration configuration = null)
74+
{
75+
if (configuration == null)
76+
{
77+
Debug.LogWarning($"This can be speed this up by passing a {nameof(ElevenLabsConfiguration)} to the {nameof(ElevenLabsAuthentication)}.ctr");
78+
configuration = Resources.LoadAll<ElevenLabsConfiguration>(string.Empty).FirstOrDefault(o => o != null);
79+
}
80+
81+
return configuration != null ? new ElevenLabsAuthentication(configuration) : null;
82+
}
8083

8184
/// <inheritdoc />
8285
public override ElevenLabsAuthentication LoadFromEnvironment()
@@ -94,11 +97,16 @@ public override ElevenLabsAuthentication LoadFromDirectory(string directory = nu
9497
directory = Environment.CurrentDirectory;
9598
}
9699

100+
if (string.IsNullOrWhiteSpace(filename))
101+
{
102+
filename = CONFIG_FILE;
103+
}
104+
97105
ElevenLabsAuthInfo tempAuthInfo = null;
98106

99107
var currentDirectory = new DirectoryInfo(directory);
100108

101-
while (tempAuthInfo == null && currentDirectory.Parent != null)
109+
while (tempAuthInfo == null && currentDirectory?.Parent != null)
102110
{
103111
var filePath = Path.Combine(currentDirectory.FullName, filename);
104112

@@ -126,12 +134,11 @@ public override ElevenLabsAuthentication LoadFromDirectory(string directory = nu
126134
var part = parts[i];
127135
var nextPart = parts[i + 1];
128136

129-
switch (part)
137+
apiKey = part switch
130138
{
131-
case ELEVEN_LABS_API_KEY:
132-
apiKey = nextPart.Trim();
133-
break;
134-
}
139+
ELEVEN_LABS_API_KEY => nextPart.Trim(),
140+
_ => apiKey
141+
};
135142
}
136143
}
137144

@@ -148,16 +155,7 @@ public override ElevenLabsAuthentication LoadFromDirectory(string directory = nu
148155
}
149156
}
150157

151-
if (tempAuthInfo == null ||
152-
string.IsNullOrEmpty(tempAuthInfo.ApiKey))
153-
{
154-
return null;
155-
}
156-
157-
return new ElevenLabsAuthentication(tempAuthInfo);
158+
return string.IsNullOrEmpty(tempAuthInfo?.ApiKey) ? null : new ElevenLabsAuthentication(tempAuthInfo);
158159
}
159-
160-
[Obsolete("use ElevenLabsAuthentication.Default.LoadFromEnvironment")]
161-
public static ElevenLabsAuthentication LoadFromEnv() => Default.LoadFromEnvironment();
162160
}
163161
}

Runtime/ElevenLabsClient.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public sealed class ElevenLabsClient : BaseClient<ElevenLabsAuthentication, Elev
1818
/// <summary>
1919
/// Creates a new client for the ElevenLabs API, handling auth and allowing for access to various API endpoints.
2020
/// </summary>
21-
/// <param name="elevenLabsAuthentication">The API authentication information to use for API calls,
21+
/// <param name="authentication">The API authentication information to use for API calls,
2222
/// or <see langword="null"/> to attempt to use the <see cref="ElevenLabsAuthentication.Default"/>,
2323
/// potentially loading from environment vars or from a config file.</param>
24-
/// <param name="clientSettings">Optional, <see cref="ElevenLabsClientSettings"/> for specifying a proxy domain.</param>
24+
/// <param name="settings">Optional, <see cref="ElevenLabsSettings"/> for specifying a proxy domain.</param>
2525
/// <exception cref="AuthenticationException">Raised when authentication details are missing or invalid.</exception>
26-
public ElevenLabsClient(ElevenLabsAuthentication elevenLabsAuthentication = null, ElevenLabsSettings clientSettings = null)
27-
: base(elevenLabsAuthentication ?? ElevenLabsAuthentication.Default, clientSettings ?? ElevenLabsSettings.Default)
26+
public ElevenLabsClient(ElevenLabsAuthentication authentication = null, ElevenLabsSettings settings = null)
27+
: base(authentication ?? ElevenLabsAuthentication.Default, settings ?? ElevenLabsSettings.Default)
2828
{
2929
UserEndpoint = new UserEndpoint(this);
3030
VoicesEndpoint = new VoicesEndpoint(this);
@@ -33,6 +33,7 @@ public ElevenLabsClient(ElevenLabsAuthentication elevenLabsAuthentication = null
3333
TextToSpeechEndpoint = new TextToSpeechEndpoint(this);
3434
VoiceGenerationEndpoint = new VoiceGenerationEndpoint(this);
3535
}
36+
3637
protected override void SetupDefaultRequestHeaders()
3738
{
3839
var headers = new Dictionary<string, string>
@@ -48,6 +49,11 @@ protected override void SetupDefaultRequestHeaders()
4849

4950
protected override void ValidateAuthentication()
5051
{
52+
if (Authentication?.Info == null)
53+
{
54+
throw new InvalidCredentialException($"Invalid {nameof(ElevenLabsAuthentication)}");
55+
}
56+
5157
if (!HasValidAuthentication)
5258
{
5359
throw new AuthenticationException("You must provide API authentication. Please refer to https://github.com/RageAgainstThePixel/com.rest.elevenlabs#authentication for details.");

Runtime/ElevenLabsClientSettings.cs

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)