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,9 +10,29 @@ namespace Luski.net;
public class API
{
public MainServer MainServer { get; internal set; }
public bool IsAnyServerLoggedin { get; internal set; }
internal List<PublicServer> InternalServers { get; } = new();
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)
{
PublicServer s;
@ -20,7 +40,7 @@ public class API
{
IEnumerable<PublicServer> isl = InternalServers.Where(a => (a.Domain == Domain && a.ApiVersion == Version));
if (isl.Any()) return isl.First();
s = await PublicServer.GetServer(Domain, Version, Secure);
s = await PublicServer.GetServer(this, Domain, Version, Secure);
}
catch (Exception e)
{
@ -40,7 +60,11 @@ public class API
if (f is not null)
{
bool b = await s.LoginViaToken(f);
if (b) Console.WriteLine("Auto Login Successful");
if (b)
{
Console.WriteLine("Auto Login Successful");
IsAnyServerLoggedin = true;
}
else
{
s.Token = null;
@ -60,7 +84,7 @@ public class API
{
DateTime dt = DateTime.UtcNow;
Console.WriteLine("Conecting to main server '{0}' using API {1}.", Domain, Version);
MainServer = new(Domain, Version)
MainServer = new(this, Domain, Version)
{
ServerType = ServerType.Main
};

View File

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

View File

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

View File

@ -171,6 +171,7 @@ public partial class PublicServer
stor);
EncryptionHandler.OfflinePublicKey = null!;
EncryptionHandler.OfflinePrivateKey = null!;
API_Handler.IsAnyServerLoggedin = true;
return true;
}
else
@ -178,6 +179,7 @@ public partial class PublicServer
throw new Exception(json?.ErrorMessage);
}
API_Handler.IsAnyServerLoggedin = 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));
EncryptionHandler.OfflinePublicKey = null!;
EncryptionHandler.OfflinePrivateKey = null!;
API_Handler.IsAnyServerLoggedin = true;
return true;
}
else throw new Exception(json?.ErrorMessage);
API_Handler.IsAnyServerLoggedin = true;
return true;
}
}

View File

@ -31,15 +31,15 @@ public partial class PublicServer : Server
public SocketAppUser User { get; private set; } = null!;
private PublicServer(string Domain, string API_Version, bool Secure = true) :
base(Domain, API_Version, Secure)
private PublicServer(API api,string Domain, string API_Version, bool Secure = true) :
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;
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;
try
{

View File

@ -19,8 +19,10 @@ namespace Luski.net;
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.ApiVersion = API_Version;
this.Secure = Secure;