Skip to content

Commit

Permalink
#164 has been resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed May 23, 2016
1 parent 8cce806 commit 5a68634
Show file tree
Hide file tree
Showing 22 changed files with 663 additions and 8 deletions.
37 changes: 37 additions & 0 deletions PlexRequests.Api.Interfaces/ISlackApi.cs
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<Compile Include="IMusicBrainzApi.cs" />
<Compile Include="IPlexApi.cs" />
<Compile Include="IPushbulletApi.cs" />
<Compile Include="ISlackApi.cs" />
<Compile Include="IPushoverApi.cs" />
<Compile Include="ISickRageApi.cs" />
<Compile Include="ISonarrApi.cs" />
Expand Down
56 changes: 56 additions & 0 deletions PlexRequests.Api.Models/Notifications/SlackNotificationBody.cs
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; }
}
}
1 change: 1 addition & 0 deletions PlexRequests.Api.Models/PlexRequests.Api.Models.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Compile Include="Notifications\PushbulletPush.cs" />
<Compile Include="Notifications\PushbulletResponse.cs" />
<Compile Include="Notifications\PushoverResponse.cs" />
<Compile Include="Notifications\SlackNotificationBody.cs" />
<Compile Include="Plex\PlexAccount.cs" />
<Compile Include="Plex\PlexAuthentication.cs" />
<Compile Include="Plex\PlexError.cs" />
Expand Down
3 changes: 1 addition & 2 deletions PlexRequests.Api/ApiRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class ApiRequest : IApiRequest

return response.Data;
}

public IRestResponse Execute(IRestRequest request, Uri baseUri)
{
var client = new RestClient { BaseUrl = baseUri };
Expand Down Expand Up @@ -109,7 +109,6 @@ public T ExecuteXml<T>(IRestRequest request, Uri baseUri) where T : class
public T ExecuteJson<T>(IRestRequest request, Uri baseUri) where T : new()
{
var client = new RestClient { BaseUrl = baseUri };

var response = client.Execute(request);
Log.Trace("Api Content Response:");
Log.Trace(response.Content);
Expand Down
1 change: 1 addition & 0 deletions PlexRequests.Api/PlexRequests.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<DependentUpon>MockApiData.resx</DependentUpon>
</Compile>
<Compile Include="Mocks\MockSonarrApi.cs" />
<Compile Include="SlackApi.cs" />
<Compile Include="PushoverApi.cs" />
<Compile Include="PushbulletApi.cs" />
<Compile Include="SickrageApi.cs" />
Expand Down
62 changes: 62 additions & 0 deletions PlexRequests.Api/SlackApi.cs
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;
});
}
}
}

1 change: 1 addition & 0 deletions PlexRequests.Core/PlexRequests.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="Models\UserProperties.cs" />
<Compile Include="SettingModels\AuthenticationSettings.cs" />
<Compile Include="SettingModels\HeadphonesSettings.cs" />
<Compile Include="SettingModels\SlackNotificationSettings.cs" />
<Compile Include="SettingModels\PushoverNotificationSettings.cs" />
<Compile Include="SettingModels\PushBulletNotificationSettings.cs" />
<Compile Include="SettingModels\EmailNotificationSettings.cs" />
Expand Down
37 changes: 37 additions & 0 deletions PlexRequests.Core/SettingModels/SlackNotificationSettings.cs
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];
}

}
}
4 changes: 2 additions & 2 deletions PlexRequests.Services/Notification/PushbulletNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public async Task NotifyAsync(NotificationModel model, Settings settings)
case NotificationType.AdminNote:
break;
case NotificationType.Test:
await PushTestAsync(model, pushSettings);
await PushTestAsync(pushSettings);
break;
default:
throw new ArgumentOutOfRangeException();
Expand Down Expand Up @@ -116,7 +116,7 @@ private async Task PushIssueAsync(NotificationModel model, PushbulletNotificatio
await Push(settings, message, pushTitle);
}

private async Task PushTestAsync(NotificationModel model, PushbulletNotificationSettings settings)
private async Task PushTestAsync(PushbulletNotificationSettings settings)
{
var message = "This is just a test! Success!";
var pushTitle = "Plex Requests: Test Message!";
Expand Down
Loading

0 comments on commit 5a68634

Please sign in to comment.