This commit is contained in:
2022-04-19 00:05:16 +02:00
parent e7c2ae7b8d
commit d7f2348f0d
20 changed files with 924 additions and 49 deletions

View File

@@ -1,4 +1,6 @@
using System.Net.Http.Json;
using System.Net.Mime;
using System.Text;
using System.Text.Json;
using APP.DTO;
@@ -7,25 +9,126 @@ namespace APP.Utility;
internal static class BackendComms
{
private const string ApiBaseUrl = "https://huso.hanami.family/api/";
private const string AuthHeader = "X-HUSO-AUTH";
private static readonly HttpClient Client = new();
private static readonly JsonSerializerOptions JsonSerializerOptions = new() {PropertyNameCaseInsensitive = true};
internal static async Task<List<SeasonAnime>?> GetSeason()
internal static async Task<List<Anime>?> GetSeason()
{
var resp = await Client.GetAsync(ApiBaseUrl + "season");
if (!resp.IsSuccessStatusCode) return null;
return await resp.Content.ReadFromJsonAsync<List<SeasonAnime>>(JsonSerializerOptions);
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<List<Anime>>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<User?> GetUser(string username)
internal static async Task<bool> Auth(string username, string secret)
{
if (string.IsNullOrWhiteSpace(username))
{
return false;
}
using var msg = new HttpRequestMessage(HttpMethod.Get, ApiBaseUrl + "auth/" + username);
msg.Headers.Add(AuthHeader, secret);
var resp = await Client.SendAsync(msg);
if (!resp.IsSuccessStatusCode)
{
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
}
return resp.IsSuccessStatusCode;
}
internal static async Task<Anime?> GetAnime(long animeId)
{
var resp = await Client.GetAsync(ApiBaseUrl + "anime/" + animeId);
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<Anime>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<List<Anime>?> SearchAnime(string query)
{
var resp = await Client.GetAsync(ApiBaseUrl + "animesearch?q=" + query);
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<List<Anime>>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<List<User>?> GetUser()
{
var resp = await Client.GetAsync(ApiBaseUrl + "user");
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<List<User>>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<User?> GetUser(string? username)
{
if (string.IsNullOrWhiteSpace(username))
{
return null;
}
var resp = await Client.GetAsync(ApiBaseUrl + "user/"+username);
if (!resp.IsSuccessStatusCode) return null;
var resp = await Client.GetAsync(ApiBaseUrl + "user/" + username);
if (!resp.IsSuccessStatusCode)
{
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
var users = await resp.Content.ReadFromJsonAsync<ICollection<User>>(JsonSerializerOptions);
return users?.SingleOrDefault();
}
}
internal static async Task<List<Watch>?> GetWatching()
{
var resp = await Client.GetAsync(ApiBaseUrl + "watch");
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<List<Watch>>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<List<Watch>?> GetWatching(string? username)
{
if (string.IsNullOrWhiteSpace(username))
{
return null;
}
var resp = await Client.GetAsync(ApiBaseUrl + "watch/" + username);
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<List<Watch>>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<List<WatchExtended>?> GetWatchingExtended()
{
var resp = await Client.GetAsync(ApiBaseUrl + "watchext");
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<List<WatchExtended>>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<List<WatchExtended>?> GetWatchingExtended(string? username)
{
if (string.IsNullOrWhiteSpace(username))
{
return null;
}
var resp = await Client.GetAsync(ApiBaseUrl + "watchext/" + username);
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<List<WatchExtended>>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
internal static async Task<Register?> Register(Register register)
{
using var content = new StringContent(JsonSerializer.Serialize(register), Encoding.UTF8, MediaTypeNames.Application.Json);
using var resp = await Client.PostAsync(ApiBaseUrl + "register", content);
if (resp.IsSuccessStatusCode) return await resp.Content.ReadFromJsonAsync<Register>(JsonSerializerOptions);
LogBoy.AddToLog(resp.RequestMessage?.RequestUri + " " + resp.StatusCode);
return null;
}
}

20
APP/Utility/LogBoy.cs Normal file
View File

@@ -0,0 +1,20 @@
using System.Text;
namespace APP.Utility;
internal static class LogBoy
{
private static readonly StringBuilder Log = new();
internal static void AddToLog(string text)
{
Log.Append('[' + DateTime.Now.ToShortTimeString() + "] ");
Log.Append(text);
Log.AppendLine();
}
internal static string GetLog()
{
return Log.ToString();
}
}

77
APP/Utility/SaveBoy.cs Normal file
View File

@@ -0,0 +1,77 @@
using System.Text.Json;
using APP.DTO;
namespace APP.Utility;
internal static class SaveBoy
{
private static readonly string AuthFile = ".." + Path.DirectorySeparatorChar + "reg.json";
internal static async Task<Auth?> ReadAuth()
{
if (File.Exists(AuthFile))
{
return await ReadFromFile<Auth>(AuthFile);
}
return null;
}
internal static void DeleteAuth()
{
if (File.Exists(AuthFile))
{
File.Delete(AuthFile);
}
}
internal static async Task SaveAuth(Auth auth)
{
await WriteToFile(AuthFile, auth);
}
private static async Task WriteToFile<T>(string filePath, T objectToWrite)
{
try
{
await using var sw = new StreamWriter(filePath, false);
try
{
var res = JsonSerializer.Serialize(objectToWrite);
await sw.WriteAsync(res);
}
finally
{
sw.Close();
}
}
catch (Exception ex)
{
await Console.Error.WriteLineAsync(ex.Message);
}
}
private static async Task<T?> ReadFromFile<T>(string filePath)
{
try
{
using var sr = new StreamReader(filePath);
try
{
var text = await sr.ReadToEndAsync();
var res = JsonSerializer.Deserialize<T>(text);
return res;
}
finally
{
sr.Close();
}
}
catch (Exception ex)
{
await Console.Error.WriteLineAsync(ex.Message);
}
return default;
}
}

View File

@@ -1,11 +1,14 @@
using System.Security.Cryptography;
using System.Text;
using APP.DTO;
namespace APP.Utility;
public static class StringAssemble
internal static class StringAssemble
{
public static string UserInfo(User? user)
private const string RegisterSecret = "綾波レイ";
internal static string UserInfo(User? user)
{
if (user == null)
{
@@ -31,7 +34,7 @@ public static class StringAssemble
return sb.ToString();
}
public static string UserData(User? user)
internal static string UserData(User? user)
{
if (user == null)
{
@@ -56,4 +59,20 @@ public static class StringAssemble
return sb.ToString();
}
internal static string CalcSha512(string input)
{
var bytes = Encoding.UTF8.GetBytes(input);
using var hash = SHA512.Create();
var hashedInputBytes = hash.ComputeHash(bytes);
var hashedInputStringBuilder = new StringBuilder(128);
foreach (var b in hashedInputBytes)
hashedInputStringBuilder.Append(b.ToString("X2"));
return hashedInputStringBuilder.ToString().ToLower();
}
internal static string CalcSauce(long malId, string username)
{
return CalcSha512(RegisterSecret + malId + username);
}
}