Explorar el Código

Merge remote-tracking branch 'origin/Stable-version-RestSharp-v11-with-JsonClient' into kenric

# Conflicts:
#	InABox.Client.Remote.Json/JsonClient.cs
#	inabox.client.rest/InABox.Client.Rest/RestClient.cs
Kenric Nugteren hace 2 años
padre
commit
71ab8a8ed7

+ 0 - 28
InABox.Client.Remote.Json/InABox.Client.Remote.Json.csproj

@@ -1,28 +0,0 @@
-<Project Sdk="Microsoft.NET.Sdk">
-
-    <PropertyGroup>
-        <ReleaseVersion>1.00</ReleaseVersion>
-        <SynchReleaseVersion>false</SynchReleaseVersion>
-        <TargetFramework>netstandard2.1</TargetFramework>
-		<Nullable>Enable</Nullable>
-    </PropertyGroup>
-
-    <ItemGroup>
-        <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
-        <PackageReference Include="RestSharp" Version="110.2.0" />
-    </ItemGroup>
-
-    <ItemGroup>
-        <ProjectReference Include="..\InABox.Client.WebSocket\InABox.Client.WebSocket.csproj" />
-        <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
-    </ItemGroup>
-
-    <ItemGroup>
-        <None Remove=".gitignore" />
-    </ItemGroup>
-
-    <Import Project="..\InABox.Client.Remote.Shared\InABox.Client.Remote.Shared.projitems" Label="Shared" />
-
-    <Import Project="..\InABox.Remote.Shared\InABox.Remote.Shared.projitems" Label="Shared" />
-
-</Project>

+ 2 - 0
InABox.Server/Rest/RestListener.cs

@@ -109,6 +109,8 @@ namespace InABox.API
                                 return new ValueTask<IResponse?>(GetUpdateFile(request).Build());
                             case "info":
                                 return new ValueTask<IResponse?>(GetServerInfo(request).Build());
+                            case "ping":
+                                return new ValueTask<IResponse?>(request.Respond().Status(ResponseStatus.OK).Build());
                         }
 
                         Logger.Send(LogType.Error, request.Client.IPAddress.ToString(),

+ 29 - 41
inabox.client.rest/InABox.Client.Rest/RestClient.cs

@@ -15,46 +15,43 @@ using System.Threading.Tasks;
 
 namespace InABox.Clients
 {
+    public static class StaticRestClients
+    {
+        public static Dictionary<string, RestClient> Clients = new Dictionary<string, RestClient>();
+
+        public static RestClient GetClient(string url)
+        {
+            var uri = new Uri(url);
+            if (!Clients.TryGetValue(url, out var cli))
+            {
+                cli = new RestClient(uri);
+                Clients.Add(url, cli);
+            }
+            return cli;
+        }
+    }
+
     public class RestClient<TEntity> : BaseClient<TEntity> where TEntity : Entity, new()
     {
         private bool _simpleencryption;
         private string _server;
         private bool _compression;
         private BinarySerializationSettings _binarysettings;
-        private static RestClient cli = null;
         
-        public RestClient(string server, bool simpleencryption, bool compression, BinarySerializationSettings binarySerializationSettings)
+        public RestClient(string server, bool useSimpleEncryption, bool compression, BinarySerializationSettings binarySerializationSettings)
         {
             _server = server;
-            _simpleencryption = simpleencryption;
+            _simpleencryption = useSimpleEncryption;
             _compression = compression;
             _binarysettings = binarySerializationSettings;
 
             RestClientCache.Check(server);
-            
         }
 
-        public static class StaticRestClients
+        public RestClient(string server, bool simpleencryption, bool compression) : this(server, simpleencryption, compression, BinarySerializationSettings.Latest)
         {
-            public static Dictionary<string, RestClient> Clients = new Dictionary<string, RestClient>();
-
-            public static void SetClient(string url)
-            {
-                var uri = new Uri(url);
-                if (Clients.ContainsKey(url))
-                    cli = Clients[url];
-                else
-                {
-                    cli = new RestClient(uri);
-                    Clients.Add(url, cli);
-                }
-            }
         }
 
-        public RestClient(string server, bool simpleencryption, bool compression) : this(server, simpleencryption, false, BinarySerializationSettings.Latest)
-        {
-        }
-        
         public RestClient(string server, bool simpleencryption) : this(server, simpleencryption, false)
         {
         }
@@ -63,17 +60,17 @@ namespace InABox.Clients
         {
         }
         
-        public static string Ping(String[] urls, out DatabaseInfo info)
+        public static string Ping(string[] urls, out DatabaseInfo info)
         {
-            String result = "";
+            var result = "";
             info = new DatabaseInfo();
-            List<Task<Tuple<String,DatabaseInfo>>> pings = urls.Select(x => Task.Run(
-                () => new Tuple<String,DatabaseInfo>(x,new RestClient<User>(x).Info())
+            List<Task<Tuple<string,DatabaseInfo>>> pings = urls.Select(x => Task.Run(
+                () => new Tuple<string, DatabaseInfo>(x, new RestClient<User>(x).Info())
             )).ToList();
             while (pings.Count > 0)
             {
                 var ping = Task.WhenAny(pings).Result;
-                if (ping.Status == TaskStatus.RanToCompletion && !String.IsNullOrWhiteSpace(ping.Result.Item2.Version))
+                if (ping.Status == TaskStatus.RanToCompletion && !string.IsNullOrWhiteSpace(ping.Result.Item2.Version))
                 {
                     result = ping.Result.Item1;
                     info = ping.Result.Item2;
@@ -197,8 +194,7 @@ namespace InABox.Clients
                 return result;
             }
 
-            if (cli == null)
-                StaticRestClients.SetClient(url);
+            var cli = StaticRestClients.GetClient(url);
 
             var cmd = string.Format(
                 "{0}{1}?format={2}&responseFormat={3}&serializationVersion={4}", 
@@ -320,13 +316,8 @@ namespace InABox.Clients
                     result.Messages.Add("- " + err.InnerException.Message);
             }
 
-            req = null;
-            //double elapsed = (DateTime.Now - now).TotalMilliseconds;
-            //Log("  * {0}{1}() completed in {2:F0}ms", Action, typeof(TEntity).Name, elapsed);
             return result;
-        }     
-        
-        
+        }
 
         #region Query Data
 
@@ -567,8 +558,8 @@ namespace InABox.Clients
 
         protected override bool DoPing()
         {
-            var cli = new RestClient(new Uri(RestClientCache.URL(_server)));
-            var req = new RestRequest("/info", Method.Get) { Timeout = 20000 };
+            var cli = StaticRestClients.GetClient(RestClientCache.URL(_server));
+            var req = new RestRequest("/ping", Method.Get) { Timeout = 20000 };
 
             try
             {
@@ -589,10 +580,7 @@ namespace InABox.Clients
         {
             var result = new List<string>();
 
-            var url = RestClientCache.URL(_server);
-            
-            var uri = new Uri(url);
-            var cli = new RestClient(uri);
+            var cli = StaticRestClients.GetClient(RestClientCache.URL(_server));
             var req = new RestRequest("/classes", Method.Get) { Timeout = 20000 };
 
             try

+ 9 - 9
inabox.client.rest/InABox.Client.Rest/RestClientCache.cs

@@ -12,30 +12,30 @@ namespace InABox.Clients
 {
     internal static class RestClientCache
     {
-        private static Dictionary<String, Tuple<String, bool, DatabaseInfo>> _cache = new Dictionary<string, Tuple<string, bool, DatabaseInfo>>();
-            
-        public static String URL(String host)
+        private static readonly Dictionary<string, Tuple<string, bool, DatabaseInfo>> _cache = new Dictionary<string, Tuple<string, bool, DatabaseInfo>>();
+        
+        public static string URL(string host)
         {
             if (_cache.TryGetValue(host, out var value))
                 return value.Item1;
             return "";
         }
 
-        public static bool IsSecure(String host)
+        public static bool IsSecure(string host)
         {
             if (_cache.TryGetValue(host, out var value))
                 return value.Item2;
             return false;
         }
         
-        public static DatabaseInfo Info(String host)
+        public static DatabaseInfo Info(string host)
         {
             if (_cache.TryGetValue(host, out var value))
                 return value.Item3;
             return new DatabaseInfo();
         }
             
-        private static bool Check(String host, bool https)
+        private static bool Check(string host, bool https)
         {
             var uri = new Uri($"http://{host}");
             string schema = https ? "https" : "http";
@@ -52,17 +52,17 @@ namespace InABox.Clients
                 _cache[host] = new Tuple<string, bool, DatabaseInfo>(url, https, response.Info);
                 return true;
             }
-            catch (Exception e)
+            catch (Exception)
             {
                 return false;
             }
         }
         
-        public static String Check(string server)
+        public static string Check(string server)
         {
             var host = server.Split(new[] { "://" }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
             
-            if (String.IsNullOrWhiteSpace(host))
+            if (string.IsNullOrWhiteSpace(host))
                 return "";
             
             if (_cache.TryGetValue(host, out var cached))