about summary refs log tree commit diff
path: root/LibMatrix/Extensions
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--LibMatrix/Extensions/DictionaryExtensions.cs3
-rw-r--r--LibMatrix/Extensions/HttpClientExtensions.cs38
-rw-r--r--LibMatrix/Extensions/JsonElementExtensions.cs4
3 files changed, 21 insertions, 24 deletions
diff --git a/LibMatrix/Extensions/DictionaryExtensions.cs b/LibMatrix/Extensions/DictionaryExtensions.cs
index fbc5cf5..f01cf68 100644
--- a/LibMatrix/Extensions/DictionaryExtensions.cs
+++ b/LibMatrix/Extensions/DictionaryExtensions.cs
@@ -3,8 +3,7 @@ namespace LibMatrix.Extensions;
 public static class DictionaryExtensions {
     public static bool ChangeKey<TKey, TValue>(this IDictionary<TKey, TValue> dict,
         TKey oldKey, TKey newKey) {
-        TValue value;
-        if (!dict.Remove(oldKey, out value))
+        if (!dict.Remove(oldKey, out var value))
             return false;
 
         dict[newKey] = value; // or dict.Add(newKey, value) depending on ur comfort
diff --git a/LibMatrix/Extensions/HttpClientExtensions.cs b/LibMatrix/Extensions/HttpClientExtensions.cs
index 797a077..d4017ed 100644
--- a/LibMatrix/Extensions/HttpClientExtensions.cs
+++ b/LibMatrix/Extensions/HttpClientExtensions.cs
@@ -1,3 +1,4 @@
+using System.Diagnostics;
 using System.Net.Http.Headers;
 using System.Reflection;
 using System.Text.Json;
@@ -23,7 +24,7 @@ public class MatrixHttpClient : HttpClient {
         CancellationToken cancellationToken) {
         Console.WriteLine($"Sending request to {request.RequestUri}");
         try {
-            HttpRequestOptionsKey<bool> WebAssemblyEnableStreamingResponseKey =
+            var WebAssemblyEnableStreamingResponseKey =
                 new HttpRequestOptionsKey<bool>("WebAssemblyEnableStreamingResponse");
             request.Options.Set(WebAssemblyEnableStreamingResponseKey, true);
         }
@@ -33,26 +34,23 @@ public class MatrixHttpClient : HttpClient {
         }
 
         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}");
-                if (ex?.RetryAfterMs is not null) {
-                    await Task.Delay(ex.RetryAfterMs.Value, cancellationToken);
-                    typeof(HttpRequestMessage).GetField("_sendStatus", BindingFlags.NonPublic | BindingFlags.Instance)
-                        ?.SetValue(request, 0);
-                    return await SendAsync(request, cancellationToken);
-                }
+        if (a.IsSuccessStatusCode) return a;
 
-                throw ex!;
-            }
+        //error handling
+        var content = await a.Content.ReadAsStringAsync(cancellationToken);
+        if (!content.StartsWith('{')) throw new InvalidDataException("Encountered invalid data:\n" + content);
+        //we have a matrix error
+        var ex = JsonSerializer.Deserialize<MatrixException>(content);
+        Debug.Assert(ex != null, nameof(ex) + " != null");
+        ex.RawContent = content;
+        // Console.WriteLine($"Failed to send request: {ex}");
+        if (ex?.RetryAfterMs is null) throw ex!;
+        //we have a ratelimit error
+        await Task.Delay(ex.RetryAfterMs.Value, cancellationToken);
+        typeof(HttpRequestMessage).GetField("_sendStatus", BindingFlags.NonPublic | BindingFlags.Instance)
+            ?.SetValue(request, 0);
+        return await SendAsync(request, cancellationToken);
 
-            throw new InvalidDataException("Encountered invalid data:\n" + content);
-        }
-
-        return a;
     }
 
     // GetFromJsonAsync
@@ -66,7 +64,7 @@ public class MatrixHttpClient : HttpClient {
     }
 
     // GetStreamAsync
-    public async Task<Stream> GetStreamAsync(string requestUri, CancellationToken cancellationToken = default) {
+    public new async Task<Stream> GetStreamAsync(string requestUri, CancellationToken cancellationToken = default) {
         var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
         request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
         var response = await SendAsync(request, cancellationToken);
diff --git a/LibMatrix/Extensions/JsonElementExtensions.cs b/LibMatrix/Extensions/JsonElementExtensions.cs
index caf96e1..f39f300 100644
--- a/LibMatrix/Extensions/JsonElementExtensions.cs
+++ b/LibMatrix/Extensions/JsonElementExtensions.cs
@@ -14,7 +14,7 @@ public static class JsonElementExtensions {
         //     return false;
 
         Console.WriteLine($"{objectType.Name} {objectPropertyName}");
-        bool unknownPropertyFound = false;
+        var unknownPropertyFound = false;
         var mappedPropsDict = objectType.GetProperties()
             .Where(x => x.GetCustomAttribute<JsonPropertyNameAttribute>() is not null)
             .ToDictionary(x => x.GetCustomAttribute<JsonPropertyNameAttribute>()!.Name, x => x);
@@ -61,7 +61,7 @@ public static class JsonElementExtensions {
             propertyType = propertyType.GetGenericArguments()[0];
         }
 
-        bool switchResult = false;
+        var switchResult = false;
         switch (field.Value.ValueKind) {
             case JsonValueKind.Array:
                 switchResult = field.Value.EnumerateArray().Aggregate(switchResult,