Skip to content

Commit

Permalink
Add pagination support (ApiOptions overloads) to UserKeys client. (#1278
Browse files Browse the repository at this point in the history
)
  • Loading branch information
devkhan authored and shiftkey committed Apr 29, 2016
1 parent 63a8c1d commit e35f237
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 38 deletions.
39 changes: 31 additions & 8 deletions Octokit.Reactive/Clients/IObservableUserKeysClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,47 @@ namespace Octokit.Reactive
/// </remarks>
public interface IObservableUserKeysClient
{
/// <summary>
/// Gets all verified public keys for a user.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-public-keys-for-a-user
/// </remarks>
/// <param name="userName">The @ handle of the user.</param>
/// <returns>Lists the verified public keys for a user.</returns>
IObservable<PublicKey> GetAll(string userName);

/// <summary>
/// Gets all verified public keys for a user.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-public-keys-for-a-user
/// </remarks>
/// <param name="userName">The @ handle of the user.</param>
/// <param name="options">Options to change API's behavior.</param>
/// <returns>Lists the verified public keys for a user.</returns>
IObservable<PublicKey> GetAll(string userName, ApiOptions options);

/// <summary>
/// Gets all public keys for the authenticated user.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-your-public-keys
/// </remarks>
/// <returns></returns>
/// <returns>Lists the current user's keys.</returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
IObservable<PublicKey> GetAllForCurrent();

/// <summary>
/// Gets all verified public keys for a user.
/// Gets all public keys for the authenticated user.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-public-keys-for-a-user
/// https://developer.github.com/v3/users/keys/#list-your-public-keys
/// </remarks>
/// <returns></returns>
IObservable<PublicKey> GetAll(string userName);
/// <param name="options">Options to change API's behavior.</param>
/// <returns>Lists the current user's keys.</returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
IObservable<PublicKey> GetAllForCurrent(ApiOptions options);

/// <summary>
/// Retrieves the <see cref="PublicKey"/> for the specified id.
Expand All @@ -38,7 +61,7 @@ public interface IObservableUserKeysClient
/// https://developer.github.com/v3/users/keys/#get-a-single-public-key
/// </remarks>
/// <param name="id">The ID of the SSH key</param>
/// <returns></returns>
/// <returns>View extended details for a single public key.</returns>
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get")]
IObservable<PublicKey> Get(int id);

Expand All @@ -49,7 +72,7 @@ public interface IObservableUserKeysClient
/// https://developer.github.com/v3/users/keys/#create-a-public-key
/// </remarks>
/// <param name="newKey">The SSH Key contents</param>
/// <returns></returns>
/// <returns>Creates a public key.</returns>
IObservable<PublicKey> Create(NewPublicKey newKey);

/// <summary>
Expand All @@ -59,7 +82,7 @@ public interface IObservableUserKeysClient
/// https://developer.github.com/v3/users/keys/#delete-a-public-key
/// </remarks>
/// <param name="id">The id of the key to delete</param>
/// <returns></returns>
/// <returns>Removes a public key.</returns>
IObservable<Unit> Delete(int id);
}
}
55 changes: 45 additions & 10 deletions Octokit.Reactive/Clients/ObservableUserKeysClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,63 @@ public ObservableUserKeysClient(IGitHubClient client)
_client = client.User.Keys;
}

/// <summary>
/// Gets all verified public keys for a user.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-public-keys-for-a-user
/// </remarks>
/// <param name="userName">The @ handle of the user.</param>
/// <returns>Lists the verified public keys for a user.</returns>
public IObservable<PublicKey> GetAll(string userName)
{
Ensure.ArgumentNotNullOrEmptyString(userName, "userName");

return GetAll(userName, ApiOptions.None);
}

/// <summary>
/// Gets all verified public keys for a user.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-public-keys-for-a-user
/// </remarks>
/// <param name="userName">The @ handle of the user.</param>
/// <param name="options">Options to change API's behavior.</param>
/// <returns>Lists the verified public keys for a user.</returns>
public IObservable<PublicKey> GetAll(string userName, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(userName, "userName");
Ensure.ArgumentNotNull(options, "options");

return _client.GetAll(userName, options).ToObservable().SelectMany(k => k);
}

/// <summary>
/// Gets all public keys for the authenticated user.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-your-public-keys
/// </remarks>
/// <returns></returns>
/// <returns>Lists the current user's keys.</returns>
public IObservable<PublicKey> GetAllForCurrent()
{
return _client.GetAllForCurrent().ToObservable().SelectMany(k => k);
return GetAllForCurrent(ApiOptions.None);
}

/// <summary>
/// Gets all verified public keys for a user.
/// Gets all public keys for the authenticated user.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-public-keys-for-a-user
/// https://developer.github.com/v3/users/keys/#list-your-public-keys
/// </remarks>
/// <returns></returns>
public IObservable<PublicKey> GetAll(string userName)
/// <param name="options">Options to change API's behavior.</param>
/// <returns>Lists the current user's keys.</returns>
public IObservable<PublicKey> GetAllForCurrent(ApiOptions options)
{
return _client.GetAll(userName).ToObservable().SelectMany(k => k);
Ensure.ArgumentNotNull(options, "options");

return _client.GetAllForCurrent(options).ToObservable().SelectMany(k => k);
}

/// <summary>
Expand All @@ -53,7 +88,7 @@ public IObservable<PublicKey> GetAll(string userName)
/// https://developer.github.com/v3/users/keys/#get-a-single-public-key
/// </remarks>
/// <param name="id">The ID of the SSH key</param>
/// <returns></returns>
/// <returns>View extended details for a single public key.</returns>
public IObservable<PublicKey> Get(int id)
{
return _client.Get(id).ToObservable();
Expand All @@ -66,7 +101,7 @@ public IObservable<PublicKey> Get(int id)
/// https://developer.github.com/v3/users/keys/#create-a-public-key
/// </remarks>
/// <param name="newKey">The SSH Key contents</param>
/// <returns></returns>
/// <returns>Creates a public key.</returns>
public IObservable<PublicKey> Create(NewPublicKey newKey)
{
Ensure.ArgumentNotNull(newKey, "newKey");
Expand All @@ -81,7 +116,7 @@ public IObservable<PublicKey> Create(NewPublicKey newKey)
/// https://developer.github.com/v3/users/keys/#delete-a-public-key
/// </remarks>
/// <param name="id">The id of the key to delete</param>
/// <returns></returns>
/// <returns>Removes a public key.</returns>
public IObservable<Unit> Delete(int id)
{
return _client.Delete(id).ToObservable();
Expand Down
7 changes: 5 additions & 2 deletions Octokit.Tests/Clients/UserKeysClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public void RequestsTheCorrectUrl()
client.GetAllForCurrent();

connection.Received().GetAll<PublicKey>(
Arg.Is<Uri>(u => u.ToString() == expectedUri));
Arg.Is<Uri>(u => u.ToString() == expectedUri),
Arg.Any<ApiOptions>());
}
}

Expand All @@ -30,6 +31,7 @@ public async Task EnsuresNonNullArguments()
{
var client = new UserKeysClient(Substitute.For<IApiConnection>());
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll(null));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.GetAll("fake", null));
}

[Fact]
Expand All @@ -50,7 +52,8 @@ public void RequestsTheCorrectUrl()
client.GetAll("auser");

connection.Received().GetAll<PublicKey>(
Arg.Is<Uri>(u => u.ToString() == expectedUri));
Arg.Is<Uri>(u => u.ToString() == expectedUri),
Arg.Any<ApiOptions>());
}
}

Expand Down
4 changes: 2 additions & 2 deletions Octokit.Tests/Reactive/ObservableUserKeysClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void CallsIntoClient()

client.GetAllForCurrent();

gitHubClient.User.Keys.Received().GetAllForCurrent();
gitHubClient.User.Keys.Received().GetAllForCurrent(Arg.Any<ApiOptions>());
}
}

Expand All @@ -31,7 +31,7 @@ public void CallsIntoClient()

client.GetAll("auser");

gitHubClient.User.Keys.Received().GetAll("auser");
gitHubClient.User.Keys.Received().GetAll("auser", Arg.Any<ApiOptions>());
}
}

Expand Down
39 changes: 31 additions & 8 deletions Octokit/Clients/IUserKeysClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,47 @@ namespace Octokit
/// </remarks>
public interface IUserKeysClient
{
/// <summary>
/// Gets all verified public keys for a user.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-public-keys-for-a-user
/// </remarks>
/// <param name="userName">The @ handle of the user.</param>
/// <returns>Lists the verified public keys for a user.</returns>
Task<IReadOnlyList<PublicKey>> GetAll(string userName);

/// <summary>
/// Gets all verified public keys for a user.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-public-keys-for-a-user
/// </remarks>
/// <param name="userName">The @ handle of the user.</param>
/// <param name="options">Options to change API's behavior.</param>
/// <returns>Lists the verified public keys for a user.</returns>
Task<IReadOnlyList<PublicKey>> GetAll(string userName, ApiOptions options);

/// <summary>
/// Gets all public keys for the authenticated user.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-your-public-keys
/// </remarks>
/// <returns></returns>
/// <returns>Lists the current user's keys.</returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
Task<IReadOnlyList<PublicKey>> GetAllForCurrent();

/// <summary>
/// Gets all verified public keys for a user.
/// Gets all public keys for the authenticated user.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-public-keys-for-a-user
/// https://developer.github.com/v3/users/keys/#list-your-public-keys
/// </remarks>
/// <returns></returns>
Task<IReadOnlyList<PublicKey>> GetAll(string userName);
/// <param name="options">Options to change API's behavior.</param>
/// <returns>Lists the current user's keys.</returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
Task<IReadOnlyList<PublicKey>> GetAllForCurrent(ApiOptions options);

/// <summary>
/// Retrieves the <see cref="PublicKey"/> for the specified id.
Expand All @@ -38,7 +61,7 @@ public interface IUserKeysClient
/// https://developer.github.com/v3/users/keys/#get-a-single-public-key
/// </remarks>
/// <param name="id">The ID of the SSH key</param>
/// <returns></returns>
/// <returns>View extended details for a single public key.</returns>
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get")]
Task<PublicKey> Get(int id);

Expand All @@ -49,7 +72,7 @@ public interface IUserKeysClient
/// https://developer.github.com/v3/users/keys/#create-a-public-key
/// </remarks>
/// <param name="newKey">The SSH Key contents</param>
/// <returns></returns>
/// <returns>Creates a public key.</returns>
Task<PublicKey> Create(NewPublicKey newKey);

/// <summary>
Expand All @@ -59,7 +82,7 @@ public interface IUserKeysClient
/// https://developer.github.com/v3/users/keys/#delete-a-public-key
/// </remarks>
/// <param name="id">The id of the key to delete</param>
/// <returns></returns>
/// <returns>Removes a public key.</returns>
Task Delete(int id);
}
}
49 changes: 41 additions & 8 deletions Octokit/Clients/UserKeysClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ public UserKeysClient(IApiConnection apiConnection)
}

/// <summary>
/// Gets all public keys for the authenticated user.
/// Gets all verified public keys for a user.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-your-public-keys
/// https://developer.github.com/v3/users/keys/#list-public-keys-for-a-user
/// </remarks>
/// <returns></returns>
public Task<IReadOnlyList<PublicKey>> GetAllForCurrent()
/// <param name="userName">The @ handle of the user.</param>
/// <returns>Lists the verified public keys for a user.</returns>
public Task<IReadOnlyList<PublicKey>> GetAll(string userName)
{
return ApiConnection.GetAll<PublicKey>(ApiUrls.Keys());
Ensure.ArgumentNotNullOrEmptyString(userName, "userName");

return GetAll(userName, ApiOptions.None);
}

/// <summary>
Expand All @@ -34,12 +37,42 @@ public Task<IReadOnlyList<PublicKey>> GetAllForCurrent()
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-public-keys-for-a-user
/// </remarks>
/// <returns></returns>
public Task<IReadOnlyList<PublicKey>> GetAll(string userName)
/// <param name="userName">The @ handle of the user.</param>
/// <param name="options">Options to change API's behavior.</param>
/// <returns>Lists the verified public keys for a user.</returns>
public Task<IReadOnlyList<PublicKey>> GetAll(string userName, ApiOptions options)
{
Ensure.ArgumentNotNullOrEmptyString(userName, "userName");
Ensure.ArgumentNotNull(options, "options");

return ApiConnection.GetAll<PublicKey>(ApiUrls.Keys(userName), options);
}

/// <summary>
/// Gets all public keys for the authenticated user.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-your-public-keys
/// </remarks>
/// <returns>Lists the current user's keys.</returns>
public Task<IReadOnlyList<PublicKey>> GetAllForCurrent()
{
return GetAllForCurrent(ApiOptions.None);
}

/// <summary>
/// Gets all public keys for the authenticated user.
/// </summary>
/// <remarks>
/// https://developer.github.com/v3/users/keys/#list-your-public-keys
/// </remarks>
/// <param name="options">Options to chagne API's behavior.</param>
/// <returns>Lists the current user's keys.</returns>
public Task<IReadOnlyList<PublicKey>> GetAllForCurrent(ApiOptions options)
{
Ensure.ArgumentNotNull(options, "options");

return ApiConnection.GetAll<PublicKey>(ApiUrls.Keys(userName));
return ApiConnection.GetAll<PublicKey>(ApiUrls.Keys(), options);
}

/// <summary>
Expand Down

0 comments on commit e35f237

Please sign in to comment.