@@ -11,7 +11,7 @@ namespace ElevenLabs
11
11
/// <summary>
12
12
/// Represents authentication for ElevenLabs
13
13
/// </summary>
14
- public sealed class ElevenLabsAuthentication : AbstractAuthentication < ElevenLabsAuthentication , ElevenLabsAuthInfo >
14
+ public sealed class ElevenLabsAuthentication : AbstractAuthentication < ElevenLabsAuthentication , ElevenLabsAuthInfo , ElevenLabsConfiguration >
15
15
{
16
16
internal const string CONFIG_FILE = ".elevenlabs" ;
17
17
private const string ELEVEN_LABS_API_KEY = nameof ( ELEVEN_LABS_API_KEY ) ;
@@ -23,30 +23,35 @@ public sealed class ElevenLabsAuthentication : AbstractAuthentication<ElevenLabs
23
23
public static implicit operator ElevenLabsAuthentication ( string apiKey ) => new ElevenLabsAuthentication ( apiKey ) ;
24
24
25
25
/// <summary>
26
- /// Instantiates a new Authentication object that will load the default config .
26
+ /// Instantiates an empty Authentication object.
27
27
/// </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 ( ) { }
38
29
39
30
/// <summary>
40
31
/// Instantiates a new Authentication object with the given <paramref name="apiKey"/>, which may be <see langword="null"/>.
41
32
/// </summary>
42
33
/// <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
+ }
44
39
45
40
/// <summary>
46
41
/// Instantiates a new Authentication object with the given <paramref name="authInfo"/>, which may be <see langword="null"/>.
47
42
/// </summary>
48
43
/// <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 ) { }
50
55
51
56
/// <inheritdoc />
52
57
public override ElevenLabsAuthInfo Info { get ; }
@@ -60,23 +65,21 @@ public ElevenLabsAuthentication()
60
65
/// </summary>
61
66
public static ElevenLabsAuthentication Default
62
67
{
63
- get => cachedDefault ?? new ElevenLabsAuthentication ( ) ;
68
+ get => cachedDefault ?? new ElevenLabsAuthentication ( ) . LoadDefault ( ) ;
64
69
internal set => cachedDefault = value ;
65
70
}
66
71
67
- [ Obsolete ( "Use ElevenLabsAuthentication.Info.ApiKey" ) ]
68
- public string ApiKey => Info . ApiKey ;
69
-
70
72
/// <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
+ }
80
83
81
84
/// <inheritdoc />
82
85
public override ElevenLabsAuthentication LoadFromEnvironment ( )
@@ -94,11 +97,16 @@ public override ElevenLabsAuthentication LoadFromDirectory(string directory = nu
94
97
directory = Environment . CurrentDirectory ;
95
98
}
96
99
100
+ if ( string . IsNullOrWhiteSpace ( filename ) )
101
+ {
102
+ filename = CONFIG_FILE ;
103
+ }
104
+
97
105
ElevenLabsAuthInfo tempAuthInfo = null ;
98
106
99
107
var currentDirectory = new DirectoryInfo ( directory ) ;
100
108
101
- while ( tempAuthInfo == null && currentDirectory . Parent != null )
109
+ while ( tempAuthInfo == null && currentDirectory ? . Parent != null )
102
110
{
103
111
var filePath = Path . Combine ( currentDirectory . FullName , filename ) ;
104
112
@@ -126,12 +134,11 @@ public override ElevenLabsAuthentication LoadFromDirectory(string directory = nu
126
134
var part = parts [ i ] ;
127
135
var nextPart = parts [ i + 1 ] ;
128
136
129
- switch ( part )
137
+ apiKey = part switch
130
138
{
131
- case ELEVEN_LABS_API_KEY :
132
- apiKey = nextPart . Trim ( ) ;
133
- break ;
134
- }
139
+ ELEVEN_LABS_API_KEY => nextPart . Trim ( ) ,
140
+ _ => apiKey
141
+ } ;
135
142
}
136
143
}
137
144
@@ -148,16 +155,7 @@ public override ElevenLabsAuthentication LoadFromDirectory(string directory = nu
148
155
}
149
156
}
150
157
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 ) ;
158
159
}
159
-
160
- [ Obsolete ( "use ElevenLabsAuthentication.Default.LoadFromEnvironment" ) ]
161
- public static ElevenLabsAuthentication LoadFromEnv ( ) => Default . LoadFromEnvironment ( ) ;
162
160
}
163
161
}
0 commit comments