25
Luski.net.sln
Executable file
25
Luski.net.sln
Executable file
@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.0.31717.71
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Luski.net", "Luski.net\Luski.net.csproj", "{3DF9B870-51B3-4338-84EC-75E4B8802F0C}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{3DF9B870-51B3-4338-84EC-75E4B8802F0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{3DF9B870-51B3-4338-84EC-75E4B8802F0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{3DF9B870-51B3-4338-84EC-75E4B8802F0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{3DF9B870-51B3-4338-84EC-75E4B8802F0C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {49AFEA24-10EC-4D2C-B99C-C3E70124E443}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
163
Luski.net/API.cs
Normal file
163
Luski.net/API.cs
Normal file
@ -0,0 +1,163 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json.Serialization.Metadata;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Luski.net.Enums;
|
||||
using Luski.Shared.PublicServers.V1.Enums;
|
||||
using Luski.Shared.PublicServers.V1.ServerToClient.HTTP;
|
||||
|
||||
namespace Luski.net;
|
||||
|
||||
public class API
|
||||
{
|
||||
[JsonIgnore]
|
||||
public MainServer MainServer { get; internal set; }
|
||||
|
||||
public bool IsAnyServerLoggedin { get; internal set; }
|
||||
|
||||
public const string DefaultVersion = "v1";
|
||||
|
||||
internal List<string> Versions = new()
|
||||
{
|
||||
DefaultVersion
|
||||
};
|
||||
|
||||
public IReadOnlyList<string> SupportedVersions => Versions.AsReadOnly();
|
||||
internal List<PublicServer> InternalServers { get; } = new();
|
||||
public IReadOnlyList<PublicServer> LoadedServers => InternalServers.AsReadOnly();
|
||||
|
||||
internal List<PublicServer> InternalFailedServers { get; } = new();
|
||||
public IReadOnlyList<PublicServer> FailedServers => InternalFailedServers.AsReadOnly();
|
||||
|
||||
private static HttpResponseMessage GetFromServer(string Domain, string ApiVersion, bool Secure, string Path, CancellationToken CancellationToken, params KeyValuePair<string, string?>[] Headers)
|
||||
{
|
||||
using HttpClient web = new();
|
||||
web.Timeout = TimeSpan.FromSeconds(10);
|
||||
if (Headers is not null && Headers.Length > 0) foreach (KeyValuePair<string, string?> header in Headers) web.DefaultRequestHeaders.Add(header.Key, header.Value);
|
||||
return web.GetAsync($"{(Secure ? "https" : "http" )}://{Domain}/{ApiVersion}/{Path}", cancellationToken: CancellationToken).Result;
|
||||
}
|
||||
private static Task<Tresult> GetFromServer<Tresult>(string Domain, string ApiVersion, bool Secure, string Path, JsonTypeInfo<Tresult> Type, CancellationToken CancellationToken, params KeyValuePair<string, string?>[] Headers) where Tresult : STC, new()
|
||||
{
|
||||
HttpResponseMessage ServerResponce = GetFromServer(Domain, ApiVersion, Secure, Path, CancellationToken, Headers);
|
||||
Tresult temp = new();
|
||||
string raw = ServerResponce.Content.ReadAsStringAsync(CancellationToken).Result;
|
||||
try
|
||||
{
|
||||
temp = JsonSerializer.Deserialize(raw, Type)!;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("JSON parse failed for the following data as type {0}\n{1}", temp.GetType(), raw);
|
||||
}
|
||||
if (temp is null) return Task.FromResult(new Tresult() { StatusCode = ServerResponce.StatusCode, Error = ErrorCode.ServerError, ErrorMessage = $"Server responded with empty data" });
|
||||
return Task.FromResult(temp);
|
||||
}
|
||||
|
||||
public Task<ServerInfoSTC> GetServerInfo(string Domain, string Version = DefaultVersion,
|
||||
bool Secure = true)
|
||||
{
|
||||
ServerInfoSTC? si = GetFromServer(Domain, Version, Secure, "socketserver", ServerInfoSTCContext.Default.ServerInfoSTC, CancellationToken.None).Result;
|
||||
if (si is null) throw new Exception("Bad Response");
|
||||
return Task.FromResult(si);
|
||||
}
|
||||
|
||||
public Task<bool> TryGetServerInfo([NotNullWhen(true)]out ServerInfoSTC? si, string Domain, string Version = DefaultVersion,
|
||||
bool Secure = true)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
si = GetServerInfo(Domain, Version, Secure).Result;
|
||||
return Task.FromResult(true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
si = null;
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
}
|
||||
|
||||
public Task<bool> TryGetPublicServer(out PublicServer Server, string Domain, string Version = DefaultVersion,
|
||||
bool Secure = true, bool GenerateEncryption = true, bool LogConsole = false)
|
||||
{
|
||||
try
|
||||
{
|
||||