Skip to content

Commit

Permalink
Adding DelayResponse configuration method to SlackEndpointConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
soxtoby committed Jan 2, 2024
1 parent ef9439d commit 4ed543f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
12 changes: 12 additions & 0 deletions SlackNet.AspNetCore/SlackEndpointConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,22 @@ public SlackEndpointConfiguration UseSocketMode(bool useSocketMode = true, Socke
return this;
}

/// <summary>
/// Delay responding to Slack until after handlers have completed.
/// May be required in environments that pause or shut down after a request has been completed.
/// </summary>
[Obsolete(Warning.Experimental)]
public SlackEndpointConfiguration DelayResponse(bool delay = true)
{
DelayedResponse = delay;
return this;
}

public string RoutePrefix { get; private set; } = "slack";
public string VerificationToken { get; private set; }
public string SigningSecret { get; private set; }
public bool VerifyEventUrl { get; private set; } = true;
public bool SocketMode { get; private set; }
public bool DelayedResponse { get; private set; }
public SocketModeConnectionOptions SocketModeConnectionOptions { get; set; }
}
33 changes: 23 additions & 10 deletions SlackNet.AspNetCore/SlackRequestMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,31 @@ public async Task Invoke(HttpContext context)
await _next(context).ConfigureAwait(false);
}

private static async Task Respond(HttpResponse httpResponse, SlackResult slackResult)
private async Task Respond(HttpResponse httpResponse, SlackResult slackResult)
{
httpResponse.StatusCode = (int)slackResult.Status;
try
{
if (_configuration.DelayedResponse)
{
foreach (var callback in slackResult.RequestCompletedCallbacks)
await callback().ConfigureAwait(false);
}
else
{
// Note: HttpResponse's completed callbacks are called in FILO order
foreach (var callback in slackResult.RequestCompletedCallbacks.Reverse())
httpResponse.OnCompleted(callback);
}
}
finally
{
httpResponse.StatusCode = (int)slackResult.Status;

// Note: HttpResponse's completed callbacks are called in FILO order
foreach (var callback in slackResult.RequestCompletedCallbacks.Reverse())
httpResponse.OnCompleted(callback);
if (slackResult.ContentType != null)
httpResponse.ContentType = slackResult.ContentType;

if (slackResult.ContentType != null)
httpResponse.ContentType = slackResult.ContentType;

if (slackResult.Body != null)
await httpResponse.WriteAsync(slackResult.Body).ConfigureAwait(false);
if (slackResult.Body != null)
await httpResponse.WriteAsync(slackResult.Body).ConfigureAwait(false);
}
}
}
6 changes: 6 additions & 0 deletions SlackNet.AspNetCore/Warning.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SlackNet.AspNetCore;

static class Warning
{
public const string Experimental = "This API is experimental, and likely to change in the future.";
}

0 comments on commit 4ed543f

Please sign in to comment.