Browse Source

PRS MOBILE - remote client updates to RestSharp v11

Nick-PRSDigital@bitbucket.org 2 years ago
parent
commit
3970dec10a

+ 4 - 4
InABox.Client.Remote.Json/HttpJsonClient.cs

@@ -71,7 +71,7 @@ namespace InABox.Client.Remote.Json
 
         public bool Ping(string endPoint)
         {
-            var req = new RestRequest(endPoint, Method.GET)
+            var req = new RestRequest(endPoint, Method.Get)
             {
                 Timeout = 10 * 1000
             };
@@ -100,7 +100,7 @@ namespace InABox.Client.Remote.Json
 
         public bool TryGetRequest<T>(string command, out T result, [NotNullWhen(false)] out string? error)
         {
-            var req = new RestRequest(command, Method.GET)
+            var req = new RestRequest(command, Method.Get)
             {
                 Timeout = 60 * 1000
             };
@@ -120,11 +120,11 @@ namespace InABox.Client.Remote.Json
         {
             var json = Serialization.Serialize(request);
 
-            var req = new RestRequest(command, Method.POST)
+            var req = new RestRequest(command, Method.Post)
             {
                 Timeout = 60 * 1000
             };
-            req.AddOrUpdateParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
+            req.AddOrUpdateParameter("application/json", json, ParameterType.RequestBody);
             req.RequestFormat = DataFormat.Json;
             return SendRequest<T>(req);
         }

+ 1 - 1
InABox.Client.Remote.Json/InABox.Client.Remote.Json.csproj

@@ -9,7 +9,7 @@
 
     <ItemGroup>
         <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
-        <PackageReference Include="RestSharp" Version="106.15.0" />
+        <PackageReference Include="RestSharp" Version="110.2.0" />
     </ItemGroup>
 
     <ItemGroup>

+ 50 - 31
InABox.Client.Remote.Json/JsonClient.cs

@@ -34,6 +34,8 @@ namespace InABox.Clients
 
     public class JsonClient<TEntity> : RemoteClient<TEntity> where TEntity : Entity, new()
     {
+        private static RestClient cli = null;
+
         private readonly bool _compression = true;
 
         private BinarySerializationSettings BinarySerializationSettings;
@@ -44,11 +46,16 @@ namespace InABox.Clients
             BinarySerializationSettings = binarySerializationSettings;
         }
 
-        public JsonClient(string url, bool useSimpleEncryption) : this(url, useSimpleEncryption, true, BinarySerializationSettings.Latest)
+        public static class StaticClients
+        {
+            public static Dictionary<string, RestClient> Clients = new Dictionary<string, RestClient>();
+        }
+
+        public JsonClient(string url, bool useSimpleEncryption) : this(url, useSimpleEncryption, false, BinarySerializationSettings.Latest)
         {
         }
 
-        public JsonClient(string url) : this(url, false, true, BinarySerializationSettings.Latest)
+        public JsonClient(string url) : this(url, false, false, BinarySerializationSettings.Latest)
         {
         }
 
@@ -113,7 +120,8 @@ namespace InABox.Clients
             //sw.Restart();
 
             var uri = new Uri(URL);
-            var cli = new RestClient(uri);
+            if (cli == null)
+                SetClient(URL);
             var cmd = string.Format(
                 "{0}{1}?format={2}&responseFormat={3}&serializationVersion={4}", 
                 Action, 
@@ -122,35 +130,35 @@ namespace InABox.Clients
                 responseFormat,
                 BinarySerializationSettings.Version
             );
-            var req = new RestRequest(cmd, Method.POST)
+            var req = new RestRequest(cmd, Method.Post)
             {
                 Timeout = Timeout.Milliseconds,
             };
             //Log("  * {0}{1}() Creating Uri, Client and RestRequest took {2}ms", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds);
             //sw.Restart();
 
-            req.AdvancedResponseWriter = (stream, response) =>
-            {
-                //Log("  * {0}{1}() Response from Server took {2}ms ({3} bytes)", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds, response.ContentLength);
-                //length = response.ContentLength;
-                //sw.Restart();
-                try
-                {
-                    if (responseFormat == SerializationFormat.Binary && typeof(TResponse).HasInterface<ISerializeBinary>())
-                    {
-                        result = (TResponse)Serialization.ReadBinary(typeof(TResponse), stream, BinarySerializationSettings);
-                    }
-                    else
-                    {
-                        result = Serialization.Deserialize<TResponse>(stream, true);
-                    }
-                }
-                catch (Exception e)
-                {
-                    Logger.Send(LogType.Information, "", $"Error deserializing response: {e.Message}");
-                }
-                //Log("  * {0}{1}() Deserializing Stream took {2}ms ({3} bytes)", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds, response.ContentLength);
-            };
+            //req.AdvancedResponseWriter = (stream, response) =>
+            //{
+            //    //Log("  * {0}{1}() Response from Server took {2}ms ({3} bytes)", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds, response.ContentLength);
+            //    //length = response.ContentLength;
+            //    //sw.Restart();
+            //    try
+            //    {
+            //        if (responseFormat == SerializationFormat.Binary && typeof(TResponse).HasInterface<ISerializeBinary>())
+            //        {
+            //            result = (TResponse)Serialization.ReadBinary(typeof(TResponse), stream, BinarySerializationSettings);
+            //        }
+            //        else
+            //        {
+            //            result = Serialization.Deserialize<TResponse>(stream, true);
+            //        }
+            //    }
+            //    catch (Exception e)
+            //    {
+            //        Logger.Send(LogType.Information, "", $"Error deserializing response: {e.Message}");
+            //    }
+            //    //Log("  * {0}{1}() Deserializing Stream took {2}ms ({3} bytes)", Action, typeof(TEntity).Name, sw.ElapsedMilliseconds, response.ContentLength);
+            //};
 
             if(requestFormat == SerializationFormat.Binary && request is ISerializeBinary binary)
             {
@@ -163,7 +171,7 @@ namespace InABox.Clients
             {
                 var json = Serialization.Serialize(request);
 
-                req.AddOrUpdateParameter("application/json; charset=utf-8", json, ParameterType.RequestBody);
+                req.AddOrUpdateParameter("application/json", json, ParameterType.RequestBody);
                 req.RequestFormat = DataFormat.Json;
             }
             try
@@ -235,16 +243,27 @@ namespace InABox.Clients
             }
 
             req = null;
-            cli = null;
             //double elapsed = (DateTime.Now - now).TotalMilliseconds;
             //Log("  * {0}{1}() completed in {2:F0}ms", Action, typeof(TEntity).Name, elapsed);
             return result;
         }
 
+        private void SetClient(string uRL)
+        {
+            var uri = new Uri(URL);
+            if (StaticClients.Clients.ContainsKey(URL))
+                cli = StaticClients.Clients[URL];
+            else
+            {
+                cli = new RestClient(uri);
+                StaticClients.Clients.Add(URL, cli);
+            }
+        }
+
         // SupportedTypes() function fallback for if the server is running on ServiceStack
         private void SupportedTypesOld(RestClient cli, List<string> result)
         {
-            var req = new RestRequest("/operations/metadata?format=csv", Method.GET) { Timeout = 20000 };
+            var req = new RestRequest("/operations/metadata?format=csv", Method.Get) { Timeout = 20000 };
 
             try
             {
@@ -277,7 +296,7 @@ namespace InABox.Clients
             var result = new List<string>();
             var uri = new Uri(URL);
             var cli = new RestClient(uri);
-            var req = new RestRequest("/classes", Method.GET) { Timeout = 20000 };
+            var req = new RestRequest("/classes", Method.Get) { Timeout = 20000 };
 
             try
             {
@@ -311,7 +330,7 @@ namespace InABox.Clients
         {
             var uri = new Uri(URL);
             var cli = new RestClient(uri);
-            var req = new RestRequest("/info", Method.GET) { Timeout = 20000 };
+            var req = new RestRequest("/info", Method.Get) { Timeout = 20000 };
 
             try
             {

+ 1 - 1
InABox.Client.Remote.Shared/RemoteClient.cs

@@ -443,7 +443,7 @@ namespace InABox.Clients
         {
             var uri = new Uri(URL);
             var cli = new RestClient(uri);
-            var req = new RestRequest("/classes", Method.GET) { Timeout = 20000 };
+            var req = new RestRequest("/classes", Method.Get) { Timeout = 20000 };
 
             try
             {