about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Extensions
diff options
context:
space:
mode:
Diffstat (limited to 'MatrixRoomUtils.Core/Extensions')
-rw-r--r--MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs22
-rw-r--r--MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs11
2 files changed, 31 insertions, 2 deletions
diff --git a/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs b/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs
index c51baec..78bbdfa 100644
--- a/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs
+++ b/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs
@@ -10,4 +10,24 @@ public static class DictionaryExtensions {
         dict[newKey] = value; // or dict.Add(newKey, value) depending on ur comfort
         return true;
     }
-}
\ No newline at end of file
+
+    public static Y GetOrCreate<X, Y>(this IDictionary<X, Y> dict, X key) where Y : new() {
+        if (dict.TryGetValue(key, out var value)) {
+            return value;
+        }
+
+        value = new Y();
+        dict.Add(key, value);
+        return value;
+    }
+
+    public static Y GetOrCreate<X, Y>(this IDictionary<X, Y> dict, X key, Func<X, Y> valueFactory) {
+        if (dict.TryGetValue(key, out var value)) {
+            return value;
+        }
+
+        value = valueFactory(key);
+        dict.Add(key, value);
+        return value;
+    }
+}
diff --git a/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs b/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs
index 9ac9c6b..009338a 100644
--- a/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs
+++ b/MatrixRoomUtils.Core/Extensions/HttpClientExtensions.cs
@@ -38,7 +38,7 @@ public class MatrixHttpClient : HttpClient {
             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)
@@ -64,4 +64,13 @@ public class MatrixHttpClient : HttpClient {
         await using var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);
         return await JsonSerializer.DeserializeAsync<T>(responseStream, cancellationToken: cancellationToken);
     }
+
+    // GetStreamAsync
+    public 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);
+        response.EnsureSuccessStatusCode();
+        return await response.Content.ReadAsStreamAsync(cancellationToken);
+    }
 }