party migrate to Pilz.Net

This commit is contained in:
2024-08-22 15:13:08 +02:00
parent 24708eb798
commit 6b5445ccdf
2 changed files with 13 additions and 32 deletions

View File

@@ -1,29 +1,25 @@
using Newtonsoft.Json;
using OwnChar.Api.Clients;
using OwnChar.Api.Clients;
using OwnChar.Api.Exceptions;
using OwnChar.Api.Packets;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using Pilz.Cryptography;
using Pilz.Net.Api;
namespace OwnChar.Api;
public class OwnCharApiClient
public class OwnCharApiClient : ApiClient
{
private readonly HttpClient httpClient = new();
private readonly Dictionary<Type, object> clients = [];
internal string? AuthSecret { get; set; } = null;
public bool IsLoggedIn => AuthSecret != null;
public string ApiUrl { get; }
public LoginApiClient Auth { get; }
public UsersApiClient Users { get; }
public CharactersApiClient Characters { get; }
public GroupsApiClient Groups { get; }
public OwnCharApiClient(string apiUrl)
public OwnCharApiClient(string apiUrl) : base(apiUrl)
{
this.ApiUrl = apiUrl;
Auth = GetClient<LoginApiClient>();
Users = GetClient<UsersApiClient>();
Characters = GetClient<CharactersApiClient>();
@@ -46,24 +42,8 @@ public class OwnCharApiClient
throw new Exception("Client could not be created!");
}
public async Task<TResponse> MakeRequest<TRequest, TResponse>(string requestUrl, TRequest request) where TRequest : OwnCharRequest where TResponse : OwnCharResponse
protected override string? EncodeAuthKey()
{
if (await TryMakeRequest<TRequest, TResponse>(requestUrl, request) is TResponse response)
return response;
throw new ApiException(string.Format("The API request to \"{0}\" failed!", requestUrl));
}
public async Task<TResponse?> TryMakeRequest<TRequest, TResponse>(string requestUrl, TRequest request) where TRequest : OwnCharRequest where TResponse : OwnCharResponse
{
request.AuthSecret = AuthSecret;
var fullRequestUrl = ApiUrl + requestUrl;
var content = new StringContent(JsonHelpers.SerializeRequest(request)!, null, "application/json");
var res = await httpClient.PostAsync(fullRequestUrl, content);
if (res.IsSuccessStatusCode)
return JsonHelpers.DeserializeResponse<TResponse>(await res.Content.ReadAsStringAsync());
return null;
return new SecureString(base.EncodeAuthKey(), false);
}
}

View File

@@ -7,11 +7,12 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.7" />
<PackageReference Include="Pilz.Cryptography" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
<PackageReference Include="Pilz.Cryptography" Version="2.1.1" />
<PackageReference Include="Pilz.Net" Version="2.0.4" />
</ItemGroup>
</Project>