Skip to content

Commit

Permalink
com.rest.elevenlabs 3.1.0 (#49)
Browse files Browse the repository at this point in the history
- updated authentication
- updated unit tests
  • Loading branch information
StephenHodgson committed Nov 29, 2023
1 parent 3ee6fdd commit bab230f
Show file tree
Hide file tree
Showing 19 changed files with 211 additions and 285 deletions.
20 changes: 11 additions & 9 deletions ElevenLabs/Packages/com.rest.elevenlabs/Documentation~/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ The recommended installation method is though the unity package manager and [Ope

### Table of Contents

- [Authentication](#authentication)
- [Authentication](#authentication) :construction:
- [API Proxy](#api-proxy)
- [Editor Dashboard](#editor-dashboard)
- [Speech Synthesis Dashboard](#speech-synthesis-dashboard)
Expand All @@ -57,7 +57,7 @@ The recommended installation method is though the unity package manager and [Ope
- [Voice Cloning Dashboard](#voice-cloning-dashboard)
- [History](#history)
- [Text to Speech](#text-to-speech)
- [Stream Text To Speech](#stream-text-to-speech) :new:
- [Stream Text To Speech](#stream-text-to-speech)
- [Voices](#voices)
- [Get All Voices](#get-all-voices)
- [Get Default Voice Settings](#get-default-voice-settings)
Expand All @@ -67,13 +67,13 @@ The recommended installation method is though the unity package manager and [Ope
- [Edit Voice](#edit-voice)
- [Delete Voice](#delete-voice)
- [Samples](#samples)
- [Download Voice Sample](#download-voice-sample) :new:
- [Download Voice Sample](#download-voice-sample)
- [Delete Voice Sample](#delete-voice-sample)
- [History](#history)
- [Get History](#get-history)
- [Get History Item](#get-history-item)
- [Download History Audio](#download-history-audio) :new:
- [Download History Items](#download-history-items) :new:
- [Download History Audio](#download-history-audio)
- [Download History Items](#download-history-items)
- [Delete History Item](#delete-history-item)
- [User](#user)
- [Get User Info](#get-user-info)
Expand All @@ -83,8 +83,10 @@ The recommended installation method is though the unity package manager and [Ope

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

1. [Pass keys directly with constructor](#pass-keys-directly-with-constructor)
2. [Unity Scriptable Object](#unity-scriptable-object)
: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.

1. [Pass keys directly with constructor](#pass-keys-directly-with-constructor) :warning:
2. [Unity Scriptable Object](#unity-scriptable-object) :warning:
3. [Load key from configuration file](#load-key-from-configuration-file)
4. [Use System Environment Variables](#use-system-environment-variables)

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

```csharp
var api = new ElevenLabsClient(ElevenLabsAuthentication.Default.LoadFromDirectory("your/path/to/.elevenlabs"));;
var api = new ElevenLabsClient(new ElevenLabsAuthentication().LoadFromDirectory("your/path/to/.elevenlabs"));;
```

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

```csharp
var api = new ElevenLabsClient(ElevenLabsAuthentication.Default.LoadFromEnvironment());
var api = new ElevenLabsClient(new ElevenLabsAuthentication().LoadFromEnvironment());
```

### [API Proxy](https://github.com/RageAgainstThePixel/ElevenLabs-DotNet/main/ElevenLabs-DotNet-Proxy/README.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ElevenLabs
/// <summary>
/// Represents authentication for ElevenLabs
/// </summary>
public sealed class ElevenLabsAuthentication : AbstractAuthentication<ElevenLabsAuthentication, ElevenLabsAuthInfo>
public sealed class ElevenLabsAuthentication : AbstractAuthentication<ElevenLabsAuthentication, ElevenLabsAuthInfo, ElevenLabsConfiguration>
{
internal const string CONFIG_FILE = ".elevenlabs";
private const string ELEVEN_LABS_API_KEY = nameof(ELEVEN_LABS_API_KEY);
Expand All @@ -23,30 +23,35 @@ public sealed class ElevenLabsAuthentication : AbstractAuthentication<ElevenLabs
public static implicit operator ElevenLabsAuthentication(string apiKey) => new ElevenLabsAuthentication(apiKey);

/// <summary>
/// Instantiates a new Authentication object that will load the default config.
/// Instantiates an empty Authentication object.
/// </summary>
public ElevenLabsAuthentication()
{
if (cachedDefault != null) { return; }

cachedDefault = (LoadFromAsset<ElevenLabsConfiguration>() ??
LoadFromDirectory()) ??
LoadFromDirectory(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)) ??
LoadFromEnvironment();
Info = cachedDefault?.Info;
}
public ElevenLabsAuthentication() { }

/// <summary>
/// Instantiates a new Authentication object with the given <paramref name="apiKey"/>, which may be <see langword="null"/>.
/// </summary>
/// <param name="apiKey">The API key, required to access the API endpoint.</param>
public ElevenLabsAuthentication(string apiKey) => Info = new ElevenLabsAuthInfo(apiKey);
public ElevenLabsAuthentication(string apiKey)
{
Info = new ElevenLabsAuthInfo(apiKey);
cachedDefault = this;
}

/// <summary>
/// Instantiates a new Authentication object with the given <paramref name="authInfo"/>, which may be <see langword="null"/>.
/// </summary>
/// <param name="authInfo"></param>
public ElevenLabsAuthentication(ElevenLabsAuthInfo authInfo) => this.Info = authInfo;
public ElevenLabsAuthentication(ElevenLabsAuthInfo authInfo)
{
Info = authInfo;
cachedDefault = this;
}

/// <summary>
/// Instantiates a new Authentication object with the given <see cref="configuration"/>.
/// </summary>
/// <param name="configuration"><see cref="ElevenLabsConfiguration"/>.</param>
public ElevenLabsAuthentication(ElevenLabsConfiguration configuration) : this(configuration.ApiKey) { }

/// <inheritdoc />
public override ElevenLabsAuthInfo Info { get; }
Expand All @@ -60,23 +65,21 @@ public ElevenLabsAuthentication()
/// </summary>
public static ElevenLabsAuthentication Default
{
get => cachedDefault ?? new ElevenLabsAuthentication();
get => cachedDefault ?? new ElevenLabsAuthentication().LoadDefault();
internal set => cachedDefault = value;
}

[Obsolete("Use ElevenLabsAuthentication.Info.ApiKey")]
public string ApiKey => Info.ApiKey;

/// <inheritdoc />
public override ElevenLabsAuthentication LoadFromAsset<T>()
=> Resources.LoadAll<T>(string.Empty)
.Where(asset => asset != null)
.Where(asset => asset is ElevenLabsConfiguration config &&
!string.IsNullOrWhiteSpace(config.ApiKey))
.Select(asset => asset is ElevenLabsConfiguration config
? new ElevenLabsAuthentication(config.ApiKey)
: null)
.FirstOrDefault();
public override ElevenLabsAuthentication LoadFromAsset(ElevenLabsConfiguration configuration = null)
{
if (configuration == null)
{
Debug.LogWarning($"This can be speed this up by passing a {nameof(ElevenLabsConfiguration)} to the {nameof(ElevenLabsAuthentication)}.ctr");
configuration = Resources.LoadAll<ElevenLabsConfiguration>(string.Empty).FirstOrDefault(o => o != null);
}

return configuration != null ? new ElevenLabsAuthentication(configuration) : null;
}

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

if (string.IsNullOrWhiteSpace(filename))
{
filename = CONFIG_FILE;
}

ElevenLabsAuthInfo tempAuthInfo = null;

var currentDirectory = new DirectoryInfo(directory);

while (tempAuthInfo == null && currentDirectory.Parent != null)
while (tempAuthInfo == null && currentDirectory?.Parent != null)
{
var filePath = Path.Combine(currentDirectory.FullName, filename);

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

switch (part)
apiKey = part switch
{
case ELEVEN_LABS_API_KEY:
apiKey = nextPart.Trim();
break;
}
ELEVEN_LABS_API_KEY => nextPart.Trim(),
_ => apiKey
};
}
}

Expand All @@ -148,16 +155,7 @@ public override ElevenLabsAuthentication LoadFromDirectory(string directory = nu
}
}

if (tempAuthInfo == null ||
string.IsNullOrEmpty(tempAuthInfo.ApiKey))
{
return null;
}

return new ElevenLabsAuthentication(tempAuthInfo);
return string.IsNullOrEmpty(tempAuthInfo?.ApiKey) ? null : new ElevenLabsAuthentication(tempAuthInfo);
}

[Obsolete("use ElevenLabsAuthentication.Default.LoadFromEnvironment")]
public static ElevenLabsAuthentication LoadFromEnv() => Default.LoadFromEnvironment();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public sealed class ElevenLabsClient : BaseClient<ElevenLabsAuthentication, Elev
/// <summary>
/// Creates a new client for the ElevenLabs API, handling auth and allowing for access to various API endpoints.
/// </summary>
/// <param name="elevenLabsAuthentication">The API authentication information to use for API calls,
/// <param name="authentication">The API authentication information to use for API calls,
/// or <see langword="null"/> to attempt to use the <see cref="ElevenLabsAuthentication.Default"/>,
/// potentially loading from environment vars or from a config file.</param>
/// <param name="clientSettings">Optional, <see cref="ElevenLabsClientSettings"/> for specifying a proxy domain.</param>
/// <param name="settings">Optional, <see cref="ElevenLabsSettings"/> for specifying a proxy domain.</param>
/// <exception cref="AuthenticationException">Raised when authentication details are missing or invalid.</exception>
public ElevenLabsClient(ElevenLabsAuthentication elevenLabsAuthentication = null, ElevenLabsSettings clientSettings = null)
: base(elevenLabsAuthentication ?? ElevenLabsAuthentication.Default, clientSettings ?? ElevenLabsSettings.Default)
public ElevenLabsClient(ElevenLabsAuthentication authentication = null, ElevenLabsSettings settings = null)
: base(authentication ?? ElevenLabsAuthentication.Default, settings ?? ElevenLabsSettings.Default)
{
UserEndpoint = new UserEndpoint(this);
VoicesEndpoint = new VoicesEndpoint(this);
Expand All @@ -33,6 +33,7 @@ public ElevenLabsClient(ElevenLabsAuthentication elevenLabsAuthentication = null
TextToSpeechEndpoint = new TextToSpeechEndpoint(this);
VoiceGenerationEndpoint = new VoiceGenerationEndpoint(this);
}

protected override void SetupDefaultRequestHeaders()
{
var headers = new Dictionary<string, string>
Expand All @@ -48,6 +49,11 @@ protected override void SetupDefaultRequestHeaders()

protected override void ValidateAuthentication()
{
if (Authentication?.Info == null)
{
throw new InvalidCredentialException($"Invalid {nameof(ElevenLabsAuthentication)}");
}

if (!HasValidAuthentication)
{
throw new AuthenticationException("You must provide API authentication. Please refer to https://github.com/RageAgainstThePixel/com.rest.elevenlabs#authentication for details.");
Expand Down

This file was deleted.

0 comments on commit bab230f

Please sign in to comment.