diff --git a/src/SlackHelper/ISlackHelper.cs b/src/SlackHelper/ISlackHelper.cs index 96ba96e..06f0e7b 100644 --- a/src/SlackHelper/ISlackHelper.cs +++ b/src/SlackHelper/ISlackHelper.cs @@ -1,4 +1,5 @@ using System.Net.Http; +using System.Threading; using System.Threading.Tasks; using ManxJason.SlackHelper.Attachments; @@ -6,9 +7,9 @@ namespace ManxJason.SlackHelper { public interface ISlackHelper { - Task SendAsync(string message); - Task SendAsync(Attachment attachment); - Task SendAsync(Attachment[] attachments); - Task SendAsync(object message); + Task SendAsync(string message, CancellationToken cancellationToken); + Task SendAsync(Attachment attachment, CancellationToken cancellationToken); + Task SendAsync(Attachment[] attachments, CancellationToken cancellationToken); + Task SendAsync(object message, CancellationToken cancellationToken); } } \ No newline at end of file diff --git a/src/SlackHelper/SlackHelper.cs b/src/SlackHelper/SlackHelper.cs index 73d2319..1ab7d54 100644 --- a/src/SlackHelper/SlackHelper.cs +++ b/src/SlackHelper/SlackHelper.cs @@ -1,6 +1,7 @@ using System; using System.Net.Http; using System.Text; +using System.Threading; using System.Threading.Tasks; using ManxJason.SlackHelper.Attachments; using Newtonsoft.Json; @@ -15,33 +16,40 @@ public sealed class SlackHelper : ISlackHelper private readonly Uri _incomingWebHook; /// - /// Obtain the incoming webhook from your Slack configuration options. + /// Obtain the incoming web hook from your Slack configuration options. /// /// public SlackHelper(Uri incomingWebHook) => _incomingWebHook = incomingWebHook ?? throw new ArgumentNullException(); /// - /// Send a regular string value via webhook. + /// Send a regular string value via web hook. /// /// Plain old text value. + /// /// - public async Task SendAsync(string message) => - await PostToSlack( + public async Task SendAsync( + string message, + CancellationToken cancellationToken = default(CancellationToken)) => + await PostToSlackAsync( _incomingWebHook, new { text = message - }) + }, + cancellationToken) .ConfigureAwait(false); /// - /// Send a single attachment via webhook. + /// Send a single attachment via web hook. /// /// + /// /// - public async Task SendAsync(Attachment attachment) => - await PostToSlack( + public async Task SendAsync( + Attachment attachment, + CancellationToken cancellationToken = default(CancellationToken)) => + await PostToSlackAsync( _incomingWebHook, new AttachmentsContainer { @@ -49,32 +57,43 @@ await PostToSlack( { attachment } - }) + }, + cancellationToken) .ConfigureAwait(false); /// - /// Send an array of attachments via webhook. + /// Send an array of attachments via web hook. /// /// + /// /// - public async Task SendAsync(Attachment[] attachments) => - await PostToSlack( + public async Task SendAsync( + Attachment[] attachments, + CancellationToken cancellationToken = default(CancellationToken)) => + await PostToSlackAsync( _incomingWebHook, new AttachmentsContainer { Attachments = attachments - }) + }, + cancellationToken) .ConfigureAwait(false); /// - /// Build your own anonymous object following Slack documentation to send your customised message. + /// Build your own anonymous object following Slack documentation to send your customized message. /// /// + /// /// HttpResponse - public Task SendAsync(object message) => - PostToSlack(_incomingWebHook, message); + public Task SendAsync( + object message, + CancellationToken cancellationToken = default(CancellationToken)) => + PostToSlackAsync(_incomingWebHook, message, cancellationToken); - private static async Task PostToSlack(Uri slackHook, object content) + private static async Task PostToSlackAsync( + Uri slackHook, + object content, + CancellationToken cancellationToken = default(CancellationToken)) { using (HttpClient client = new HttpClient()) { @@ -91,7 +110,7 @@ private static async Task PostToSlack(Uri slackHook, object }); StringContent stringContent = new StringContent(serializeObject, Encoding.UTF8, "application/json"); - return await client.PostAsync(slackHook, stringContent).ConfigureAwait(false); + return await client.PostAsync(slackHook, stringContent, cancellationToken).ConfigureAwait(false); } } } diff --git a/src/SlackHelper/SlackHelper.csproj b/src/SlackHelper/SlackHelper.csproj index 531a319..b5df434 100644 --- a/src/SlackHelper/SlackHelper.csproj +++ b/src/SlackHelper/SlackHelper.csproj @@ -5,7 +5,7 @@ netstandard2.0 ManxJason.SlackHelper ManxJason.SlackHelper - 0.0.3 + 0.0.4 Jason Callister Jason Callister Lightweight .Net Standard library for posting Slack messages or attachments via webhook integration @@ -14,7 +14,7 @@ https://github.com/manxjason/SlackHelper/blob/master/LICENSE https://github.com/manxjason/SlackHelper https://github.com/manxjason/SlackHelper - Basic webhook functionality only. + Added cancellation tokens true