2023-08-21 10:58:17 -04:00
|
|
|
using System;
|
2023-07-03 23:24:35 -04:00
|
|
|
using System.Collections.Generic;
|
2023-08-21 10:58:17 -04:00
|
|
|
using System.IO;
|
2023-07-03 23:24:35 -04:00
|
|
|
using System.Linq;
|
2023-10-01 13:12:27 -04:00
|
|
|
using System.Text.Json.Serialization;
|
2023-08-21 10:58:17 -04:00
|
|
|
using System.Threading.Tasks;
|
2023-07-10 07:35:05 -04:00
|
|
|
using Luski.net.Enums;
|
2023-07-03 23:24:35 -04:00
|
|
|
|
|
|
|
namespace Luski.net;
|
|
|
|
|
|
|
|
public class API
|
|
|
|
{
|
2023-10-01 13:12:27 -04:00
|
|
|
[JsonIgnore]
|
2023-07-08 09:06:13 -04:00
|
|
|
public MainServer MainServer { get; internal set; }
|
2023-10-01 12:28:10 -04:00
|
|
|
|
|
|
|
public bool IsAnyServerLoggedin { get; internal set; }
|
2023-07-08 09:06:13 -04:00
|
|
|
internal List<PublicServer> InternalServers { get; } = new();
|
|
|
|
public IReadOnlyList<PublicServer> LoadedServers => InternalServers.AsReadOnly();
|
2023-10-01 12:28:10 -04:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2023-07-03 23:24:35 -04:00
|
|
|
|
2023-08-21 10:58:17 -04:00
|
|
|
public async Task<PublicServer> GetPublicServer(string Domain, string Version = "v1", bool Secure = true)
|
2023-07-03 23:24:35 -04:00
|
|
|
{
|
2023-08-21 10:58:17 -04:00
|
|
|
PublicServer s;
|
|
|
|
try
|
2023-07-03 23:24:35 -04:00
|
|
|
{
|
2023-08-21 10:58:17 -04:00
|
|
|
IEnumerable<PublicServer> isl = InternalServers.Where(a => (a.Domain == Domain && a.ApiVersion == Version));
|
|
|
|
if (isl.Any()) return isl.First();
|
2023-10-01 13:12:27 -04:00
|
|
|
s = await PublicServer.GetServer(Domain, Version, Secure);
|
2023-08-21 10:58:17 -04:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2023-08-25 12:07:36 -04:00
|
|
|
Console.WriteLine("Failed to connect to public server '{0}' using API {1}. No alternate server was found.", Domain, Version);
|
2023-08-21 10:58:17 -04:00
|
|
|
throw;
|
|
|
|
}
|
2023-08-25 12:07:36 -04:00
|
|
|
|
2023-08-21 10:58:17 -04:00
|
|
|
string? f = s.Storage.GetStorageDirectory(StorageDirectory.StorageInfo) + "token";
|
|
|
|
if (File.Exists(f))
|
|
|
|
{
|
|
|
|
f = File.ReadAllText(f);
|
|
|
|
}
|
|
|
|
else f = null;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (f is not null)
|
|
|
|
{
|
|
|
|
bool b = await s.LoginViaToken(f);
|
2023-10-01 12:28:10 -04:00
|
|
|
if (b)
|
|
|
|
{
|
|
|
|
Console.WriteLine("Auto Login Successful");
|
|
|
|
IsAnyServerLoggedin = true;
|
|
|
|
}
|
2023-08-21 10:58:17 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
s.Token = null;
|
|
|
|
s.Error = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-07-03 23:24:35 -04:00
|
|
|
InternalServers.Add(s);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2023-07-08 09:06:13 -04:00
|
|
|
public MainServer GetMainServer(string Domain, string Version = "v1")
|
2023-07-03 23:24:35 -04:00
|
|
|
{
|
2023-08-21 10:58:17 -04:00
|
|
|
DateTime dt = DateTime.UtcNow;
|
|
|
|
Console.WriteLine("Conecting to main server '{0}' using API {1}.", Domain, Version);
|
2023-10-01 13:12:27 -04:00
|
|
|
MainServer = new(Domain, Version)
|
2023-07-03 23:24:35 -04:00
|
|
|
{
|
2023-07-10 07:35:05 -04:00
|
|
|
ServerType = ServerType.Main
|
2023-07-03 23:24:35 -04:00
|
|
|
};
|
2023-08-21 10:58:17 -04:00
|
|
|
Console.WriteLine("Connected to main server '{0}' using API {1} in {2}.", Domain, Version, DateTime.UtcNow.Subtract(dt).ToString("g"));
|
2023-07-03 23:24:35 -04:00
|
|
|
return MainServer;
|
|
|
|
}
|
|
|
|
}
|