party migrate to Pilz.Net
This commit is contained in:
@@ -1,29 +1,25 @@
|
|||||||
using Newtonsoft.Json;
|
using OwnChar.Api.Clients;
|
||||||
using OwnChar.Api.Clients;
|
|
||||||
using OwnChar.Api.Exceptions;
|
using OwnChar.Api.Exceptions;
|
||||||
using OwnChar.Api.Packets;
|
using OwnChar.Api.Packets;
|
||||||
using System.Net.Http.Headers;
|
using Pilz.Cryptography;
|
||||||
using System.Net.Http.Json;
|
using Pilz.Net.Api;
|
||||||
|
|
||||||
namespace OwnChar.Api;
|
namespace OwnChar.Api;
|
||||||
|
|
||||||
public class OwnCharApiClient
|
public class OwnCharApiClient : ApiClient
|
||||||
{
|
{
|
||||||
private readonly HttpClient httpClient = new();
|
|
||||||
private readonly Dictionary<Type, object> clients = [];
|
private readonly Dictionary<Type, object> clients = [];
|
||||||
|
|
||||||
internal string? AuthSecret { get; set; } = null;
|
internal string? AuthSecret { get; set; } = null;
|
||||||
public bool IsLoggedIn => AuthSecret != null;
|
public bool IsLoggedIn => AuthSecret != null;
|
||||||
public string ApiUrl { get; }
|
|
||||||
|
|
||||||
public LoginApiClient Auth { get; }
|
public LoginApiClient Auth { get; }
|
||||||
public UsersApiClient Users { get; }
|
public UsersApiClient Users { get; }
|
||||||
public CharactersApiClient Characters { get; }
|
public CharactersApiClient Characters { get; }
|
||||||
public GroupsApiClient Groups { get; }
|
public GroupsApiClient Groups { get; }
|
||||||
|
|
||||||
public OwnCharApiClient(string apiUrl)
|
public OwnCharApiClient(string apiUrl) : base(apiUrl)
|
||||||
{
|
{
|
||||||
this.ApiUrl = apiUrl;
|
|
||||||
Auth = GetClient<LoginApiClient>();
|
Auth = GetClient<LoginApiClient>();
|
||||||
Users = GetClient<UsersApiClient>();
|
Users = GetClient<UsersApiClient>();
|
||||||
Characters = GetClient<CharactersApiClient>();
|
Characters = GetClient<CharactersApiClient>();
|
||||||
@@ -46,24 +42,8 @@ public class OwnCharApiClient
|
|||||||
throw new Exception("Client could not be created!");
|
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 new SecureString(base.EncodeAuthKey(), false);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,12 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.7" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="8.0.8" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.7" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
|
||||||
<PackageReference Include="Pilz.Cryptography" Version="2.0.1" />
|
<PackageReference Include="Pilz.Cryptography" Version="2.1.1" />
|
||||||
|
<PackageReference Include="Pilz.Net" Version="2.0.4" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user