Skip to content

Commit

Permalink
Added cancellation tokens.
Browse files Browse the repository at this point in the history
  • Loading branch information
manxjason committed Jan 9, 2019
1 parent def7cb8 commit 942ad57
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
9 changes: 5 additions & 4 deletions src/SlackHelper/ISlackHelper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using ManxJason.SlackHelper.Attachments;

namespace ManxJason.SlackHelper
{
public interface ISlackHelper
{
Task<HttpResponseMessage> SendAsync(string message);
Task<HttpResponseMessage> SendAsync(Attachment attachment);
Task<HttpResponseMessage> SendAsync(Attachment[] attachments);
Task<HttpResponseMessage> SendAsync(object message);
Task<HttpResponseMessage> SendAsync(string message, CancellationToken cancellationToken);
Task<HttpResponseMessage> SendAsync(Attachment attachment, CancellationToken cancellationToken);
Task<HttpResponseMessage> SendAsync(Attachment[] attachments, CancellationToken cancellationToken);
Task<HttpResponseMessage> SendAsync(object message, CancellationToken cancellationToken);
}
}
55 changes: 37 additions & 18 deletions src/SlackHelper/SlackHelper.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,66 +16,84 @@ public sealed class SlackHelper : ISlackHelper
private readonly Uri _incomingWebHook;

/// <summary>
/// Obtain the incoming webhook from your Slack configuration options.
/// Obtain the incoming web hook from your Slack configuration options.
/// </summary>
/// <param name="incomingWebHook"></param>
public SlackHelper(Uri incomingWebHook) =>
_incomingWebHook = incomingWebHook ?? throw new ArgumentNullException();

/// <summary>
/// Send a regular string value via webhook.
/// Send a regular string value via web hook.
/// </summary>
/// <param name="message">Plain old text value.</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<HttpResponseMessage> SendAsync(string message) =>
await PostToSlack(
public async Task<HttpResponseMessage> SendAsync(
string message,
CancellationToken cancellationToken = default(CancellationToken)) =>
await PostToSlackAsync(
_incomingWebHook,
new
{
text = message
})
},
cancellationToken)
.ConfigureAwait(false);

/// <summary>
/// Send a single attachment via webhook.
/// Send a single attachment via web hook.
/// </summary>
/// <param name="attachment"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<HttpResponseMessage> SendAsync(Attachment attachment) =>
await PostToSlack(
public async Task<HttpResponseMessage> SendAsync(
Attachment attachment,
CancellationToken cancellationToken = default(CancellationToken)) =>
await PostToSlackAsync(
_incomingWebHook,
new AttachmentsContainer
{
Attachments = new[]
{
attachment
}
})
},
cancellationToken)
.ConfigureAwait(false);

/// <summary>
/// Send an array of attachments via webhook.
/// Send an array of attachments via web hook.
/// </summary>
/// <param name="attachments"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<HttpResponseMessage> SendAsync(Attachment[] attachments) =>
await PostToSlack(
public async Task<HttpResponseMessage> SendAsync(
Attachment[] attachments,
CancellationToken cancellationToken = default(CancellationToken)) =>
await PostToSlackAsync(
_incomingWebHook,
new AttachmentsContainer
{
Attachments = attachments
})
},
cancellationToken)
.ConfigureAwait(false);

/// <summary>
/// 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.
/// </summary>
/// <param name="message"></param>
/// <param name="cancellationToken"></param>
/// <returns>HttpResponse</returns>
public Task<HttpResponseMessage> SendAsync(object message) =>
PostToSlack(_incomingWebHook, message);
public Task<HttpResponseMessage> SendAsync(
object message,
CancellationToken cancellationToken = default(CancellationToken)) =>
PostToSlackAsync(_incomingWebHook, message, cancellationToken);

private static async Task<HttpResponseMessage> PostToSlack(Uri slackHook, object content)
private static async Task<HttpResponseMessage> PostToSlackAsync(
Uri slackHook,
object content,
CancellationToken cancellationToken = default(CancellationToken))
{
using (HttpClient client = new HttpClient())
{
Expand All @@ -91,7 +110,7 @@ private static async Task<HttpResponseMessage> 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);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/SlackHelper/SlackHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>ManxJason.SlackHelper</AssemblyName>
<RootNamespace>ManxJason.SlackHelper</RootNamespace>
<Version>0.0.3</Version>
<Version>0.0.4</Version>
<Authors>Jason Callister</Authors>
<Company>Jason Callister</Company>
<Description>Lightweight .Net Standard library for posting Slack messages or attachments via webhook integration</Description>
Expand All @@ -14,7 +14,7 @@
<Copyright>https://github.com/manxjason/SlackHelper/blob/master/LICENSE</Copyright>
<PackageProjectUrl>https://github.com/manxjason/SlackHelper</PackageProjectUrl>
<RepositoryUrl>https://github.com/manxjason/SlackHelper</RepositoryUrl>
<PackageReleaseNotes>Basic webhook functionality only.</PackageReleaseNotes>
<PackageReleaseNotes>Added cancellation tokens</PackageReleaseNotes>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

Expand Down

0 comments on commit 942ad57

Please sign in to comment.