rename MessageSerializer to ApiMessageSerializer

This commit is contained in:
2024-10-17 10:10:00 +02:00
parent 9c1723cb0e
commit 8a8407d86e
7 changed files with 17 additions and 17 deletions

View File

@@ -10,7 +10,7 @@ public class ApiClient(string apiUrl) : IApiClient
public string? AuthKey { get; set; } public string? AuthKey { get; set; }
public virtual IMessageSerializer Serializer { get; set; } = new DefaultMessageSerializer(); public virtual IApiMessageSerializer Serializer { get; set; } = new DefaultApiMessageSerializer();
public ILogger Log { get; set; } = NullLogger.Instance; public ILogger Log { get; set; } = NullLogger.Instance;
@@ -24,7 +24,7 @@ public class ApiClient(string apiUrl) : IApiClient
return SendRequest(route, message, null); return SendRequest(route, message, null);
} }
public virtual async Task<ApiResponse> SendRequest(string route, ApiMessage? message, IMessageSerializer? serializer) public virtual async Task<ApiResponse> SendRequest(string route, ApiMessage? message, IApiMessageSerializer? serializer)
{ {
serializer ??= Serializer; serializer ??= Serializer;
@@ -45,7 +45,7 @@ public class ApiClient(string apiUrl) : IApiClient
return SendRequest<TResponse>(route, message, null); return SendRequest<TResponse>(route, message, null);
} }
public virtual async Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route, ApiMessage? message, IMessageSerializer? serializer) where TResponse : ApiMessage public virtual async Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route, ApiMessage? message, IApiMessageSerializer? serializer) where TResponse : ApiMessage
{ {
serializer ??= Serializer; serializer ??= Serializer;
@@ -60,7 +60,7 @@ public class ApiClient(string apiUrl) : IApiClient
return new(res.StatusCode, result); return new(res.StatusCode, result);
} }
protected virtual async Task<HttpResponseMessage> Send(string route, ApiMessage? message, IMessageSerializer serializer) protected virtual async Task<HttpResponseMessage> Send(string route, ApiMessage? message, IApiMessageSerializer serializer)
{ {
var url = ApiUrl + route; var url = ApiUrl + route;
HttpContent content; HttpContent content;

View File

@@ -1,7 +1,7 @@
namespace Pilz.Net.Api; namespace Pilz.Net.Api;
[AttributeUsage(AttributeTargets.Method)] [AttributeUsage(AttributeTargets.Method)]
public class MessageHandlerAttribute(string route) : Attribute public class ApiMessageHandlerAttribute(string route) : Attribute
{ {
public string Route { get; set; } = route; public string Route { get; set; } = route;
public Type? Serializer { get; set; } public Type? Serializer { get; set; }

View File

@@ -9,7 +9,7 @@ namespace Pilz.Net.Api;
public class ApiServer(string apiUrl) : IApiServer public class ApiServer(string apiUrl) : IApiServer
{ {
protected readonly Dictionary<string, Delegate> handlers = []; protected readonly Dictionary<string, Delegate> handlers = [];
protected readonly Dictionary<Type, IMessageSerializer> serializers = []; protected readonly Dictionary<Type, IApiMessageSerializer> serializers = [];
protected readonly HttpListener httpListener = new(); protected readonly HttpListener httpListener = new();
public event OnCheckAuthenticationEventHandler? OnCheckAuthentication; public event OnCheckAuthenticationEventHandler? OnCheckAuthentication;
@@ -20,7 +20,7 @@ public class ApiServer(string apiUrl) : IApiServer
public virtual bool EnableAuth { get; set; } public virtual bool EnableAuth { get; set; }
public IMessageSerializer Serializer { get; set; } = new DefaultMessageSerializer(); public IApiMessageSerializer Serializer { get; set; } = new DefaultApiMessageSerializer();
public ILogger Log { get; set; } = NullLogger.Instance; public ILogger Log { get; set; } = NullLogger.Instance;
@@ -58,7 +58,7 @@ public class ApiServer(string apiUrl) : IApiServer
var method = handler.Method; var method = handler.Method;
// Sanity checks // Sanity checks
if (method.GetCustomAttribute<MessageHandlerAttribute>() is not MessageHandlerAttribute attribute if (method.GetCustomAttribute<ApiMessageHandlerAttribute>() is not ApiMessageHandlerAttribute attribute
|| !method.ReturnType.IsAssignableTo(typeof(ApiResult))) || !method.ReturnType.IsAssignableTo(typeof(ApiResult)))
{ {
if (throwOnError) if (throwOnError)
@@ -155,7 +155,7 @@ public class ApiServer(string apiUrl) : IApiServer
// Get handler // Get handler
Log.Debug("Find handler"); Log.Debug("Find handler");
if (!handlers.TryGetValue(url, out var handler) if (!handlers.TryGetValue(url, out var handler)
|| handler.Method.GetCustomAttribute<MessageHandlerAttribute>() is not MessageHandlerAttribute attribute) || handler.Method.GetCustomAttribute<ApiMessageHandlerAttribute>() is not ApiMessageHandlerAttribute attribute)
return null; return null;
// Check authentication // Check authentication
@@ -220,13 +220,13 @@ public class ApiServer(string apiUrl) : IApiServer
return [.. objs]; return [.. objs];
} }
protected virtual IMessageSerializer GetSerializer(Type? t) protected virtual IApiMessageSerializer GetSerializer(Type? t)
{ {
if (t is not null) if (t is not null)
{ {
if (serializers.TryGetValue(t, out var s) && s is not null) if (serializers.TryGetValue(t, out var s) && s is not null)
return s; return s;
else if (Activator.CreateInstance(t) is IMessageSerializer ss) else if (Activator.CreateInstance(t) is IApiMessageSerializer ss)
{ {
serializers.Add(t, ss); serializers.Add(t, ss);
return ss; return ss;

View File

@@ -2,7 +2,7 @@
namespace Pilz.Net.Api; namespace Pilz.Net.Api;
public class DefaultMessageSerializer : IMessageSerializer public class DefaultApiMessageSerializer : IApiMessageSerializer
{ {
private static JsonSerializerSettings? defaultSerializerSettings; private static JsonSerializerSettings? defaultSerializerSettings;

View File

@@ -8,7 +8,7 @@ public interface IApiClient
string? AuthKey { get; set; } string? AuthKey { get; set; }
IMessageSerializer Serializer { get; } IApiMessageSerializer Serializer { get; }
ILogger Log { get; set; } ILogger Log { get; set; }
@@ -16,11 +16,11 @@ public interface IApiClient
Task<ApiResponse> SendRequest(string route, ApiMessage? message); Task<ApiResponse> SendRequest(string route, ApiMessage? message);
Task<ApiResponse> SendRequest(string route, ApiMessage? message, IMessageSerializer? serializer); Task<ApiResponse> SendRequest(string route, ApiMessage? message, IApiMessageSerializer? serializer);
Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route) where TResponse : ApiMessage; Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route) where TResponse : ApiMessage;
Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route, ApiMessage? message) where TResponse : ApiMessage; Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route, ApiMessage? message) where TResponse : ApiMessage;
Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route, ApiMessage? message, IMessageSerializer? serializer) where TResponse : ApiMessage; Task<ApiResponse<TResponse>> SendRequest<TResponse>(string route, ApiMessage? message, IApiMessageSerializer? serializer) where TResponse : ApiMessage;
} }

View File

@@ -1,6 +1,6 @@
namespace Pilz.Net.Api; namespace Pilz.Net.Api;
public interface IMessageSerializer public interface IApiMessageSerializer
{ {
string? Serialize(ApiMessage message); string? Serialize(ApiMessage message);
ApiMessage? Deserialize(string json, Type target); ApiMessage? Deserialize(string json, Type target);

View File

@@ -12,7 +12,7 @@ public interface IApiServer
bool EnableAuth { get; set; } bool EnableAuth { get; set; }
IMessageSerializer Serializer { get; } IApiMessageSerializer Serializer { get; }
ILogger Log { get; set; } ILogger Log { get; set; }