Files
octokit.net/Octokit.Reactive/Helpers/ConnectionExtensions.cs
Prayank Mathur 43f6cfe28b Added ApiOption overloads to methods on IRepositoryCommitsClient (#1247)
* Added ApiOptions overloads for RepositoryCommitClient methods

* Added overloads to Reactive clients

* Integration tests in progress

* Integration test addition in progress

* More integration tests added

* Added Ensure things

* Minor changes

* Minor changes

* Few changes

* Tried to fix errors

* Tried to fix errors

* Fixed a few things

* Fixed integration tests

* Tidying up

* Added public keyword

* Added unit tests for GetAll() in normal and reactive methods

* Minor changes

* Changed the class name

* Fixed the unit test
2016-04-10 17:28:01 -04:00

71 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
namespace Octokit.Reactive.Internal
{
public static class ConnectionExtensions
{
public static IObservable<T> GetAndFlattenAllPages<T>(this IConnection connection, Uri url)
{
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 connection.GetAndFlattenAllPages<T>(url, null, options);
}
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());
}
public static IObservable<T> GetAndFlattenAllPages<T>(this IConnection connection, Uri url, IDictionary<string, string> parameters, ApiOptions options)
{
return GetPagesWithOptions(url, parameters, options, (pageUrl, pageParams, o) =>
{
var passingParameters = Pagination.Setup(parameters, options);
return connection.Get<List<T>>(pageUrl, passingParameters, null).ToObservable();
});
}
public static IObservable<T> GetAndFlattenAllPages<T>(this IConnection connection, Uri url, IDictionary<string, string> parameters, string accepts)
{
return GetPages(url, parameters, (pageUrl, pageParams) => connection.Get<List<T>>(pageUrl, pageParams, accepts).ToObservable());
}
static IObservable<T> GetPages<T>(Uri uri, IDictionary<string, string> parameters,
Func<Uri, IDictionary<string, string>, IObservable<IApiResponse<List<T>>>> getPageFunc)
{
return getPageFunc(uri, parameters).Expand(resp =>
{
var nextPageUrl = resp.HttpResponse.ApiInfo.GetNextPageUrl();
return nextPageUrl == null
? Observable.Empty<IApiResponse<List<T>>>()
: Observable.Defer(() => getPageFunc(nextPageUrl, null));
})
.Where(resp => resp != null)
.SelectMany(resp => resp.Body);
}
static IObservable<T> GetPagesWithOptions<T>(Uri uri, IDictionary<string, string> parameters, ApiOptions options, Func<Uri, IDictionary<string, string>, ApiOptions, IObservable<IApiResponse<List<T>>>> getPageFunc)
{
return getPageFunc(uri, parameters, options).Expand(resp =>
{
var nextPageUrl = resp.HttpResponse.ApiInfo.GetNextPageUrl();
var shouldContinue = Pagination.ShouldContinue(nextPageUrl, options);
return shouldContinue
? Observable.Defer(() => getPageFunc(nextPageUrl, null, null))
: Observable.Empty<IApiResponse<List<T>>>();
})
.Where(resp => resp != null)
.SelectMany(resp => resp.Body);
}
}
}