about summary refs log tree commit diff
path: root/LibMatrix/Extensions
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 /LibMatrix/Extensions
parentSingle httpclient implementation (diff)
downloadLibMatrix-99294322327f899eb01e9c78c0681528cada99d1.tar.xz
Diffstat (limited to '')
-rw-r--r--LibMatrix/Extensions/JsonConverters.cs29
-rw-r--r--LibMatrix/Extensions/MatrixHttpClient.Single.cs15
2 files changed, 36 insertions, 8 deletions
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) {