11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
34using System . Net ;
45using System . Net . Http ;
56using System . Net . Http . Headers ;
7+ using System . Net . Http . Json ;
68using System . Runtime . CompilerServices ;
9+ using System . Text ;
710using System . Text . Json ;
811using System . Text . Json . Serialization ;
912using System . Threading ;
@@ -13,6 +16,8 @@ namespace Beefweb.Client.Infrastructure;
1316
1417internal sealed class RequestHandler : IRequestHandler
1518{
19+ private static readonly Encoding Utf8 = new UTF8Encoding ( false ) ;
20+ private static readonly Type StringType = typeof ( string ) ;
1621 private static readonly byte [ ] EventPrefix = "data:"u8 . ToArray ( ) ;
1722
1823 internal static readonly JsonSerializerOptions SerializerOptions = CreateSerializerOptions ( ) ;
@@ -132,9 +137,10 @@ public async IAsyncEnumerable<object> GetEvents(
132137 var requestUri = UriFormatter . Format ( _baseUri , url ) ;
133138 using var request = new HttpRequestMessage ( HttpMethod . Post , requestUri ) ;
134139
135- request . Content = body != null
136- ? CreateContent ( ContentTypes . Json , JsonSerializer . SerializeToUtf8Bytes ( body , SerializerOptions ) )
137- : CreateContent ( ContentTypes . Text , [ ] ) ;
140+ if ( returnType != null )
141+ request . Headers . Accept . Add ( new MediaTypeWithQualityHeaderValue ( ContentTypes . Json ) ) ;
142+
143+ request . Content = GetContent ( ) ;
138144
139145 using var response = await _client
140146 . SendAsync ( request , HttpCompletionOption . ResponseContentRead , cancellationToken )
@@ -146,25 +152,35 @@ public async IAsyncEnumerable<object> GetEvents(
146152 ? await ParseResponse ( response , returnType , cancellationToken ) . ConfigureAwait ( false )
147153 : null ;
148154
149- static ByteArrayContent CreateContent ( string type , byte [ ] data ) =>
150- new ( data ) { Headers = { ContentType = new MediaTypeHeaderValue ( type ) } } ;
155+ HttpContent GetContent ( )
156+ {
157+ if ( body is string str )
158+ return new StringContent ( str , Utf8 , new MediaTypeHeaderValue ( ContentTypes . Json , "utf-8" ) ) ;
159+
160+ if ( body != null )
161+ return JsonContent . Create ( body , body . GetType ( ) , options : SerializerOptions ) ;
162+
163+ return new ByteArrayContent ( [ ] ) ;
164+ }
151165 }
152166
153167 private static async ValueTask < object > ParseResponse (
154168 HttpResponseMessage response , Type returnType , CancellationToken cancellationToken )
155169 {
156- var responseStream = await response . Content . ReadAsStreamAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
157- await using var responseStreamScope = responseStream . ConfigureAwait ( false ) ;
170+ if ( returnType == StringType )
171+ {
172+ return response . Content . ReadAsStringAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
173+ }
158174
159- var result = await JsonSerializer
160- . DeserializeAsync ( responseStream , returnType , SerializerOptions , cancellationToken )
175+ var result = await response . Content
176+ . ReadFromJsonAsync ( returnType , SerializerOptions , cancellationToken )
161177 . ConfigureAwait ( false ) ;
162178
163179 return result ?? throw InvalidResponse ( ) ;
164180 }
165181
166- private static InvalidOperationException InvalidResponse ( )
182+ private static InvalidDataException InvalidResponse ( )
167183 {
168- return new InvalidOperationException ( "Invalid response: expected JSON value ." ) ;
184+ return new InvalidDataException ( "Invalid response: expected JSON object ." ) ;
169185 }
170186}
0 commit comments