mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-02 10:55:53 +00:00
updated IObservableReleasesClient to use ApiOptions, and added some tests
This commit is contained in:
@@ -18,6 +18,19 @@ namespace Octokit.Reactive
|
||||
/// <returns>The list of <see cref="Release"/>s for the specified repository.</returns>
|
||||
IObservable<Release> GetAll(string owner, string name);
|
||||
|
||||
/// <summary>
|
||||
/// Gets all <see cref="Release"/>s for the specified repository.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// See the <a href="http://developer.github.com/v3/repos/releases/#list-releases-for-a-repository">API documentation</a> for more information.
|
||||
/// </remarks>
|
||||
/// <param name="owner">The repository's owner</param>
|
||||
/// <param name="name">The repository's name</param>
|
||||
/// <param name="options">Options for changing the API response</param>
|
||||
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
|
||||
/// <returns>The list of <see cref="Release"/>s for the specified repository.</returns>
|
||||
IObservable<Release> GetAll(string owner, string name, ApiOptions options);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a single <see cref="Release"/> for the specified repository.
|
||||
/// </summary>
|
||||
|
||||
@@ -36,6 +36,26 @@ namespace Octokit.Reactive
|
||||
return _connection.GetAndFlattenAllPages<Release>(ApiUrls.Releases(owner, name));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all <see cref="Release"/>s for the specified repository.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// See the <a href="http://developer.github.com/v3/repos/releases/#list-releases-for-a-repository">API documentation</a> for more information.
|
||||
/// </remarks>
|
||||
/// <param name="owner">The repository's owner</param>
|
||||
/// <param name="name">The repository's name</param>
|
||||
/// <param name="options">Options for changing the API response</param>
|
||||
/// <exception cref="ApiException">Thrown when a general API error occurs.</exception>
|
||||
/// <returns>The list of <see cref="Release"/>s for the specified repository.</returns>
|
||||
public IObservable<Release> GetAll(string owner, string name, ApiOptions options)
|
||||
{
|
||||
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
|
||||
Ensure.ArgumentNotNullOrEmptyString(name, "name");
|
||||
Ensure.ArgumentNotNull(options, "options");
|
||||
|
||||
return _connection.GetAndFlattenAllPages<Release>(ApiUrls.Releases(owner, name), options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a single <see cref="Release"/> for the specified repository.
|
||||
/// </summary>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reactive.Threading.Tasks;
|
||||
|
||||
@@ -12,6 +14,26 @@ namespace Octokit.Reactive.Internal
|
||||
return GetPages(url, null, (pageUrl, pageParams) => connection.Get<List<T>>(pageUrl, null, null).ToObservable());
|
||||
}
|
||||
|
||||
public static IObservable<T> GetAndFlattenAllPages<T>(this IConnection connection, Uri url, ApiOptions options)
|
||||
{
|
||||
return GetPagesWithOptions(url, options, (pageUrl, o) =>
|
||||
{
|
||||
var parameters = new Dictionary<string, string>();
|
||||
|
||||
if (options.PageSize.HasValue)
|
||||
{
|
||||
parameters.Add("per_page", options.PageSize.Value.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
if (options.StartPage.HasValue)
|
||||
{
|
||||
parameters.Add("page", options.StartPage.Value.ToString(CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
return connection.Get<List<T>>(pageUrl, parameters, null).ToObservable();
|
||||
});
|
||||
}
|
||||
|
||||
public static IObservable<T> GetAndFlattenAllPages<T>(this IConnection connection, Uri url, IDictionary<string, string> parameters)
|
||||
{
|
||||
return GetPages(url, parameters, (pageUrl, pageParams) => connection.Get<List<T>>(pageUrl, pageParams, null).ToObservable());
|
||||
@@ -35,5 +57,60 @@ namespace Octokit.Reactive.Internal
|
||||
.Where(resp => resp != null)
|
||||
.SelectMany(resp => resp.Body);
|
||||
}
|
||||
|
||||
static IObservable<T> GetPagesWithOptions<T>(Uri uri, ApiOptions options,
|
||||
Func<Uri, ApiOptions, IObservable<IApiResponse<List<T>>>> getPageFunc)
|
||||
{
|
||||
return getPageFunc(uri, options).Expand(resp =>
|
||||
{
|
||||
var nextPageUri = resp.HttpResponse.ApiInfo.GetNextPageUrl();
|
||||
|
||||
if (nextPageUri == null)
|
||||
{
|
||||
return Observable.Empty<IApiResponse<List<T>>>();
|
||||
}
|
||||
|
||||
if (nextPageUri.Query.Contains("page=") && options.PageCount.HasValue)
|
||||
{
|
||||
var allValues = ToQueryStringDictionary(nextPageUri);
|
||||
|
||||
string pageValue;
|
||||
if (allValues.TryGetValue("page", out pageValue))
|
||||
{
|
||||
var startPage = options.StartPage ?? 1;
|
||||
var pageCount = options.PageCount.Value;
|
||||
|
||||
var endPage = startPage + pageCount;
|
||||
if (pageValue.Equals(endPage.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return Observable.Empty<IApiResponse<List<T>>>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Observable.Defer(() => getPageFunc(nextPageUri, null));
|
||||
})
|
||||
.Where(resp => resp != null)
|
||||
.SelectMany(resp => resp.Body);
|
||||
}
|
||||
|
||||
static Dictionary<string, string> ToQueryStringDictionary(Uri uri)
|
||||
{
|
||||
return uri.Query.Split('&')
|
||||
.Select(keyValue =>
|
||||
{
|
||||
var indexOf = keyValue.IndexOf('=');
|
||||
if (indexOf > 0)
|
||||
{
|
||||
var key = keyValue.Substring(0, indexOf);
|
||||
var value = keyValue.Substring(indexOf + 1);
|
||||
return new KeyValuePair<string, string>(key, value);
|
||||
}
|
||||
|
||||
//just a plain old value, return it
|
||||
return new KeyValuePair<string, string>(keyValue, null);
|
||||
})
|
||||
.ToDictionary(x => x.Key, x => x.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,6 +132,7 @@
|
||||
<Compile Include="Reactive\Enterprise\ObservableEnterpriseOrganizationClientTests.cs" />
|
||||
<Compile Include="Reactive\ObservableIssuesClientTests.cs" />
|
||||
<Compile Include="Reactive\ObservableMilestonesClientTests.cs" />
|
||||
<Compile Include="Reactive\ObservableReleaseClientTests.cs" />
|
||||
<Compile Include="Reactive\ObservableRepositoriesClientTests.cs" />
|
||||
<Compile Include="Clients\ReleasesClientTests.cs" />
|
||||
<Compile Include="Clients\RepositoriesClientTests.cs" />
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Octokit.Reactive;
|
||||
using Xunit;
|
||||
|
||||
namespace Octokit.Tests.Integration.Reactive
|
||||
{
|
||||
public class ObservableReleaseClientTests
|
||||
{
|
||||
public class TheGetAllMethod
|
||||
{
|
||||
readonly ObservableReleasesClient _releaseClient;
|
||||
const string owner = "octokit";
|
||||
const string name = "octokit.net";
|
||||
|
||||
public TheGetAllMethod()
|
||||
{
|
||||
var github = Helper.GetAuthenticatedClient();
|
||||
|
||||
_releaseClient = new ObservableReleasesClient(github);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task ReturnsReleases()
|
||||
{
|
||||
var releases = await _releaseClient.GetAll(owner, name).ToList();
|
||||
|
||||
Assert.NotEmpty(releases);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task ReturnsCorrectCountOfReleasesWithoutStart()
|
||||
{
|
||||
var options = new ApiOptions
|
||||
{
|
||||
PageSize = 5,
|
||||
PageCount = 1
|
||||
};
|
||||
|
||||
var releases = await _releaseClient.GetAll(owner, name, options).ToList();
|
||||
|
||||
Assert.Equal(5, releases.Count);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task ReturnsCorrectCountOfReleasesWithStart()
|
||||
{
|
||||
var options = new ApiOptions
|
||||
{
|
||||
PageSize = 5,
|
||||
PageCount = 1,
|
||||
StartPage = 2
|
||||
};
|
||||
|
||||
var releases = await _releaseClient.GetAll(owner, name, options).ToList();
|
||||
|
||||
Assert.Equal(5, releases.Count);
|
||||
}
|
||||
|
||||
[IntegrationTest]
|
||||
public async Task ReturnsDistinctResultsBasedOnStartPage()
|
||||
{
|
||||
var startOptions = new ApiOptions
|
||||
{
|
||||
PageSize = 5,
|
||||
PageCount = 1
|
||||
};
|
||||
|
||||
var firstPage = await _releaseClient.GetAll(owner, name, startOptions).ToList();
|
||||
|
||||
var skipStartOptions = new ApiOptions
|
||||
{
|
||||
PageSize = 5,
|
||||
PageCount = 1,
|
||||
StartPage = 2
|
||||
};
|
||||
|
||||
var secondPage = await _releaseClient.GetAll(owner, name, skipStartOptions).ToList();
|
||||
|
||||
Assert.NotEqual(firstPage[0].Id, secondPage[0].Id);
|
||||
Assert.NotEqual(firstPage[1].Id, secondPage[1].Id);
|
||||
Assert.NotEqual(firstPage[2].Id, secondPage[2].Id);
|
||||
Assert.NotEqual(firstPage[3].Id, secondPage[3].Id);
|
||||
Assert.NotEqual(firstPage[4].Id, secondPage[4].Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user