44using System . Runtime . CompilerServices ;
55using System . Text ;
66using System . Text . Json ;
7- using System . Text . Json . Serialization ;
87using Cnblogs . DashScope . Core . Internals ;
98
109namespace Cnblogs . DashScope . Core ;
@@ -14,22 +13,18 @@ namespace Cnblogs.DashScope.Core;
1413/// </summary>
1514public class DashScopeClientCore : IDashScopeClient
1615{
17- private static readonly JsonSerializerOptions SerializationOptions =
18- new ( )
19- {
20- DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull ,
21- PropertyNamingPolicy = JsonNamingPolicy . SnakeCaseLower
22- } ;
23-
2416 private readonly HttpClient _httpClient ;
17+ private readonly DashScopeClientWebSocketPool _socketPool ;
2518
2619 /// <summary>
2720 /// For DI container to inject pre-configured httpclient.
2821 /// </summary>
2922 /// <param name="httpClient">Pre-configured httpclient.</param>
30- public DashScopeClientCore ( HttpClient httpClient )
23+ /// <param name="pool">Websocket pool.</param>
24+ public DashScopeClientCore ( HttpClient httpClient , DashScopeClientWebSocketPool pool )
3125 {
3226 _httpClient = httpClient ;
27+ _socketPool = pool ;
3328 }
3429
3530 /// <inheritdoc />
@@ -283,6 +278,15 @@ public async Task<DashScopeDeleteFileResult> DeleteFileAsync(
283278 return ( await SendCompatibleAsync < DashScopeDeleteFileResult > ( request , cancellationToken ) ) ! ;
284279 }
285280
281+ /// <inheritdoc />
282+ public async Task < SpeechSynthesizerSocketSession > CreateSpeechSynthesizerSocketSessionAsync (
283+ string modelId ,
284+ CancellationToken cancellationToken = default )
285+ {
286+ var socket = await _socketPool . RentSocketAsync < SpeechSynthesizerOutput > ( cancellationToken ) ;
287+ return new SpeechSynthesizerSocketSession ( socket , modelId ) ;
288+ }
289+
286290 private static HttpRequestMessage BuildSseRequest < TPayload > ( HttpMethod method , string url , TPayload payload )
287291 where TPayload : class
288292 {
@@ -304,7 +308,9 @@ private static HttpRequestMessage BuildRequest<TPayload>(
304308 {
305309 var message = new HttpRequestMessage ( method , url )
306310 {
307- Content = payload != null ? JsonContent . Create ( payload , options : SerializationOptions ) : null
311+ Content = payload != null
312+ ? JsonContent . Create ( payload , options : DashScopeDefaults . SerializationOptions )
313+ : null
308314 } ;
309315
310316 if ( sse )
@@ -340,7 +346,9 @@ private static HttpRequestMessage BuildRequest<TPayload>(
340346 } ,
341347 HttpCompletionOption . ResponseContentRead ,
342348 cancellationToken ) ;
343- return await response . Content . ReadFromJsonAsync < TResponse > ( SerializationOptions , cancellationToken ) ;
349+ return await response . Content . ReadFromJsonAsync < TResponse > (
350+ DashScopeDefaults . SerializationOptions ,
351+ cancellationToken ) ;
344352 }
345353
346354 private async Task < TResponse ? > SendAsync < TResponse > ( HttpRequestMessage message , CancellationToken cancellationToken )
@@ -350,7 +358,9 @@ private static HttpRequestMessage BuildRequest<TPayload>(
350358 message ,
351359 HttpCompletionOption . ResponseContentRead ,
352360 cancellationToken ) ;
353- return await response . Content . ReadFromJsonAsync < TResponse > ( SerializationOptions , cancellationToken ) ;
361+ return await response . Content . ReadFromJsonAsync < TResponse > (
362+ DashScopeDefaults . SerializationOptions ,
363+ cancellationToken ) ;
354364 }
355365
356366 private async IAsyncEnumerable < TResponse > StreamAsync < TResponse > (
@@ -373,15 +383,16 @@ private async IAsyncEnumerable<TResponse> StreamAsync<TResponse>(
373383 var data = line [ "data:" . Length ..] ;
374384 if ( data . StartsWith ( "{\" code\" :" ) )
375385 {
376- var error = JsonSerializer . Deserialize < DashScopeError > ( data , SerializationOptions ) ! ;
386+ var error =
387+ JsonSerializer . Deserialize < DashScopeError > ( data , DashScopeDefaults . SerializationOptions ) ! ;
377388 throw new DashScopeException (
378389 message . RequestUri ? . ToString ( ) ,
379390 ( int ) response . StatusCode ,
380391 error ,
381392 error . Message ) ;
382393 }
383394
384- yield return JsonSerializer . Deserialize < TResponse > ( data , SerializationOptions ) ! ;
395+ yield return JsonSerializer . Deserialize < TResponse > ( data , DashScopeDefaults . SerializationOptions ) ! ;
385396 }
386397 }
387398 }
@@ -418,7 +429,9 @@ private async Task<HttpResponseMessage> GetSuccessResponseAsync<TError>(
418429 DashScopeError ? error = null ;
419430 try
420431 {
421- var r = await response . Content . ReadFromJsonAsync < TError > ( SerializationOptions , cancellationToken ) ;
432+ var r = await response . Content . ReadFromJsonAsync < TError > (
433+ DashScopeDefaults . SerializationOptions ,
434+ cancellationToken ) ;
422435 error = r == null ? null : errorMapper . Invoke ( r ) ;
423436 }
424437 catch ( Exception )
0 commit comments