Try method for public server.

Added TryGetPublicServer to API handler.
This commit is contained in:
JacobTech 2023-10-01 12:28:10 -04:00
parent 0349745944
commit 0740c6daca
6 changed files with 41 additions and 11 deletions

View File

@ -10,8 +10,28 @@ namespace Luski.net;
public class API public class API
{ {
public MainServer MainServer { get; internal set; } public MainServer MainServer { get; internal set; }
public bool IsAnyServerLoggedin { get; internal set; }
internal List<PublicServer> InternalServers { get; } = new(); internal List<PublicServer> InternalServers { get; } = new();
public IReadOnlyList<PublicServer> LoadedServers => InternalServers.AsReadOnly(); public IReadOnlyList<PublicServer> LoadedServers => InternalServers.AsReadOnly();
public Task<bool> TryGetPublicServer(out PublicServer Server, string Domain, string Version = "v1",
bool Secure = true)
{
try
{
Task<PublicServer> result = GetPublicServer(Domain, Version, Secure);
Task.WaitAll(result);
Server = result.Result;
return Task.FromResult(true);
}
catch (Exception e)
{
Console.WriteLine(e);
Server = null!;
return Task.FromResult(false);
}
}
public async Task<PublicServer> GetPublicServer(string Domain, string Version = "v1", bool Secure = true) public async Task<PublicServer> GetPublicServer(string Domain, string Version = "v1", bool Secure = true)
{ {
@ -20,7 +40,7 @@ public class API
{ {
IEnumerable<PublicServer> isl = InternalServers.Where(a => (a.Domain == Domain && a.ApiVersion == Version)); IEnumerable<PublicServer> isl = InternalServers.Where(a => (a.Domain == Domain && a.ApiVersion == Version));
if (isl.Any()) return isl.First(); if (isl.Any()) return isl.First();
s = await PublicServer.GetServer(Domain, Version, Secure); s = await PublicServer.GetServer(this, Domain, Version, Secure);
} }
catch (Exception e) catch (Exception e)
{ {
@ -40,7 +60,11 @@ public class API
if (f is not null) if (f is not null)
{ {
bool b = await s.LoginViaToken(f); bool b = await s.LoginViaToken(f);
if (b) Console.WriteLine("Auto Login Successful"); if (b)
{
Console.WriteLine("Auto Login Successful");
IsAnyServerLoggedin = true;
}
else else
{ {
s.Token = null; s.Token = null;
@ -60,7 +84,7 @@ public class API
{ {
DateTime dt = DateTime.UtcNow; DateTime dt = DateTime.UtcNow;
Console.WriteLine("Conecting to main server '{0}' using API {1}.", Domain, Version); Console.WriteLine("Conecting to main server '{0}' using API {1}.", Domain, Version);
MainServer = new(Domain, Version) MainServer = new(this, Domain, Version)
{ {
ServerType = ServerType.Main ServerType = ServerType.Main
}; };

View File

@ -13,7 +13,7 @@
<RepositoryUrl>https://github.com/JacobTech-com/Luski.net</RepositoryUrl> <RepositoryUrl>https://github.com/JacobTech-com/Luski.net</RepositoryUrl>
<IncludeSymbols>True</IncludeSymbols> <IncludeSymbols>True</IncludeSymbols>
<FileVersion>1.0.0</FileVersion> <FileVersion>1.0.0</FileVersion>
<Version>2.0.0-alpha23</Version> <Version>2.0.0-alpha24</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -20,8 +20,8 @@ namespace Luski.net;
public partial class MainServer : Server public partial class MainServer : Server
{ {
internal MainServer(string Domain, string API_Version): internal MainServer(API api, string Domain, string API_Version):
base(Domain, API_Version) base(api, Domain, API_Version)
{ {
} }

View File

@ -171,6 +171,7 @@ public partial class PublicServer
stor); stor);
EncryptionHandler.OfflinePublicKey = null!; EncryptionHandler.OfflinePublicKey = null!;
EncryptionHandler.OfflinePrivateKey = null!; EncryptionHandler.OfflinePrivateKey = null!;
API_Handler.IsAnyServerLoggedin = true;
return true; return true;
} }
else else
@ -178,6 +179,7 @@ public partial class PublicServer
throw new Exception(json?.ErrorMessage); throw new Exception(json?.ErrorMessage);
} }
API_Handler.IsAnyServerLoggedin = true;
return true; return true;
} }
@ -344,10 +346,12 @@ public partial class PublicServer
// _ = await setkey.PostAsync($"{(Secure ? "https" : "http" )}://{Domain}/{ApiVersion}/Keys/SetOfflineKey", new StringContent(EncryptionHandler.OfflinePublicKey)); // _ = await setkey.PostAsync($"{(Secure ? "https" : "http" )}://{Domain}/{ApiVersion}/Keys/SetOfflineKey", new StringContent(EncryptionHandler.OfflinePublicKey));
EncryptionHandler.OfflinePublicKey = null!; EncryptionHandler.OfflinePublicKey = null!;
EncryptionHandler.OfflinePrivateKey = null!; EncryptionHandler.OfflinePrivateKey = null!;
API_Handler.IsAnyServerLoggedin = true;
return true; return true;
} }
else throw new Exception(json?.ErrorMessage); else throw new Exception(json?.ErrorMessage);
API_Handler.IsAnyServerLoggedin = true;
return true; return true;
} }
} }

View File

@ -31,15 +31,15 @@ public partial class PublicServer : Server
public SocketAppUser User { get; private set; } = null!; public SocketAppUser User { get; private set; } = null!;
private PublicServer(string Domain, string API_Version, bool Secure = true) : private PublicServer(API api,string Domain, string API_Version, bool Secure = true) :
base(Domain, API_Version, Secure) base(api, Domain, API_Version, Secure)
{ } { }
internal static async Task<PublicServer> GetServer(string Domain, string API_Version, bool Secure = true) internal static async Task<PublicServer> GetServer(API api, string Domain, string API_Version, bool Secure = true)
{ {
DateTime dt = DateTime.UtcNow; DateTime dt = DateTime.UtcNow;
Console.WriteLine("Connecting to public server '{0}' using API {1}.", Domain, API_Version); Console.WriteLine("Connecting to public server '{0}' using API {1}.", Domain, API_Version);
PublicServer s = new(Domain, API_Version, Secure); PublicServer s = new(api, Domain, API_Version, Secure);
ServerInfo? si = null; ServerInfo? si = null;
try try
{ {

View File

@ -19,8 +19,10 @@ namespace Luski.net;
public partial class Server public partial class Server
{ {
internal Server(string Domain, string API_Version, bool Secure = true) internal API API_Handler;
internal Server(API api_handle, string Domain, string API_Version, bool Secure = true)
{ {
API_Handler = api_handle;
this.Domain = Domain; this.Domain = Domain;
this.ApiVersion = API_Version; this.ApiVersion = API_Version;
this.Secure = Secure; this.Secure = Secure;