Skip to content

Commit

Permalink
#types
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftkey committed Mar 4, 2015
1 parent d2229b7 commit 63ac22a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ public async Task CanPageRepositories()
var github = Helper.GetAuthenticatedClient();

var repositories = await github.Repository.GetAllForCurrent()
.WithOptions(pageSize: 10, startPage: 0, pageCount: 1);
.WithOptions(new ApiOptions { PageSize = 10, PageCount = 1 });

Assert.Equal(10, repositories.Count);
}
Expand Down
16 changes: 9 additions & 7 deletions Octokit/Http/ILazyRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

namespace Octokit
{
public class ApiOptions
{
public int? StartPage { get; set; }
public int? PageCount { get; set; }
public int? PageSize { get; set; }
public string Accepts { get; set; }
}

public interface ILazyRequest<T>
{
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed",
Justification = "let me make this money")]

This comment has been minimized.

Copy link
@keithduncan

keithduncan Mar 4, 2015

lol

ILazyRequest<T> WithOptions(
int startPage = 0,
int pageCount = -1,
int pageSize = 30,
string accepts = null);
ILazyRequest<T> WithOptions(ApiOptions options);

TaskAwaiter<IReadOnlyList<T>> ToTask();
}
Expand Down
35 changes: 12 additions & 23 deletions Octokit/Http/LazyRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ public class LazyRequest<T> : ILazyRequest<T>
readonly Uri _uri;
readonly IDictionary<string, string> _parameters;

int defaultPageSize = 30;
int _pageSize = 30;
int _startPage;
int _pageCount;
ApiOptions _options = new ApiOptions();

public LazyRequest(IApiConnection apiConnection, Uri uri)
: this(apiConnection, uri, new Dictionary<string, string>()) { }
Expand All @@ -33,33 +30,22 @@ public LazyRequest(IApiConnection apiConnection, Uri uri, IDictionary<string, st
_parameters = parameters;
}

[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed",
Justification = "let me make this money")]
public ILazyRequest<T> WithOptions(
int startPage,
int pageCount,
int pageSize,
string accepts = null)
public ILazyRequest<T> WithOptions(ApiOptions options)
{
this._pageSize = pageSize;
this._startPage = startPage;
this._pageCount = pageCount;

// TODO: set custom accepts

this._options = options;
return this;
}

public TaskAwaiter<IReadOnlyList<T>> ToTask()
{
if (_pageSize != defaultPageSize)
if (_options.PageSize.HasValue)
{
_parameters.Add("per_page", _pageSize.ToString(CultureInfo.InvariantCulture));
_parameters.Add("per_page", _options.PageSize.Value.ToString(CultureInfo.InvariantCulture));
}

if (_startPage > 0)
if (_options.StartPage.HasValue)
{
_parameters.Add("page", _startPage.ToString(CultureInfo.InvariantCulture));
_parameters.Add("page", _options.StartPage.Value.ToString(CultureInfo.InvariantCulture));
}

return _pagination.GetAllPages(
Expand All @@ -82,14 +68,17 @@ async Task<IReadOnlyPagedCollection<TU>> GetPage<TU>(
response,
nextPageUri =>
{
if (nextPageUri.Query.Contains("page=") && _pageCount > -1)
if (nextPageUri.Query.Contains("page=") && _options.PageCount.HasValue)
{
var allValues = ToQueryStringDictionary(nextPageUri);

string pageValue;
if (allValues.TryGetValue("page", out pageValue))
{
var endPage = _startPage + _pageCount + 1;
var startPage = _options.StartPage ?? 1;
var pageCount = _options.PageCount.Value;

var endPage = startPage + pageCount;
if (pageValue.Equals(endPage.ToString(), StringComparison.OrdinalIgnoreCase))
{
return null;
Expand Down

1 comment on commit 63ac22a

@keithduncan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.