Skip to content

Commit

Permalink
Merge pull request #138 from dahlbyk/ObservableMilestones
Browse files Browse the repository at this point in the history
Reactive Milestone Client
  • Loading branch information
haacked committed Nov 1, 2013
2 parents 9c4cfaf + 9c1ace9 commit bc82697
Show file tree
Hide file tree
Showing 13 changed files with 947 additions and 423 deletions.
79 changes: 79 additions & 0 deletions Octokit.Reactive/Clients/IObservableMilestonesClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reactive;

namespace Octokit
{
public interface IObservableMilestonesClient
{
/// <summary>
/// Gets a single Milestone by number.
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/issues/milestones/#get-a-single-milestone
/// </remarks>
/// <returns></returns>
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get",
Justification = "Method makes a network request")]
IObservable<Milestone> Get(string owner, string name, int number);

/// <summary>
/// Gets all open milestones for the repository.
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/issues/milestones/#list-milestones-for-a-repository
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <returns></returns>
IObservable<Milestone> GetForRepository(string owner, string name);

/// <summary>
/// Gets all open milestones for the repository.
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/issues/milestones/#list-milestones-for-a-repository
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="request">Used to filter and sort the list of Milestones returned</param>
/// <returns></returns>
IObservable<Milestone> GetForRepository(string owner, string name, MilestoneRequest request);

/// <summary>
/// Creates a milestone for the specified repository. Any user with pull access to a repository can create an
/// Milestone.
/// </summary>
/// <remarks>http://developer.github.com/v3/issues/milestones/#create-a-milestone</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="newMilestone">A <see cref="NewMilestone"/> instance describing the new Milestone to create</param>
/// <returns></returns>
IObservable<Milestone> Create(string owner, string name, NewMilestone newMilestone);

/// <summary>
/// Creates a milestone for the specified repository. Any user with pull access to a repository can create an
/// Milestone.
/// </summary>
/// <remarks>http://developer.github.com/v3/issues/milestones/#update-a-milestone</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="number">The Milestone number</param>
/// <param name="milestoneUpdate">An <see cref="MilestoneUpdate"/> instance describing the changes to make to the Milestone
/// </param>
/// <returns></returns>
IObservable<Milestone> Update(string owner, string name, int number, MilestoneUpdate milestoneUpdate);

/// <summary>
/// Deletes a milestone for the specified repository. Any user with pull access to a repository can create an
/// Milestone.
/// </summary>
/// <remarks>http://developer.github.com/v3/issues/milestones/#delete-a-milestone</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="number">The milestone number</param>
/// <returns></returns>
IObservable<Unit> Delete(string owner, string name, int number);
}
}
125 changes: 125 additions & 0 deletions Octokit.Reactive/Clients/ObservableMilestonesClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using System.Reactive;
using System.Reactive.Threading.Tasks;
using Octokit.Reactive.Internal;

namespace Octokit.Reactive.Clients
{
public class ObservableMilestonesClient : IObservableMilestonesClient
{
readonly IMilestonesClient _client;
readonly IConnection _connection;

public ObservableMilestonesClient(IGitHubClient client)
{
Ensure.ArgumentNotNull(client, "client");

_client = client.Issue.Milestone;
_connection = client.Connection;
}

/// <summary>
/// Gets a single Milestone by number.
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/issues/milestones/#get-a-single-milestone
/// </remarks>
/// <returns></returns>
public IObservable<Milestone> Get(string owner, string name, int number)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");

return _client.Get(owner, name, number).ToObservable();
}

/// <summary>
/// Gets all open milestones for the repository.
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/issues/milestones/#list-milestones-for-a-repository
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <returns></returns>
public IObservable<Milestone> GetForRepository(string owner, string name)
{
return _connection.GetAndFlattenAllPages<Milestone>(ApiUrls.Milestones(owner, name));
}

/// <summary>
/// Gets all open milestones for the repository.
/// </summary>
/// <remarks>
/// http://developer.github.com/v3/issues/milestones/#list-milestones-for-a-repository
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="request">Used to filter and sort the list of Milestones returned</param>
/// <returns></returns>
public IObservable<Milestone> GetForRepository(string owner, string name, MilestoneRequest request)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(request, "request");

return _connection.GetAndFlattenAllPages<Milestone>(ApiUrls.Milestones(owner, name),
request.ToParametersDictionary());
}

/// <summary>
/// Creates an Milestone for the specified repository. Any user with pull access to a repository can create an
/// Milestone.
/// </summary>
/// <remarks>http://developer.github.com/v3/issues/milestones/#create-a-milestone</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="newMilestone">A <see cref="NewMilestone"/> instance describing the new Milestone to create</param>
/// <returns></returns>
public IObservable<Milestone> Create(string owner, string name, NewMilestone newMilestone)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(newMilestone, "newMilestone");

return _client.Create(owner, name, newMilestone).ToObservable();
}

/// <summary>
/// Creates an Milestone for the specified repository. Any user with pull access to a repository can create an
/// Milestone.
/// </summary>
/// <remarks>http://developer.github.com/v3/issues/milestones/#create-a-milestone</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="number">The Milestone number</param>
/// <param name="milestoneUpdate">An <see cref="MilestoneUpdate"/> instance describing the changes to make to the Milestone
/// </param>
/// <returns></returns>
public IObservable<Milestone> Update(string owner, string name, int number, MilestoneUpdate milestoneUpdate)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(milestoneUpdate, "milestoneUpdate");

return _client.Update(owner, name, number, milestoneUpdate).ToObservable();
}

/// <summary>
/// Deletes a milestone for the specified repository. Any user with pull access to a repository can create an
/// Milestone.
/// </summary>
/// <remarks>http://developer.github.com/v3/issues/milestones/#delete-a-milestone</remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="number">The milestone number</param>
/// <returns></returns>
public IObservable<Unit> Delete(string owner, string name, int number)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");

return _client.Delete(owner, name, number).ToObservable();
}
}
}
12 changes: 6 additions & 6 deletions Octokit.Reactive/Helpers/ConnectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ namespace Octokit.Reactive.Internal
{
internal static class ConnectionExtensions
{
public static IObservable<T> GetAndFlattenAllPages<T>(this IConnection connection, Uri url)
public static IObservable<T> GetAndFlattenAllPages<T>(this IConnection connection, Uri url, IDictionary<string, string> parameters = null, string accepts = null)
{
return GetPages(url, nextPageUrl => connection.GetAsync<List<T>>(nextPageUrl).ToObservable());
return GetPages(url, parameters, (pageUrl, pageParams) => connection.GetAsync<List<T>>(pageUrl, pageParams, accepts).ToObservable());
}

static IObservable<T> GetPages<T>(Uri uri,
Func<Uri, IObservable<IResponse<List<T>>>> getPageFunc)
static IObservable<T> GetPages<T>(Uri uri, IDictionary<string, string> parameters,
Func<Uri, IDictionary<string, string>, IObservable<IResponse<List<T>>>> getPageFunc)
{
return getPageFunc(uri).Expand(resp =>
return getPageFunc(uri, parameters).Expand(resp =>
{
var nextPageUrl = resp.ApiInfo.GetNextPageUrl();
return nextPageUrl == null
? Observable.Empty<IResponse<List<T>>>()
: Observable.Defer(() => getPageFunc(nextPageUrl));
: Observable.Defer(() => getPageFunc(nextPageUrl, null));
})
.Where(resp => resp != null)
.SelectMany(resp => resp.BodyAsObject);
Expand Down
2 changes: 2 additions & 0 deletions Octokit.Reactive/Octokit.Reactive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
<Compile Include="..\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Clients\IObservableMilestonesClient.cs" />
<Compile Include="Clients\ObservableMilestonesClient.cs" />
<Compile Include="Clients\ObservableAssigneesClient.cs" />
<Compile Include="Clients\IObservableCommitStatusClient.cs" />
<Compile Include="Clients\ObservableCommitStatusClient.cs" />
Expand Down
1 change: 1 addition & 0 deletions Octokit.Tests.Integration/Octokit.Tests.Integration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="IntegrationTestAttribute.cs" />
<Compile Include="IssuesClientTests.cs" />
<Compile Include="MiscellaneousClientTests.cs" />
<Compile Include="Reactive\ObservableMilestonesClientTests.cs" />
<Compile Include="Reactive\ObservableRepositoriesClientTests.cs" />
<Compile Include="ReleasesClientTests.cs" />
<Compile Include="RepositoriesClientTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Linq;
using System.Net.Http.Headers;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Octokit.Reactive.Clients;
using Xunit;

namespace Octokit.Tests.Integration
{
public class ObservableMilestonesClientTests
{
public class TheGetMethod
{
[IntegrationTest]
public async Task ReturnsSpecifiedMilestone()
{
var github = new GitHubClient(new ProductHeaderValue("OctokitTests"))
{
Credentials = Helper.Credentials
};
var client = new ObservableMilestonesClient(github);
var observable = client.Get("libgit2", "libgit2sharp", 1);
var milestone = await observable;

Assert.Equal(1, milestone.Number);
Assert.Equal("v0.4.0", milestone.Title);
Assert.Equal(7, milestone.ClosedIssues);
}

[IntegrationTest]
public void ReturnsAllMilestones()
{
var github = new GitHubClient(new ProductHeaderValue("OctokitTests"))
{
Credentials = Helper.Credentials
};
var client = new ObservableMilestonesClient(github);
var milestones = client.GetForRepository("libgit2", "libgit2sharp", new MilestoneRequest { State = ItemState.Closed }).ToList().Wait();

Assert.NotEmpty(milestones);
Assert.True(milestones.All(m => m.State == ItemState.Closed));
}
}
}
}
Loading

0 comments on commit bc82697

Please sign in to comment.