Skip to content

Commit

Permalink
Adding prefix endpoint to retry (#141)
Browse files Browse the repository at this point in the history
* chore: adding prefix to retry

* fix: some discussions about the constructor

* feat: solving some discussions

* fix: some discussions

* feat: update endpoint

* feat: changing endpoint and cleaning code

---------

Co-authored-by: ext.joao.veloso <[email protected]>
  • Loading branch information
astrinh0 and ext.joao.veloso authored Oct 30, 2023
1 parent 366e938 commit 42096fa
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 31 deletions.
34 changes: 27 additions & 7 deletions src/KafkaFlow.Retry.API/AppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,65 @@

public static class AppBuilderExtensions
{

public static IApplicationBuilder UseKafkaFlowRetryEndpoints(
this IApplicationBuilder appBuilder
)
this IApplicationBuilder appBuilder,
string endpointPrefix)
{
var retryDurableQueueRepositoryProvider =
appBuilder
.ApplicationServices
.GetService(typeof(IRetryDurableQueueRepositoryProvider)) as IRetryDurableQueueRepositoryProvider;

appBuilder.UseRetryEndpoints(retryDurableQueueRepositoryProvider);
appBuilder.UseRetryEndpoints(retryDurableQueueRepositoryProvider, endpointPrefix);

return appBuilder;
}

public static IApplicationBuilder UseKafkaFlowRetryEndpoints(
this IApplicationBuilder appBuilder)
{
return appBuilder.UseKafkaFlowRetryEndpoints(string.Empty);
}

public static IApplicationBuilder UseRetryEndpoints(
this IApplicationBuilder appBuilder,
IRetryDurableQueueRepositoryProvider retryDurableQueueRepositoryProvider
IRetryDurableQueueRepositoryProvider retryDurableQueueRepositoryProvider,
string endpointPrefix
)
{
appBuilder.UseMiddleware<RetryMiddleware>(
new GetItemsHandler(
retryDurableQueueRepositoryProvider,
new GetItemsRequestDtoReader(),
new GetItemsInputAdapter(),
new GetItemsResponseDtoAdapter()));
new GetItemsResponseDtoAdapter(),
endpointPrefix));

appBuilder.UseMiddleware<RetryMiddleware>(
new PatchItemsHandler(
retryDurableQueueRepositoryProvider,
new UpdateItemsInputAdapter(),
new UpdateItemsResponseDtoAdapter()));
new UpdateItemsResponseDtoAdapter(),
endpointPrefix));

appBuilder.UseMiddleware<RetryMiddleware>(
new PatchQueuesHandler(
retryDurableQueueRepositoryProvider,
new UpdateQueuesInputAdapter(),
new UpdateQueuesResponseDtoAdapter()));
new UpdateQueuesResponseDtoAdapter(),
endpointPrefix));

return appBuilder;
}

public static IApplicationBuilder UseRetryEndpoints(
this IApplicationBuilder appBuilder,
IRetryDurableQueueRepositoryProvider retryDurableQueueRepositoryProvider
)
{
return appBuilder.UseRetryEndpoints(retryDurableQueueRepositoryProvider, string.Empty);
}

}
}
5 changes: 2 additions & 3 deletions src/KafkaFlow.Retry.API/Handlers/GetItemsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public GetItemsHandler(
IRetryDurableQueueRepositoryProvider retryDurableQueueRepositoryProvider,
IGetItemsRequestDtoReader getItemsRequestDtoReader,
IGetItemsInputAdapter getItemsInputAdapter,
IGetItemsResponseDtoAdapter getItemsResponseDtoAdapter)
IGetItemsResponseDtoAdapter getItemsResponseDtoAdapter,
string endpointPrefix) : base(endpointPrefix, "items")
{
Guard.Argument(retryDurableQueueRepositoryProvider, nameof(retryDurableQueueRepositoryProvider)).NotNull();
Guard.Argument(getItemsRequestDtoReader, nameof(getItemsRequestDtoReader)).NotNull();
Expand All @@ -31,8 +32,6 @@ public GetItemsHandler(
this.getItemsResponseDtoAdapter = getItemsResponseDtoAdapter;
}

protected override string ResourcePath => base.ResourcePath.ExtendResourcePath("items");

protected override HttpMethod HttpMethod => HttpMethod.GET;

protected override async Task HandleRequestAsync(HttpRequest request, HttpResponse response)
Expand Down
5 changes: 2 additions & 3 deletions src/KafkaFlow.Retry.API/Handlers/PatchItemsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ internal class PatchItemsHandler : RetryRequestHandlerBase
public PatchItemsHandler(
IRetryDurableQueueRepositoryProvider retryDurableQueueRepositoryProvider,
IUpdateItemsInputAdapter updateItemsInputAdapter,
IUpdateItemsResponseDtoAdapter updateItemsResponseDtoAdapter)
IUpdateItemsResponseDtoAdapter updateItemsResponseDtoAdapter,
string endpointPrefix) : base(endpointPrefix, "items")
{
this.retryDurableQueueRepositoryProvider = retryDurableQueueRepositoryProvider;
this.updateItemsInputAdapter = updateItemsInputAdapter;
this.updateItemsResponseDtoAdapter = updateItemsResponseDtoAdapter;
}

protected override string ResourcePath => base.ResourcePath.ExtendResourcePath("items");

protected override HttpMethod HttpMethod => HttpMethod.PATCH;

protected override async Task HandleRequestAsync(HttpRequest request, HttpResponse response)
Expand Down
4 changes: 2 additions & 2 deletions src/KafkaFlow.Retry.API/Handlers/PatchQueuesHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ internal class PatchQueuesHandler : RetryRequestHandlerBase
public PatchQueuesHandler(
IRetryDurableQueueRepositoryProvider retryDurableQueueRepositoryProvider,
IUpdateQueuesInputAdapter updateQueuesInputAdapter,
IUpdateQueuesResponseDtoAdapter updateQueuesResponseDtoAdapter)
IUpdateQueuesResponseDtoAdapter updateQueuesResponseDtoAdapter,
string endpointPrefix) : base(endpointPrefix, "queues")
{
this.retryDurableQueueRepositoryProvider = retryDurableQueueRepositoryProvider;
this.updateQueuesInputAdapter = updateQueuesInputAdapter;
this.updateQueuesResponseDtoAdapter = updateQueuesResponseDtoAdapter;
}

protected override string ResourcePath => base.ResourcePath.ExtendResourcePath("queues");

protected override HttpMethod HttpMethod => HttpMethod.PATCH;

Expand Down
24 changes: 22 additions & 2 deletions src/KafkaFlow.Retry.API/RetryRequestHandlerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Dawn;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;

internal abstract class RetryRequestHandlerBase : IHttpRequestHandler
{

private readonly string path;
private const string RetryResource = "retry";


protected JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
Expand All @@ -16,7 +22,21 @@ internal abstract class RetryRequestHandlerBase : IHttpRequestHandler

protected abstract HttpMethod HttpMethod { get; }

protected virtual string ResourcePath => "/retry";
protected RetryRequestHandlerBase(string endpointPrefix, string resource)
{
Guard.Argument(resource, nameof(resource)).NotNull().NotEmpty();

if (!string.IsNullOrEmpty(endpointPrefix))
{
this.path = this.path
.ExtendResourcePath(endpointPrefix);
}

this.path = this.path
.ExtendResourcePath(RetryResource)
.ExtendResourcePath(resource);
}


public virtual async Task<bool> HandleAsync(HttpRequest request, HttpResponse response)
{
Expand All @@ -34,7 +54,7 @@ protected bool CanHandle(HttpRequest httpRequest)
{
var resource = httpRequest.Path.ToUriComponent();

if (!resource.Equals(this.ResourcePath))
if (!resource.Equals(this.path))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public class GetItemsHandlerTests
private readonly Mock<IGetItemsInputAdapter> mockGetItemsInputAdapter = new Mock<IGetItemsInputAdapter>();
private readonly Mock<IGetItemsRequestDtoReader> mockGetItemsRequestDtoReader = new Mock<IGetItemsRequestDtoReader>();
private readonly Mock<IGetItemsResponseDtoAdapter> mockGetItemsResponseDtoReader = new Mock<IGetItemsResponseDtoAdapter>();
private readonly string resourcePath = "/retry/items";
private readonly string resourcePath = "/testendpoint/retry/items";
private readonly Mock<IRetryDurableQueueRepositoryProvider> retryDurableQueueRepositoryProvider = new Mock<IRetryDurableQueueRepositoryProvider>();

[Fact]
public async Task GetItemsHandler_HandleAsync_Success()
public async Task GetItemsHandler_HandleAsync_WithEndpointPrefix_Success()
{
// Arrange
var httpContext = this.CreateHttpContext();
Expand Down Expand Up @@ -61,7 +61,8 @@ public async Task GetItemsHandler_HandleAsync_Success()
retryDurableQueueRepositoryProvider.Object,
mockGetItemsRequestDtoReader.Object,
mockGetItemsInputAdapter.Object,
mockGetItemsResponseDtoReader.Object
mockGetItemsResponseDtoReader.Object,
"testendpoint"
);

// Act
Expand All @@ -79,7 +80,7 @@ public async Task GetItemsHandler_HandleAsync_Success()

[Theory]
[ClassData(typeof(DependenciesThrowingExceptionsData))]
public async Task GetItemsHandler_HandleAsync_WithException_ReturnsExpectedStatusCode(
public async Task GetItemsHandler_HandleAsync_WithExceptionAndEndpointPrefix_ReturnsExpectedStatusCode(
IGetItemsRequestDtoReader getItemsRequestDtoReader,
IGetItemsInputAdapter getItemsInputAdapter,
IRetryDurableQueueRepositoryProvider retryQueueDataProvider,
Expand All @@ -93,7 +94,8 @@ public async Task GetItemsHandler_HandleAsync_WithException_ReturnsExpectedStatu
retryQueueDataProvider,
getItemsRequestDtoReader,
getItemsInputAdapter,
getItemsResponseDtoAdapter
getItemsResponseDtoAdapter,
"testendpoint"
);

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public async Task PatchItemsHandler_HandleAsync_Success()
var handler = new PatchItemsHandler(
retryQueueDataProvider.Object,
mockUpdateItemsInputAdapter.Object,
mockUpdateItemsResponseDtoAdapter.Object
mockUpdateItemsResponseDtoAdapter.Object,
string.Empty
);

// Act
Expand Down Expand Up @@ -103,7 +104,8 @@ public async Task PatchItemsHandler_HandleAsync_WithErrorInDeserialization()
var handler = new PatchItemsHandler(
Mock.Of<IRetryDurableQueueRepositoryProvider>(),
Mock.Of<IUpdateItemsInputAdapter>(),
Mock.Of<IUpdateItemsResponseDtoAdapter>()
Mock.Of<IUpdateItemsResponseDtoAdapter>(),
string.Empty
);

// act
Expand All @@ -129,7 +131,8 @@ public async Task PatchItemsHandler_HandleAsync_WithException_ReturnsExpectedSta
var handler = new PatchItemsHandler(
retryQueueDataProvider,
updateItemsInputAdapter,
updateItemsResponseDtoAdapter
updateItemsResponseDtoAdapter,
string.Empty
);

// act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public async Task PatchQueuesHandler_HandleAsync_Success()
var handler = new PatchQueuesHandler(
retryQueueDataProvider.Object,
mockUpdateQueuesInputAdapter.Object,
mockUpdateQueuesResponseDtoAdapter.Object
mockUpdateQueuesResponseDtoAdapter.Object,
string.Empty
);

// Act
Expand Down Expand Up @@ -83,7 +84,8 @@ public async Task PatchQueuesHandler_HandleAsync_WithException_ReturnsExpectedSt
var handler = new PatchQueuesHandler(
retryQueueDataProvider,
updateQueuesInputAdapter,
updateQueuesResponseDtoAdapter
updateQueuesResponseDtoAdapter,
string.Empty
);

// act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class RetryRequestHandlerBaseTests
{
private const string HttpMethod = "GET";
private const string ResourcePath = "/retry";
private const string ResourcePath = "/retry/resource";

[Fact]
public async Task RetryRequestHandlerBase_HandleAsync_CallsHandleRequestAsync()
Expand Down Expand Up @@ -44,7 +44,7 @@ public async Task RetryRequestHandlerBase_HandleAsync_CallsHandleRequestAsync()
.SetupGet(ctx => ctx.Response)
.Returns(httpResponse.Object);

var surrogate = new RetryRequestHandlerSurrogate();
var surrogate = new RetryRequestHandlerSurrogate(string.Empty, "resource");

// Act
var result = await surrogate.HandleAsync(mockHttpContext.Object.Request, mockHttpContext.Object.Response);
Expand All @@ -62,7 +62,7 @@ public async Task RetryRequestHandlerBase_HandleAsync_WithWrongHttpMethod_DoesNo

var httpContext = await HttpContextHelper.CreateContext(ResourcePath, wrongMethod);

var surrogate = new RetryRequestHandlerSurrogate();
var surrogate = new RetryRequestHandlerSurrogate(string.Empty, "resource");

// Act
var result = await surrogate.HandleAsync(httpContext.Request, httpContext.Response).ConfigureAwait(false);
Expand All @@ -79,7 +79,7 @@ public async Task RetryRequestHandlerBase_HandleAsync_WithWrongResourcePath_Does

var httpContext = await HttpContextHelper.CreateContext(wrongPath, HttpMethod);

var surrogate = new RetryRequestHandlerSurrogate();
var surrogate = new RetryRequestHandlerSurrogate(string.Empty, "resource");

// Act
var result = await surrogate.HandleAsync(httpContext.Request, httpContext.Response).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

internal class RetryRequestHandlerSurrogate : RetryRequestHandlerBase
{

public RetryRequestHandlerSurrogate(string endpointPrefix, string resource) : base(endpointPrefix, resource)
{
}

protected override HttpMethod HttpMethod => HttpMethod.GET;

protected override async Task HandleRequestAsync(HttpRequest request, HttpResponse response)
Expand Down

0 comments on commit 42096fa

Please sign in to comment.