Skip to content

Commit b6b9f37

Browse files
ElevenLabs-DotNet 3.5.1 (#87)
- Fix some proxy support isssues. ## ElevenLabs-DotNet-Proxy 3.5.1 - Bug fixes and better logging support.
1 parent fca4904 commit b6b9f37

File tree

7 files changed

+55
-34
lines changed

7 files changed

+55
-34
lines changed

ElevenLabs-DotNet-Proxy/ElevenLabs-DotNet-Proxy.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
2121
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
2222
<SignAssembly>false</SignAssembly>
23-
<Version>3.0.0</Version>
23+
<Version>3.5.1</Version>
2424
<PackageReleaseNotes>
25+
Version 3.5.1
26+
- Bug fixes and better logging support.
2527
Version 3.0.0
2628
- Renamed ElevenLabsProxyStartup to ElevenLabsProxy
2729
- Deprecated ValidateAuthentication

ElevenLabs-DotNet-Proxy/Proxy/ElevenLabsProxy.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Server.Kestrel.Core;
77
using Microsoft.Extensions.DependencyInjection;
88
using Microsoft.Extensions.Hosting;
9+
using Microsoft.Extensions.Logging;
910
using System;
1011
using System.Threading.Tasks;
1112

@@ -62,6 +63,12 @@ public static IHost CreateDefaultHost<T>(string[] args, ElevenLabsClient elevenL
6263
webBuilder.UseStartup<ElevenLabsProxy>();
6364
webBuilder.ConfigureKestrel(ConfigureKestrel);
6465
})
66+
.ConfigureLogging(logger =>
67+
{
68+
logger.ClearProviders();
69+
logger.AddConsole();
70+
logger.SetMinimumLevel(LogLevel.Debug);
71+
})
6572
.ConfigureServices(services =>
6673
{
6774
services.AddSingleton(elevenLabsClient);
@@ -77,6 +84,9 @@ public static IHost CreateDefaultHost<T>(string[] args, ElevenLabsClient elevenL
7784
public static WebApplication CreateWebApplication<T>(string[] args, ElevenLabsClient elevenLabsClient) where T : class, IAuthenticationFilter
7885
{
7986
var builder = WebApplication.CreateBuilder(args);
87+
builder.Logging.ClearProviders();
88+
builder.Logging.AddConsole();
89+
builder.Logging.SetMinimumLevel(LogLevel.Debug);
8090
builder.WebHost.ConfigureKestrel(ConfigureKestrel);
8191
builder.Services.AddSingleton(elevenLabsClient);
8292
builder.Services.AddSingleton<IAuthenticationFilter, T>();

ElevenLabs-DotNet-Proxy/Proxy/EndpointRouteBuilder.cs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
using Microsoft.AspNetCore.Builder;
44
using Microsoft.AspNetCore.Http;
55
using Microsoft.AspNetCore.Routing;
6+
using Microsoft.AspNetCore.WebUtilities;
67
using Microsoft.Net.Http.Headers;
78
using System;
89
using System.Collections.Generic;
910
using System.IO;
1011
using System.Linq;
1112
using System.Net.Http;
13+
using System.Net.WebSockets;
1214
using System.Security.Authentication;
1315
using System.Text.Json;
1416
using System.Threading.Tasks;
@@ -46,42 +48,42 @@ public static class EndpointRouteBuilder
4648
/// Maps the <see cref="ElevenLabsClient"/> endpoints.
4749
/// </summary>
4850
/// <param name="endpoints"><see cref="IEndpointRouteBuilder"/>.</param>
49-
/// <param name="elevenLabsClient"><see cref="ElevenLabsClient"/>.</param>
51+
/// <param name="client"><see cref="ElevenLabsClient"/>.</param>
5052
/// <param name="authenticationFilter"><see cref="IAuthenticationFilter"/>.</param>
5153
/// <param name="routePrefix">Optional, custom route prefix. i.e. '/elevenlabs'.</param>
52-
public static void MapElevenLabsEndpoints(this IEndpointRouteBuilder endpoints, ElevenLabsClient elevenLabsClient, IAuthenticationFilter authenticationFilter, string routePrefix = "")
54+
public static void MapElevenLabsEndpoints(this IEndpointRouteBuilder endpoints, ElevenLabsClient client, IAuthenticationFilter authenticationFilter, string routePrefix = "")
5355
{
54-
endpoints.Map($"{routePrefix}{elevenLabsClient.ElevenLabsClientSettings.BaseRequest}{{**endpoint}}", HandleRequest);
56+
endpoints.Map($"{routePrefix}{client.Settings.BaseRequest}{{**endpoint}}", HandleRequest);
57+
return;
5558

5659
async Task HandleRequest(HttpContext httpContext, string endpoint)
5760
{
5861
try
5962
{
60-
#pragma warning disable CS0618 // Type or member is obsolete
61-
// ReSharper disable once MethodHasAsyncOverload
62-
authenticationFilter.ValidateAuthentication(httpContext.Request.Headers);
63-
#pragma warning restore CS0618 // Type or member is obsolete
64-
await authenticationFilter.ValidateAuthenticationAsync(httpContext.Request.Headers);
65-
63+
await authenticationFilter.ValidateAuthenticationAsync(httpContext.Request.Headers).ConfigureAwait(false);
6664
var method = new HttpMethod(httpContext.Request.Method);
67-
var uri = new Uri(string.Format(
68-
elevenLabsClient.ElevenLabsClientSettings.BaseRequestUrlFormat,
69-
$"{endpoint}{httpContext.Request.QueryString}"
70-
));
71-
using var request = new HttpRequestMessage(method, uri);
72-
request.Content = new StreamContent(httpContext.Request.Body);
65+
var originalQuery = QueryHelpers.ParseQuery(httpContext.Request.QueryString.Value ?? "");
66+
var modifiedQuery = new Dictionary<string, string>(originalQuery.Count);
7367

74-
if (httpContext.Request.Body.CanSeek)
68+
foreach (var pair in originalQuery)
7569
{
76-
httpContext.Request.Body.Position = 0;
70+
modifiedQuery[pair.Key] = pair.Value.FirstOrDefault();
7771
}
7872

73+
var uri = new Uri(string.Format(
74+
client.Settings.BaseRequestUrlFormat,
75+
QueryHelpers.AddQueryString(endpoint, modifiedQuery)
76+
));
77+
78+
using var request = new HttpRequestMessage(method, uri);
79+
request.Content = new StreamContent(httpContext.Request.Body);
80+
7981
if (httpContext.Request.ContentType != null)
8082
{
8183
request.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(httpContext.Request.ContentType);
8284
}
8385

84-
var proxyResponse = await elevenLabsClient.Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
86+
var proxyResponse = await client.Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, httpContext.RequestAborted).ConfigureAwait(false);
8587
httpContext.Response.StatusCode = (int)proxyResponse.StatusCode;
8688

8789
foreach (var (key, value) in proxyResponse.Headers)
@@ -101,32 +103,37 @@ async Task HandleRequest(HttpContext httpContext, string endpoint)
101103

102104
if (httpContext.Response.ContentType.Equals(streamingContent))
103105
{
104-
using var reader = new StreamReader(await request.Content.ReadAsStreamAsync());
105-
var stream = await proxyResponse.Content.ReadAsStreamAsync();
106-
await WriteServerStreamEventsAsync(httpContext, stream);
106+
var stream = await proxyResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);
107+
await WriteServerStreamEventsAsync(httpContext, stream).ConfigureAwait(false);
107108
}
108109
else
109110
{
110-
await proxyResponse.Content.CopyToAsync(httpContext.Response.Body);
111+
await proxyResponse.Content.CopyToAsync(httpContext.Response.Body, httpContext.RequestAborted).ConfigureAwait(false);
111112
}
112113
}
113114
catch (AuthenticationException authenticationException)
114115
{
115116
httpContext.Response.StatusCode = StatusCodes.Status401Unauthorized;
116-
await httpContext.Response.WriteAsync(authenticationException.Message);
117+
await httpContext.Response.WriteAsync(authenticationException.Message).ConfigureAwait(false);
118+
}
119+
catch (WebSocketException)
120+
{
121+
// ignore
122+
throw;
117123
}
118124
catch (Exception e)
119125
{
126+
if (httpContext.Response.HasStarted) { throw; }
120127
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
121-
var response = JsonSerializer.Serialize(new { error = new { message = e.Message, stackTrace = e.StackTrace } });
122-
await httpContext.Response.WriteAsync(response);
128+
var response = JsonSerializer.Serialize(new { error = new { e.Message, e.StackTrace } });
129+
await httpContext.Response.WriteAsync(response).ConfigureAwait(false);
123130
}
124131

125132
static async Task WriteServerStreamEventsAsync(HttpContext httpContext, Stream contentStream)
126133
{
127134
var responseStream = httpContext.Response.Body;
128-
await contentStream.CopyToAsync(responseStream, httpContext.RequestAborted);
129-
await responseStream.FlushAsync(httpContext.RequestAborted);
135+
await contentStream.CopyToAsync(responseStream, httpContext.RequestAborted).ConfigureAwait(false);
136+
await responseStream.FlushAsync(httpContext.RequestAborted).ConfigureAwait(false);
130137
}
131138
}
132139
}

ElevenLabs-DotNet-Tests/TestFixture_00_Authentication.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ public void Test_09_CustomDomainConfigurationSettings()
112112
var auth = new ElevenLabsAuthentication("customIssuedToken");
113113
var settings = new ElevenLabsClientSettings(domain: "api.your-custom-domain.com");
114114
var api = new ElevenLabsClient(auth, settings);
115-
Console.WriteLine(api.ElevenLabsClientSettings.BaseRequest);
116-
Console.WriteLine(api.ElevenLabsClientSettings.BaseRequestUrlFormat);
115+
Console.WriteLine(api.Settings.BaseRequest);
116+
Console.WriteLine(api.Settings.BaseRequestUrlFormat);
117117
}
118118

119119
[TearDown]

ElevenLabs-DotNet/Common/ElevenLabsBaseEndPoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public abstract class ElevenLabsBaseEndPoint
2424
/// <param name="queryParameters">Optional, parameters to add to the endpoint.</param>
2525
protected string GetUrl(string endpoint = "", Dictionary<string, string> queryParameters = null)
2626
{
27-
var result = string.Format(client.ElevenLabsClientSettings.BaseRequestUrlFormat, $"{Root}{endpoint}");
27+
var result = string.Format(client.Settings.BaseRequestUrlFormat, $"{Root}{endpoint}");
2828

2929
if (queryParameters is { Count: not 0 })
3030
{

ElevenLabs-DotNet/ElevenLabs-DotNet.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ All copyrights, trademarks, logos, and assets are the property of their respecti
2525
<SignAssembly>false</SignAssembly>
2626
<IncludeSymbols>true</IncludeSymbols>
2727
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
28-
<Version>3.5.0</Version>
28+
<Version>3.5.1</Version>
2929
<PackageReleaseNotes>
30+
Version 3.5.1
31+
- Fix some proxy support issues.
3032
Version 3.5.0
3133
- Added TextToSpeechRequest.ctr overload
3234
- Added seed property

ElevenLabs-DotNet/ElevenLabsClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public sealed class ElevenLabsClient : IDisposable
3939
public ElevenLabsClient(ElevenLabsAuthentication authentication = null, ElevenLabsClientSettings settings = null, HttpClient httpClient = null)
4040
{
4141
ElevenLabsAuthentication = authentication ?? ElevenLabsAuthentication.Default;
42-
ElevenLabsClientSettings = settings ?? ElevenLabsClientSettings.Default;
42+
Settings = settings ?? ElevenLabsClientSettings.Default;
4343

4444
if (string.IsNullOrWhiteSpace(ElevenLabsAuthentication?.ApiKey))
4545
{
@@ -128,7 +128,7 @@ private void Dispose(bool disposing)
128128
/// </summary>
129129
public ElevenLabsAuthentication ElevenLabsAuthentication { get; }
130130

131-
internal ElevenLabsClientSettings ElevenLabsClientSettings { get; }
131+
internal ElevenLabsClientSettings Settings { get; }
132132

133133
public UserEndpoint UserEndpoint { get; }
134134

0 commit comments

Comments
 (0)