about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs
diff options
context:
space:
mode:
authorEmma@Rory& <root@rory.gay>2023-07-24 20:56:42 +0200
committerEmma@Rory& <root@rory.gay>2023-07-24 20:56:42 +0200
commit5495a35403d285007d67f503042720302efdf94c (patch)
tree3db4dc545ce3ad252f82a577ecad4150320c1ba3 /MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs
parentChange max line width from 120 to 180 (diff)
downloadMatrixUtils-5495a35403d285007d67f503042720302efdf94c.tar.xz
Code cleanup
Diffstat (limited to '')
-rw-r--r--MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs28
1 files changed, 15 insertions, 13 deletions
diff --git a/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs b/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs

index 695e8e3..9ac9c6b 100644 --- a/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs +++ b/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs
@@ -19,40 +19,42 @@ public static class HttpClientExtensions { } public class MatrixHttpClient : HttpClient { - public override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { - Console.WriteLine($"Sending request to {request.RequestUri}"); - try - { - HttpRequestOptionsKey<bool> WebAssemblyEnableStreamingResponseKey = new HttpRequestOptionsKey<bool>("WebAssemblyEnableStreamingResponse"); + public override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, + CancellationToken cancellationToken) { + Console.WriteLine($"Sending request to {request.RequestUri}"); + try { + HttpRequestOptionsKey<bool> WebAssemblyEnableStreamingResponseKey = + new HttpRequestOptionsKey<bool>("WebAssemblyEnableStreamingResponse"); request.Options.Set(WebAssemblyEnableStreamingResponseKey, true); - // var asm = Assembly.Load("Microsoft.AspNetCore.Components.WebAssembly"); - // var browserHttpHandlerType = asm.GetType("Microsoft.AspNetCore.Components.WebAssembly.Http.WebAssemblyHttpRequestMessageExtensions", true); - // var browserHttpHandlerMethod = browserHttpHandlerType.GetMethod("SetBrowserResponseStreamingEnabled", BindingFlags.Public | BindingFlags.Static); - // browserHttpHandlerMethod?.Invoke(null, new object[] {request, true}); } - catch (Exception e) - { + catch (Exception e) { Console.WriteLine("Failed to set browser response streaming:"); Console.WriteLine(e); } + var a = await base.SendAsync(request, cancellationToken); if (!a.IsSuccessStatusCode) { var content = await a.Content.ReadAsStringAsync(cancellationToken); if (content.StartsWith('{')) { var ex = JsonSerializer.Deserialize<MatrixException>(content); ex.RawContent = content; - Console.WriteLine($"Failed to send request: {ex}"); + Console.WriteLine($"Failed to send request: {ex}"); if (ex?.RetryAfterMs is not null) { await Task.Delay(ex.RetryAfterMs.Value, cancellationToken); - typeof(HttpRequestMessage).GetField("_sendStatus", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(request, 0); + typeof(HttpRequestMessage).GetField("_sendStatus", BindingFlags.NonPublic | BindingFlags.Instance) + ?.SetValue(request, 0); return await SendAsync(request, cancellationToken); } + throw ex!; } + throw new InvalidDataException("Encountered invalid data:\n" + content); } + return a; } + // GetFromJsonAsync public async Task<T> GetFromJsonAsync<T>(string requestUri, CancellationToken cancellationToken = default) { var request = new HttpRequestMessage(HttpMethod.Get, requestUri);