about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-06-17 06:17:03 +0200
committerEmma [it/its]@Rory& <root@rory.gay>2024-06-17 06:25:20 +0200
commit99294322327f899eb01e9c78c0681528cada99d1 (patch)
tree023a30b463867deb4c8b1b9df8c8b11770846860
parentSingle httpclient implementation (diff)
downloadLibMatrix-github/dev/new-httpclient.tar.xz
-rw-r--r--.gitignore1
m---------ExampleBots/ModerationBot0
-rw-r--r--LibMatrix.sln.DotSettings.user6
-rw-r--r--LibMatrix/Extensions/JsonConverters.cs29
-rw-r--r--LibMatrix/Extensions/MatrixHttpClient.Single.cs15
-rw-r--r--LibMatrix/Homeservers/FederationClient.cs2
-rw-r--r--LibMatrix/Homeservers/RemoteHomeServer.cs2
-rw-r--r--LibMatrix/RoomTypes/GenericRoom.cs4
-rw-r--r--LibMatrix/Services/HomeserverResolverService.cs2
-rw-r--r--Tests/LibMatrix.Tests/Tests/AuthTests.cs2
10 files changed, 43 insertions, 20 deletions
diff --git a/.gitignore b/.gitignore
index fba66db..509a921 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
 **/bin/
 **/obj/
+**/*.[Dd]ot[Ss]ettings.[Uu]ser
 MatrixRoomUtils/
 MatrixRoomUtils.Web/wwwroot/MRU.tar.xz
 /src/
diff --git a/ExampleBots/ModerationBot b/ExampleBots/ModerationBot
-Subproject c137f94aeb122c636629fb9361dd73626594f69
+Subproject e4fbb948bce90fdfe757c70081c6dbec84b7024
diff --git a/LibMatrix.sln.DotSettings.user b/LibMatrix.sln.DotSettings.user
deleted file mode 100644
index e26043a..0000000
--- a/LibMatrix.sln.DotSettings.user
+++ /dev/null
@@ -1,6 +0,0 @@
-<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
-	<s:Boolean x:Key="/Default/AddReferences/RecentPaths/=_002Fhome_002FRory_002Fgit_002Fmatrix_002FMatrixRoomUtils_002FLibMatrix_002FArcaneLibs_002FArcaneLibs_002Fbin_002FDebug_002Fnet8_002E0_002FArcaneLibs_002Edll/@EntryIndexedValue">True</s:Boolean>
-	<s:String x:Key="/Default/CodeInspection/Highlighting/SweaWarningsMode/@EntryValue">ShowAndRun</s:String>
-	<s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue">&lt;AssemblyExplorer&gt;
-  &lt;Assembly Path="/home/root@Rory/.cache/NuGetPackages/microsoft.extensions.hosting.abstractions/7.0.0/lib/net7.0/Microsoft.Extensions.Hosting.Abstractions.dll" /&gt;
-&lt;/AssemblyExplorer&gt;</s:String></wpf:ResourceDictionary>
\ No newline at end of file
diff --git a/LibMatrix/Extensions/JsonConverters.cs b/LibMatrix/Extensions/JsonConverters.cs
new file mode 100644
index 0000000..eed3fb2
--- /dev/null
+++ b/LibMatrix/Extensions/JsonConverters.cs
@@ -0,0 +1,29 @@
+using System.Globalization;
+using System.Text.Json;
+using System.Text.Json.Serialization;
+
+namespace LibMatrix.Extensions;
+
+public class JsonFloatStringConverter : JsonConverter<float> {
+    public override float Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+        => float.Parse(reader.GetString()!);
+
+    public override void Write(Utf8JsonWriter writer, float value, JsonSerializerOptions options)
+        => writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture));
+}
+
+public class JsonDoubleStringConverter : JsonConverter<double> {
+    public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+        => double.Parse(reader.GetString()!);
+
+    public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options)
+        => writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture));
+}
+
+public class JsonDecimalStringConverter : JsonConverter<decimal> {
+    public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+        => decimal.Parse(reader.GetString()!);
+
+    public override void Write(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options)
+        => writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture));
+}
\ No newline at end of file
diff --git a/LibMatrix/Extensions/MatrixHttpClient.Single.cs b/LibMatrix/Extensions/MatrixHttpClient.Single.cs
index 9d0f9d0..e722370 100644
--- a/LibMatrix/Extensions/MatrixHttpClient.Single.cs
+++ b/LibMatrix/Extensions/MatrixHttpClient.Single.cs
@@ -14,19 +14,18 @@ using ArcaneLibs.Extensions;
 namespace LibMatrix.Extensions;
 
 #if SINGLE_HTTPCLIENT
+// TODO: Add URI wrapper for 
 public class MatrixHttpClient {
-    private static readonly SocketsHttpHandler handler;
-
-    private static readonly HttpClient client;
+    private static readonly HttpClient Client;
 
     static MatrixHttpClient() {
         try {
-            handler = new SocketsHttpHandler {
+            var handler = new SocketsHttpHandler {
                 PooledConnectionLifetime = TimeSpan.FromMinutes(15),
                 MaxConnectionsPerServer = 4096,
                 EnableMultipleHttp2Connections = true
             };
-            client = new HttpClient(handler) {
+            Client = new HttpClient(handler) {
                 DefaultRequestVersion = new Version(3, 0)
             };
         }
@@ -35,7 +34,7 @@ public class MatrixHttpClient {
             Console.WriteLine("Original exception (safe to ignore!):");
             Console.WriteLine(e);
 
-            client = new HttpClient {
+            Client = new HttpClient {
                 DefaultRequestVersion = new Version(3, 0)
             };
         }
@@ -84,7 +83,7 @@ public class MatrixHttpClient {
 
         HttpResponseMessage? responseMessage;
         try {
-            responseMessage = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
+            responseMessage = await Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
         }
         catch (Exception e) {
             Console.WriteLine(
@@ -208,7 +207,7 @@ public class MatrixHttpClient {
     public async Task<bool> CheckSuccessStatus(string url) {
         //cors causes failure, try to catch
         try {
-            var resp = await client.GetAsync(url);
+            var resp = await Client.GetAsync(url);
             return resp.IsSuccessStatusCode;
         }
         catch (Exception e) {
diff --git a/LibMatrix/Homeservers/FederationClient.cs b/LibMatrix/Homeservers/FederationClient.cs
index dc0d1f6..22653e4 100644
--- a/LibMatrix/Homeservers/FederationClient.cs
+++ b/LibMatrix/Homeservers/FederationClient.cs
@@ -9,7 +9,7 @@ public class FederationClient {
     public FederationClient(string federationEndpoint, string? proxy = null) {
         HttpClient = new MatrixHttpClient {
             BaseAddress = new Uri(proxy?.TrimEnd('/') ?? federationEndpoint.TrimEnd('/')),
-            Timeout = TimeSpan.FromSeconds(120)
+            // Timeout = TimeSpan.FromSeconds(120) // TODO: Re-implement this
         };
         if (proxy is not null) HttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", federationEndpoint);
     }
diff --git a/LibMatrix/Homeservers/RemoteHomeServer.cs b/LibMatrix/Homeservers/RemoteHomeServer.cs
index 8669ca7..ecf3e3a 100644
--- a/LibMatrix/Homeservers/RemoteHomeServer.cs
+++ b/LibMatrix/Homeservers/RemoteHomeServer.cs
@@ -19,7 +19,7 @@ public class RemoteHomeserver {
         WellKnownUris = wellKnownUris;
         ClientHttpClient = new MatrixHttpClient {
             BaseAddress = new Uri(proxy?.TrimEnd('/') ?? wellKnownUris.Client?.TrimEnd('/') ?? throw new InvalidOperationException($"No client URI for {baseUrl}!")),
-            Timeout = TimeSpan.FromSeconds(300)
+            // Timeout = TimeSpan.FromSeconds(300) // TODO: Re-implement this
         };
 
         if (proxy is not null) ClientHttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", baseUrl);
diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs
index f15327c..fe2ee8d 100644
--- a/LibMatrix/RoomTypes/GenericRoom.cs
+++ b/LibMatrix/RoomTypes/GenericRoom.cs
@@ -531,14 +531,14 @@ public class GenericRoom {
         if (!string.IsNullOrEmpty(to)) uri = uri.AddQuery("to", to);
 
         // Console.WriteLine($"Getting related events from {uri}");
-        var result = await Homeserver.ClientHttpClient.GetFromJsonAsync<RecursedBatchedChunkedStateEventResponse>(uri);
+        var result = await Homeserver.ClientHttpClient.GetFromJsonAsync<RecursedBatchedChunkedStateEventResponse>(uri.ToString());
         while (result!.Chunk.Count > 0) {
             foreach (var resp in result.Chunk) {
                 yield return resp;
             }
 
             if (result.NextBatch is null) break;
-            result = await Homeserver.ClientHttpClient.GetFromJsonAsync<RecursedBatchedChunkedStateEventResponse>(uri.AddQuery("from", result.NextBatch));
+            result = await Homeserver.ClientHttpClient.GetFromJsonAsync<RecursedBatchedChunkedStateEventResponse>(uri.AddQuery("from", result.NextBatch).ToString());
         }
     }
 
diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs
index 05ce733..f899230 100644
--- a/LibMatrix/Services/HomeserverResolverService.cs
+++ b/LibMatrix/Services/HomeserverResolverService.cs
@@ -14,7 +14,7 @@ namespace LibMatrix.Services;
 
 public class HomeserverResolverService {
     private readonly MatrixHttpClient _httpClient = new() {
-        Timeout = TimeSpan.FromSeconds(60)
+        // Timeout = TimeSpan.FromSeconds(60) // TODO: Re-implement this
     };
 
     private static readonly SemaphoreCache<WellKnownUris> WellKnownCache = new();
diff --git a/Tests/LibMatrix.Tests/Tests/AuthTests.cs b/Tests/LibMatrix.Tests/Tests/AuthTests.cs
index 67ba8eb..f331dd0 100644
--- a/Tests/LibMatrix.Tests/Tests/AuthTests.cs
+++ b/Tests/LibMatrix.Tests/Tests/AuthTests.cs
@@ -60,7 +60,7 @@ public class AuthTests : TestBed<TestFixture> {
         Assert.NotNull(reg.AccessToken);
         Assert.NotNull(reg.DeviceId);
         Assert.NotNull(reg.UserId);
-        var hs = await reg.GetAuthenticatedHomeserver();
+        var hs = await _provider.GetAuthenticatedWithToken(reg.Homeserver, reg.AccessToken);
         Assert.NotNull(hs);
         Assert.NotNull(hs.WhoAmI);
         hs.WhoAmI.VerifyRequiredFields();