Review PowerDns types
This commit is contained in:
parent
ea5f732e24
commit
69b2de4613
4 changed files with 22 additions and 16 deletions
|
|
@ -23,6 +23,6 @@ public abstract class Method<TParam>(TParam parameters) : Method
|
|||
public TParam Parameters => parameters;
|
||||
}
|
||||
|
||||
public class InitializeMethod(InitializeMethodParameters parameters) : Method<InitializeMethodParameters>(parameters), IMethod;
|
||||
public class InitializeMethod(InitializeParameters parameters) : Method<InitializeParameters>(parameters), IMethod;
|
||||
|
||||
public class LookupMethod(LookupMethodParameters parameters) : Method<LookupMethodParameters>(parameters), IMethod;
|
||||
public class LookupMethod(LookupParameters parameters) : Method<LookupParameters>(parameters), IMethod;
|
||||
|
|
|
|||
|
|
@ -11,12 +11,12 @@ public record class MethodParameters : Parameters
|
|||
public Dictionary<string, JsonElement> AdditionalProperties { get; set; } = [];
|
||||
}
|
||||
|
||||
public record class InitializeMethodParameters(
|
||||
public record class InitializeParameters(
|
||||
[property: JsonPropertyName("command")] string Command,
|
||||
[property: JsonPropertyName("timeout")] int Timeout
|
||||
) : MethodParameters;
|
||||
|
||||
public record class LookupMethodParameters(
|
||||
public record class LookupParameters(
|
||||
[property: JsonPropertyName("qtype")] string Qtype,
|
||||
[property: JsonPropertyName("qname")] string Qname,
|
||||
[property: JsonPropertyName("remote")] string Remote,
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ public class PowerDnsHandler : ConnectionHandler
|
|||
catch (Exception e) { }
|
||||
|
||||
await JsonSerializer.SerializeAsync(writer, reply, ReplyContext.Default.Reply, connection.ConnectionClosed)
|
||||
.ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
|
||||
.ConfigureAwait(continueOnCapturedContext: false);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -106,22 +106,28 @@ public class PowerDnsHandler : ConnectionHandler
|
|||
InitializeMethod { Parameters: { } init } => HandleInitialize(init),
|
||||
LookupMethod { Parameters: { } lookup } => HandleLookup(lookup),
|
||||
|
||||
_ => ValueTask.FromResult<Reply>(new BoolReply(false))
|
||||
_ => LogUnhandled(_logger, method)
|
||||
};
|
||||
|
||||
static ValueTask<Reply> LogUnhandled(ILogger logger, Method method)
|
||||
{
|
||||
logger.LogWarning("Unhandled Method {Method}", method);
|
||||
return ValueTask.FromResult<Reply>(BoolReply.False);
|
||||
}
|
||||
}
|
||||
|
||||
private ValueTask<Reply> HandleInitialize(InitializeMethodParameters method)
|
||||
private ValueTask<Reply> HandleInitialize(InitializeParameters parameters)
|
||||
{
|
||||
return ValueTask.FromResult(BoolReply.True);
|
||||
return ValueTask.FromResult<Reply>(BoolReply.True);
|
||||
}
|
||||
|
||||
private ValueTask<Reply> HandleLookup(LookupMethodParameters method)
|
||||
private ValueTask<Reply> HandleLookup(LookupParameters parameters)
|
||||
{
|
||||
switch (method.Qtype.ToLowerInvariant())
|
||||
switch (parameters.Qtype.ToUpperInvariant())
|
||||
{
|
||||
default:
|
||||
_logger.LogWarning("Unhandled QType {QType}", method.Qtype);
|
||||
return ValueTask.FromResult(BoolReply.False);
|
||||
_logger.LogWarning("Unhandled QType {QType}", parameters.Qtype);
|
||||
return ValueTask.FromResult<Reply>(BoolReply.False);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,22 +7,22 @@ namespace pdns_dhcp.PowerDns;
|
|||
public abstract class Reply;
|
||||
|
||||
[JsonSerializable(typeof(Reply)), JsonSourceGenerationOptions(
|
||||
GenerationMode = JsonSourceGenerationMode.Serialization,
|
||||
PropertyNamingPolicy = JsonKnownNamingPolicy.KebabCaseLower,
|
||||
GenerationMode = JsonSourceGenerationMode.Metadata,
|
||||
WriteIndented = false
|
||||
)]
|
||||
internal partial class ReplyContext : JsonSerializerContext;
|
||||
|
||||
public abstract class Reply<T>(T result) : Reply
|
||||
{
|
||||
[JsonPropertyName("result")]
|
||||
public T Result => result;
|
||||
}
|
||||
|
||||
public class BoolReply(bool result) : Reply<bool>(result)
|
||||
{
|
||||
public static Reply False { get; } = new BoolReply(false);
|
||||
public static BoolReply False { get; } = new BoolReply(false);
|
||||
|
||||
public static Reply True { get; } = new BoolReply(true);
|
||||
public static BoolReply True { get; } = new BoolReply(true);
|
||||
}
|
||||
|
||||
public class LookupReply(QueryResult[] result) : Reply<QueryResult[]>(result);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue