Merge pull request #26 from octokit/notification-support

Initial hack at supporting Notifications API
This commit is contained in:
Phil Haack
2013-10-21 10:17:19 -07:00
17 changed files with 226 additions and 3 deletions
@@ -0,0 +1,37 @@
using System;
using Octokit.Reactive.Internal;
namespace Octokit.Reactive.Clients
{
public class ObservableNotificationsClient : IObservableNotificationsClient
{
readonly IConnection _connection;
public ObservableNotificationsClient(IGitHubClient client)
{
Ensure.ArgumentNotNull(client, "client");
_connection = client.Connection;
}
/// <summary>
/// Retrieves all of the <see cref="Notification"/>s for the current user.
/// </summary>
/// <exception cref="AuthorizationException">Thrown if the client is not authenticated.</exception>
/// <returns>A <see cref="IObservable{Notification}"/> of <see cref="Notification"/>.</returns>
public IObservable<Notification> GetAllForCurrent()
{
return _connection.GetAndFlattenAllPages<Notification>(ApiUrls.Notifications());
}
/// <summary>
/// Retrieves all of the <see cref="Notification"/>s for the current user specific to the specified repository.
/// </summary>
/// <exception cref="AuthorizationException">Thrown if the client is not authenticated.</exception>
/// <returns>A <see cref="IObservable{Notification}"/> of <see cref="Notification"/>.</returns>
public IObservable<Notification> GetAllForRepository(string owner, string name)
{
return _connection.GetAndFlattenAllPages<Notification>(ApiUrls.Notifications(owner, name));
}
}
}
@@ -0,0 +1,24 @@
using System;
using System.Diagnostics.CodeAnalysis;
namespace Octokit.Reactive
{
public interface IObservableNotificationsClient
{
/// <summary>
/// Retrieves all of the <see cref="Notification"/>s for the current user.
/// </summary>
/// <exception cref="AuthorizationException">Thrown if the client is not authenticated.</exception>
/// <returns>A <see cref="IObservable{Notification}"/> of <see cref="Notification"/>.</returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
IObservable<Notification> GetAllForCurrent();
/// <summary>
/// Retrieves all of the <see cref="Notification"/>s for the current user specific to the specified repository.
/// </summary>
/// <exception cref="AuthorizationException">Thrown if the client is not authenticated.</exception>
/// <returns>A <see cref="IObservable{Notification}"/> of <see cref="Notification"/>.</returns>
IObservable<Notification> GetAllForRepository(string owner, string name);
}
}
+3 -1
View File
@@ -1,4 +1,4 @@
using Octokit.Internal;
using Octokit.Reactive.Clients;
namespace Octokit.Reactive
{
@@ -13,6 +13,7 @@ namespace Octokit.Reactive
_gitHubClient = gitHubClient;
Authorization = new ObservableAuthorizationsClient(gitHubClient);
Miscellaneous = new ObservableMiscellaneousClient(gitHubClient.Miscellaneous);
Notification = new ObservableNotificationsClient(gitHubClient);
Organization = new ObservableOrganizationsClient(gitHubClient);
Repository = new ObservableRepositoriesClient(gitHubClient);
SshKey = new ObservableSshKeysClient(gitHubClient);
@@ -27,6 +28,7 @@ namespace Octokit.Reactive
public IObservableAuthorizationsClient Authorization { get; private set; }
public IObservableMiscellaneousClient Miscellaneous { get; private set; }
public IObservableNotificationsClient Notification { get; private set; }
public IObservableOrganizationsClient Organization { get; private set; }
public IObservableRepositoriesClient Repository { get; private set; }
public IObservableReleasesClient Release { get; private set; }
+2
View File
@@ -85,6 +85,7 @@
<Compile Include="..\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Clients\ObservableNotificationsClient.cs" />
<Compile Include="Clients\ObservableAuthorizationsClient.cs" />
<Compile Include="Clients\ObservableMiscellaneousClient.cs" />
<Compile Include="Clients\ObservableOrganizationsClient.cs" />
@@ -92,6 +93,7 @@
<Compile Include="Clients\ObservableRepositoriesClient.cs" />
<Compile Include="Clients\ObservableSshKeysClient.cs" />
<Compile Include="Clients\ObservableUsersClient.cs" />
<Compile Include="IObservableNotificationsClient.cs" />
<Compile Include="Helpers\AuthorizationExtensions.cs" />
<Compile Include="Helpers\ConnectionExtensions.cs" />
<Compile Include="Helpers\ObservableExtensions.cs" />
@@ -79,7 +79,9 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -0,0 +1,39 @@
using System;
using NSubstitute;
using Xunit;
namespace Octokit.Tests.Clients
{
public class NotificationsClientTests
{
public class TheGetAllForCurrentMethod
{
[Fact]
public void RequestsCorrectUrl()
{
var endpoint = new Uri("/notifications", UriKind.Relative);
var connection = Substitute.For<IApiConnection>();
var client = new NotificationsClient(connection);
client.GetAllForCurrent();
connection.Received().GetAll<Notification>(endpoint);
}
}
public class TheGetAllForRepository
{
[Fact]
public void RequestsCorrectUrl()
{
var endpoint = new Uri("/repos/banana/split/notifications", UriKind.Relative);
var connection = Substitute.For<IApiConnection>();
var client = new NotificationsClient(connection);
client.GetAllForRepository("banana", "split");
connection.Received().GetAll<Notification>(endpoint);
}
}
}
}
+1
View File
@@ -60,6 +60,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Authentication\CredentialsTests.cs" />
<Compile Include="Clients\NotificationsClientTests.cs" />
<Compile Include="Clients\ReleasesClientTests.cs" />
<Compile Include="Clients\SshKeysClientTests.cs" />
<Compile Include="Exceptions\ApiExceptionTests.cs" />
+1
View File
@@ -52,6 +52,7 @@
<ItemGroup>
<Compile Include="Authentication\CredentialsTests.cs" />
<Compile Include="Clients\MiscellaneousClientTests.cs" />
<Compile Include="Clients\NotificationsClientTests.cs" />
<Compile Include="Clients\ReleasesClientTests.cs" />
<Compile Include="Clients\SshKeysClientTests.cs" />
<Compile Include="Exceptions\ApiExceptionTests.cs" />
+33
View File
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Octokit
{
public class NotificationsClient : ApiClient, INotificationsClient
{
public NotificationsClient(IApiConnection client) : base(client)
{
}
/// <summary>
/// Retrieves all of the <see cref="Notification"/>s for the current user.
/// </summary>
/// <exception cref="AuthorizationException">Thrown if the client is not authenticated.</exception>
/// <returns>A <see cref="IReadOnlyPagedCollection{Notification}"/> of <see cref="Notification"/>.</returns>
public async Task<IReadOnlyCollection<Notification>> GetAllForCurrent()
{
return await Client.GetAll<Notification>(ApiUrls.Notifications());
}
/// <summary>
/// Retrieves all of the <see cref="Notification"/>s for the current user specific to the specified repository.
/// </summary>
/// <exception cref="AuthorizationException">Thrown if the client is not authenticated.</exception>
/// <returns>A <see cref="IReadOnlyPagedCollection{Notification}"/> of <see cref="Notification"/>.</returns>
public async Task<IReadOnlyCollection<Notification>> GetAllForRepository(string owner, string name)
{
return await Client.GetAll<Notification>(ApiUrls.Notifications(owner, name));
}
}
}
+2
View File
@@ -36,6 +36,7 @@ namespace Octokit
Connection = connection;
Authorization = new AuthorizationsClient(new ApiConnection(connection));
Miscellaneous = new MiscellaneousClient(connection);
Notification = new NotificationsClient(new ApiConnection(connection));
Organization = new OrganizationsClient(new ApiConnection(connection));
Repository = new RepositoriesClient(new ApiConnection(connection));
Release = new ReleasesClient(new ApiConnection(connection));
@@ -84,5 +85,6 @@ namespace Octokit
public IReleasesClient Release { get; private set; }
public ISshKeysClient SshKey { get; private set; }
public IUsersClient User { get; private set; }
public INotificationsClient Notification { get; private set; }
}
}
+20 -1
View File
@@ -12,7 +12,7 @@ namespace Octokit
static readonly Uri _currentUserSshKeys = new Uri("/user/keys", UriKind.Relative);
static readonly Uri _currentUserEmailsEndpoint = new Uri("/user/emails", UriKind.Relative);
static readonly Uri _currentUserAuthorizationsEndpoint = new Uri("/authorizations", UriKind.Relative);
static readonly Uri _currentUserNotificationsEndpoint = new Uri("/notifications", UriKind.Relative);
/// <summary>
/// Returns the <see cref="Uri"/> that returns all of the repositories for the currently logged in user in
@@ -111,5 +111,24 @@ namespace Octokit
{
return _currentUserAuthorizationsEndpoint;
}
/// <summary>
/// Returns the <see cref="Uri"/> that returns all of the notifications for the currently logged in user.
/// </summary>
/// <returns></returns>
public static Uri Notifications()
{
return _currentUserNotificationsEndpoint;
}
/// <summary>
/// Returns the <see cref="Uri"/> that returns all of the notifications for the currently logged in user
/// specific to the repository.
/// </summary>
/// <returns></returns>
public static Uri Notifications(string owner, string name)
{
return "/repos/{0}/{1}/notifications".FormatUri(owner, name);
}
}
}
+1
View File
@@ -13,5 +13,6 @@ namespace Octokit
IReleasesClient Release { get; }
ISshKeysClient SshKey { get; }
IUsersClient User { get; }
INotificationsClient Notification { get; }
}
}
+24
View File
@@ -0,0 +1,24 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
namespace Octokit
{
public interface INotificationsClient
{
/// <summary>
/// Retrieves all of the <see cref="Notification"/>s for the current user.
/// </summary>
/// <exception cref="AuthorizationException">Thrown if the client is not authenticated.</exception>
/// <returns>A <see cref="IReadOnlyPagedCollection{Notification}"/> of <see cref="Notification"/>.</returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
Task<IReadOnlyCollection<Notification>> GetAllForCurrent();
/// <summary>
/// Retrieves all of the <see cref="Notification"/>s for the current user specific to the specified repository.
/// </summary>
/// <exception cref="AuthorizationException">Thrown if the client is not authenticated.</exception>
/// <returns>A <see cref="IReadOnlyPagedCollection{Notification}"/> of <see cref="Notification"/>.</returns>
Task<IReadOnlyCollection<Notification>> GetAllForRepository(string owner, string name);
}
}
+14
View File
@@ -0,0 +1,14 @@
namespace Octokit
{
public class Notification
{
public string Id { get; set; } // NB: API currently returns this as string which is Weird
public Repository Repository { get; set; }
public NotificationInfo Subject { get; set; }
public string Reason { get; set; }
public bool Unread { get; set; }
public string UpdatedAt { get; set; }
public string LastReadAt { get; set; }
public string Url { get; set; }
}
}
+14
View File
@@ -0,0 +1,14 @@
using System.Diagnostics.CodeAnalysis;
namespace Octokit
{
public class NotificationInfo
{
public string Title { get; set; }
public string Url { get; set; }
public string LatestCommentUrl { get; set; }
[SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods",
Justification = "Matches the property name used by the API")]
public string Type { get; set; }
}
}
+4
View File
@@ -81,6 +81,8 @@
<Compile Include="..\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Models\Notification.cs" />
<Compile Include="Models\NotificationInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Exceptions\TwoFactorChallengeFailedException.cs" />
<Compile Include="Exceptions\TwoFactorRequiredException.cs" />
@@ -98,6 +100,7 @@
<Compile Include="Clients\ApiClient.cs" />
<Compile Include="Clients\AuthorizationsClient.cs" />
<Compile Include="Clients\ApiPagination.cs" />
<Compile Include="Clients\NotificationsClient.cs" />
<Compile Include="Clients\OrganizationsClient.cs" />
<Compile Include="Authentication\AnonymousAuthenticator.cs" />
<Compile Include="Authentication\Authenticator.cs" />
@@ -126,6 +129,7 @@
<Compile Include="IGitHubClient.cs" />
<Compile Include="IMiscellaneousClient.cs" />
<Compile Include="IReleasesClient.cs" />
<Compile Include="INotificationsClient.cs" />
<Compile Include="ISshKeysClient.cs" />
<Compile Include="IOrganizationsClient.cs" />
<Compile Include="IReadOnlyPagedCollection.cs" />
+4
View File
@@ -112,6 +112,7 @@
<Compile Include="Clients\ApiPagination.cs" />
<Compile Include="Clients\AuthorizationsClient.cs" />
<Compile Include="Clients\MiscellaneousClient.cs" />
<Compile Include="Clients\NotificationsClient.cs" />
<Compile Include="Clients\OrganizationsClient.cs" />
<Compile Include="Clients\ReleasesClient.cs" />
<Compile Include="Clients\RepositoriesClient.cs" />
@@ -146,6 +147,7 @@
<Compile Include="IAuthorizationsClient.cs" />
<Compile Include="IGitHubClient.cs" />
<Compile Include="IMiscellaneousClient.cs" />
<Compile Include="INotificationsClient.cs" />
<Compile Include="IOrganizationsClient.cs" />
<Compile Include="IReadOnlyPagedCollection.cs" />
<Compile Include="Http\ApiInfoExtensions.cs" />
@@ -180,6 +182,8 @@
<Compile Include="Models\EmailAddress.cs" />
<Compile Include="Models\NewAuthorization.cs" />
<Compile Include="Models\NewRepository.cs" />
<Compile Include="Models\Notification.cs" />
<Compile Include="Models\NotificationInfo.cs" />
<Compile Include="Models\Organization.cs" />
<Compile Include="Models\Plan.cs" />
<Compile Include="Models\Readme.cs" />