Skip to content

Commit

Permalink
Add desired ducument type to request
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristian F. Thomsen committed Mar 28, 2023
1 parent 4721147 commit 9a3fea5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
29 changes: 27 additions & 2 deletions source/Api/OutgoingMessages/PeekRequestListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,33 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Application.Configuration.Authentication;
using Application.OutgoingMessages.Peek;
using Domain.OutgoingMessages.Peek;
using Domain.SeedWork;
using Infrastructure.IncomingMessages;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;

namespace Api.OutgoingMessages;

public class PeekRequestListener
{
private readonly MessagePeeker _messagePeeker;
private readonly IMarketActorAuthenticator _authenticator;
private readonly ILogger<PeekRequestListener> _logger;

public PeekRequestListener(MessagePeeker messagePeeker, IMarketActorAuthenticator authenticator)
public PeekRequestListener(MessagePeeker messagePeeker, IMarketActorAuthenticator authenticator, ILogger<PeekRequestListener> logger)
{
_messagePeeker = messagePeeker;
_authenticator = authenticator;
_logger = logger;
}

[Function("PeekRequestListener")]
Expand All @@ -44,9 +51,20 @@ public async Task<HttpResponseData> RunAsync(
FunctionContext executionContext,
string messageCategory)
{
ArgumentNullException.ThrowIfNull(request);

var contentType = GetContentType(request.Headers);
var desiredDocumentFormat = CimFormatParser.ParseFromContentTypeHeaderValue(contentType);
if (desiredDocumentFormat is null)
{
_logger.LogInformation($"Could not parse desired CIM format from Content-Type header value: {contentType}");
return request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
}

var result = await _messagePeeker.PeekAsync(
_authenticator.CurrentIdentity.Number,
EnumerationType.FromName<MessageCategory>(messageCategory))
EnumerationType.FromName<MessageCategory>(messageCategory),
desiredDocumentFormat)
.ConfigureAwait(false);

var response = HttpResponseData.CreateResponse(request);
Expand All @@ -68,4 +86,11 @@ public async Task<HttpResponseData> RunAsync(
response.StatusCode = HttpStatusCode.OK;
return response;
}

private static string GetContentType(HttpHeaders headers)
{
var contentHeader = headers.GetValues("Content-Type").FirstOrDefault();
if (contentHeader == null) throw new InvalidOperationException("No Content-Type found in request headers");
return contentHeader;
}
}
5 changes: 3 additions & 2 deletions source/Application/OutgoingMessages/Peek/MessagePeeker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using System.Threading.Tasks;
using Domain.Actors;
using Domain.OutgoingMessages;
using Domain.OutgoingMessages.Peek;
using MediatR;
using Microsoft.EntityFrameworkCore;
Expand All @@ -29,11 +30,11 @@ public MessagePeeker(IMediator mediator)
_mediator = mediator;
}

public async Task<PeekResult> PeekAsync(ActorNumber actorNumber, MessageCategory messageCategory)
public async Task<PeekResult> PeekAsync(ActorNumber actorNumber, MessageCategory messageCategory, DocumentFormat desiredDocumentFormat)
{
try
{
return await _mediator.Send(new PeekRequest(actorNumber, messageCategory)).ConfigureAwait(false);
return await _mediator.Send(new PeekRequest(actorNumber, messageCategory, desiredDocumentFormat)).ConfigureAwait(false);
}
catch (DbUpdateException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@ private async Task<BundledMessage> CreateBundledMessageAsync(MessageRecords mess
}
}

public record PeekRequest(ActorNumber ActorNumber, MessageCategory MessageCategory) : ICommand<PeekResult>;
public record PeekRequest(ActorNumber ActorNumber, MessageCategory MessageCategory, DocumentFormat DesiredDocumentFormat) : ICommand<PeekResult>;

public record PeekResult(Stream? Bundle, Guid? MessageId = default);
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public async Task Dequeue_is_Successful()
await GivenAMoveInTransactionHasBeenAccepted().ConfigureAwait(false);
var peekResult = await InvokeCommandAsync(new PeekRequest(
ActorNumber.Create(SampleData.NewEnergySupplierNumber),
MessageCategory.MasterData)).ConfigureAwait(false);
MessageCategory.MasterData,
DocumentFormat.Xml)).ConfigureAwait(false);

var dequeueResult = await InvokeCommandAsync(new DequeueRequest(peekResult.MessageId.GetValueOrDefault().ToString())).ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ public async Task Return_empty_bundle_if_bundle_is_already_registered()
Assert.Null(peekResult.Bundle);
}

private static PeekRequest CreatePeekRequest(MessageCategory messageCategory)
{
return new PeekRequest(ActorNumber.Create(SampleData.NewEnergySupplierNumber), messageCategory);
}

private static IncomingMessageBuilder MessageBuilder()
{
return new IncomingMessageBuilder()
Expand Down Expand Up @@ -188,6 +183,6 @@ private async Task<bool> BundleIsRegistered()

private Task<PeekResult> PeekMessage(MessageCategory category)
{
return _messagePeeker.PeekAsync(ActorNumber.Create(SampleData.NewEnergySupplierNumber), category);
return _messagePeeker.PeekAsync(ActorNumber.Create(SampleData.NewEnergySupplierNumber), category, DocumentFormat.Xml);
}
}

0 comments on commit 9a3fea5

Please sign in to comment.