-
-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tidusjar
committed
May 23, 2016
1 parent
8cce806
commit 5a68634
Showing
22 changed files
with
663 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#region Copyright | ||
// /************************************************************************ | ||
// Copyright (c) 2016 Jamie Rees | ||
// File: ISlackApi.cs | ||
// Created By: Jamie Rees | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining | ||
// a copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to | ||
// permit persons to whom the Software is furnished to do so, subject to | ||
// the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// ************************************************************************/ | ||
#endregion | ||
using System.Threading.Tasks; | ||
|
||
using PlexRequests.Api.Models.Notifications; | ||
|
||
namespace PlexRequests.Api.Interfaces | ||
{ | ||
public interface ISlackApi | ||
{ | ||
Task<string> PushAsync(string team, string token, string service, SlackNotificationBody message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
PlexRequests.Api.Models/Notifications/SlackNotificationBody.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#region Copyright | ||
// /************************************************************************ | ||
// Copyright (c) 2016 Jamie Rees | ||
// File: SlackNotificationBody.cs | ||
// Created By: Jamie Rees | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining | ||
// a copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to | ||
// permit persons to whom the Software is furnished to do so, subject to | ||
// the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// ************************************************************************/ | ||
#endregion | ||
using Newtonsoft.Json; | ||
|
||
namespace PlexRequests.Api.Models.Notifications | ||
{ | ||
public class SlackNotificationBody | ||
{ | ||
[JsonConstructor] | ||
public SlackNotificationBody() | ||
{ | ||
username = "Plex Requests"; | ||
} | ||
|
||
[JsonIgnore] | ||
private string _username; | ||
public string username | ||
{ | ||
get { return _username; } | ||
set | ||
{ | ||
if (!string.IsNullOrEmpty(value)) | ||
_username = value; | ||
} | ||
} | ||
public string channel { get; set; } | ||
public string text { get; set; } | ||
|
||
public string icon_url { get; set; } | ||
public string icon_emoji { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#region Copyright | ||
// /************************************************************************ | ||
// Copyright (c) 2016 Jamie Rees | ||
// File: PlexApi.cs | ||
// Created By: Jamie Rees | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining | ||
// a copy of this software and associated documentation files (the | ||
// "Software"), to deal in the Software without restriction, including | ||
// without limitation the rights to use, copy, modify, merge, publish, | ||
// distribute, sublicense, and/or sell copies of the Software, and to | ||
// permit persons to whom the Software is furnished to do so, subject to | ||
// the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// ************************************************************************/ | ||
#endregion | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
using PlexRequests.Api.Interfaces; | ||
using PlexRequests.Api.Models.Notifications; | ||
|
||
using RestSharp; | ||
|
||
namespace PlexRequests.Api | ||
{ | ||
public class SlackApi : ISlackApi | ||
{ | ||
public async Task<string> PushAsync(string team, string token, string service, SlackNotificationBody message) | ||
{ | ||
var request = new RestRequest | ||
{ | ||
Method = Method.POST, | ||
Resource = "/services/{team}/{service}/{token}" | ||
}; | ||
|
||
request.AddUrlSegment("team", team); | ||
request.AddUrlSegment("service", service); | ||
request.AddUrlSegment("token", token); | ||
request.AddJsonBody(message); | ||
|
||
var api = new ApiRequest(); | ||
return await Task.Run( | ||
() => | ||
{ | ||
var result = api.Execute(request, new Uri("https://hooks.slack.com/")); | ||
return result.Content; | ||
}); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
PlexRequests.Core/SettingModels/SlackNotificationSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
|
||
using Newtonsoft.Json; | ||
|
||
namespace PlexRequests.Core.SettingModels | ||
{ | ||
public class SlackNotificationSettings : Settings | ||
{ | ||
public bool Enabled { get; set; } | ||
public string WebhookUrl { get; set; } | ||
public string Channel { get; set; } | ||
public string Username { get; set; } | ||
|
||
[JsonIgnore] | ||
public string Team => SplitWebUrl(3); | ||
|
||
[JsonIgnore] | ||
public string Service => SplitWebUrl(4); | ||
|
||
[JsonIgnore] | ||
public string Token => SplitWebUrl(5); | ||
|
||
private string SplitWebUrl(int index) | ||
{ | ||
if (!WebhookUrl.StartsWith("http", StringComparison.InvariantCulture)) | ||
{ | ||
WebhookUrl = "https://" + WebhookUrl; | ||
} | ||
var split = WebhookUrl.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); | ||
|
||
return split.Length < index | ||
? string.Empty | ||
: split[index]; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.