client-dotnet/src/INetMock.Client/Audit/Details.cs
Peter Kurfer 15106dbfd6
Add audit API client
- cleanup
- generate PCAP API
2021-02-20 15:18:08 +01:00

90 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using Google.Protobuf.WellKnownTypes;
using INetMock.Client.Audit.Details;
namespace INetMock.Client.Audit
{
public abstract record EventDetails;
public record EmptyDetails : EventDetails;
public record GenericDetails : EventDetails
{
private readonly Any? _detailsAny;
public GenericDetails()
{
_detailsAny = null;
}
public GenericDetails(Any? any)
{
_detailsAny = any;
}
public static implicit operator HttpDetails(GenericDetails gd)
{
if (gd._detailsAny == null || gd._detailsAny.Value == null) return new();
if (!gd._detailsAny.TypeUrl.EndsWith(HTTPDetailsEntity.Descriptor.FullName))
throw new InvalidOperationException();
return new HttpDetails(gd._detailsAny);
}
public static implicit operator DnsDetails(GenericDetails gd)
{
if (gd._detailsAny == null || gd._detailsAny.Value == null) return new();
if (!gd._detailsAny.TypeUrl.EndsWith(HTTPDetailsEntity.Descriptor.FullName))
throw new InvalidOperationException();
return new DnsDetails(gd._detailsAny);
}
}
public record HttpDetails : EventDetails
{
public HttpDetails()
{
}
public HttpDetails(Any? any)
{
if (any == null || any.Value == null) return;
var detailsEntity = HTTPDetailsEntity.Parser.ParseFrom(any.Value);
Method = new HttpMethod(detailsEntity.Method.ToString());
Host = detailsEntity.Host;
Uri = detailsEntity.Uri;
Proto = detailsEntity.Proto;
Headers = new INetMockHttpHeaders(detailsEntity.Headers);
}
public HttpMethod Method { get; init; }
public string Host { get; init; }
public string Uri { get; init; }
public string Proto { get; init; }
public HttpHeaders Headers { get; init; }
}
public record DnsDetails : EventDetails
{
public DnsDetails()
{
}
public DNSOpCode OpCode { get; init; }
public IReadOnlyList<DNSQuestionEntity> Questions { get; init; }
public DnsDetails(Any? any)
{
if (any == null || any.Value == null) return;
var entity = DNSDetailsEntity.Parser.ParseFrom(any.Value);
OpCode = entity.Opcode;
Questions = entity.Questions;
}
}
}