added overloads to IApiConnection which support passing in ApiOptions

This commit is contained in:
Brendan Forster
2016-02-14 15:29:02 +11:00
parent 740b70f3c7
commit c098ecda60
6 changed files with 146 additions and 7 deletions
@@ -35,7 +35,7 @@ namespace Octokit.Tests.Clients
client.Received().GetAll<Authorization>(
Arg.Is<Uri>(u => u.ToString() == "authorizations"),
null);
Arg.Is<IDictionary<string,string>>(d => d == null));
}
}
+2 -2
View File
@@ -90,13 +90,13 @@ namespace Octokit.Tests.Http
new Response(),
new List<object> { new object(), new object() });
var connection = Substitute.For<IConnection>();
connection.Get<List<object>>(Args.Uri, null, null).Returns(Task.FromResult(response));
connection.Get<List<object>>(Args.Uri, Args.EmptyDictionary, null).Returns(Task.FromResult(response));
var apiConnection = new ApiConnection(connection);
var data = await apiConnection.GetAll<object>(getAllUri);
Assert.Equal(2, data.Count);
connection.Received().Get<List<object>>(getAllUri, null, null);
connection.Received().Get<List<object>>(getAllUri, Args.EmptyDictionary, null);
}
[Fact]
+1 -1
View File
@@ -36,7 +36,7 @@ namespace Octokit
/// <returns>A list of <see cref="Authorization"/>s.</returns>
public Task<IReadOnlyList<Authorization>> GetAll()
{
return ApiConnection.GetAll<Authorization>(ApiUrls.Authorizations(), null);
return ApiConnection.GetAll<Authorization>(ApiUrls.Authorizations(), (IDictionary<string,string>)null);
}
/// <summary>
+1 -1
View File
@@ -26,7 +26,7 @@ namespace Octokit
Ensure.ArgumentNotNull(connection, "connection");
Ensure.ArgumentNotNull(uri, "uri");
return connection.GetAll<T>(uri, null);
return connection.GetAll<T>(uri, ApiOptions.None);
}
/// <summary>
+108 -2
View File
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
@@ -115,7 +117,20 @@ namespace Octokit
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
public Task<IReadOnlyList<T>> GetAll<T>(Uri uri)
{
return GetAll<T>(uri, null, null);
return GetAll<T>(uri, ApiOptions.None);
}
/// <summary>
/// Gets all API resources in the list at the specified URI.
/// </summary>
/// <typeparam name="T">Type of the API resource in the list.</typeparam>
/// <param name="uri">URI of the API resource to get</param>
/// <param name="options">Options for changing the API response</param>
/// <returns><see cref="IReadOnlyList{T}"/> of the The API resources in the list.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
public Task<IReadOnlyList<T>> GetAll<T>(Uri uri, ApiOptions options)
{
return GetAll<T>(uri, null, null, options);
}
/// <summary>
@@ -128,7 +143,21 @@ namespace Octokit
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
public Task<IReadOnlyList<T>> GetAll<T>(Uri uri, IDictionary<string, string> parameters)
{
return GetAll<T>(uri, parameters, null);
return GetAll<T>(uri, parameters, null, ApiOptions.None);
}
/// <summary>
/// Gets all API resources in the list at the specified URI.
/// </summary>
/// <typeparam name="T">Type of the API resource in the list.</typeparam>
/// <param name="uri">URI of the API resource to get</param>
/// <param name="parameters">Parameters to add to the API request</param>
/// <param name="options">Options for changing the API response</param>
/// <returns><see cref="IReadOnlyList{T}"/> of the The API resources in the list.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
public Task<IReadOnlyList<T>> GetAll<T>(Uri uri, IDictionary<string, string> parameters, ApiOptions options)
{
return GetAll<T>(uri, parameters, null, options);
}
/// <summary>
@@ -148,6 +177,27 @@ namespace Octokit
.ConfigureAwait(false), uri);
}
public Task<IReadOnlyList<T>> GetAll<T>(Uri uri, IDictionary<string, string> parameters, string accepts, ApiOptions options)
{
Ensure.ArgumentNotNull(uri, "uri");
Ensure.ArgumentNotNull(options, "options");
parameters = 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 _pagination.GetAllPages(async () => await GetPage<T>(uri, parameters, accepts, options)
.ConfigureAwait(false), uri);
}
/// <summary>
/// Creates a new API resource in the list at the specified URI.
/// </summary>
@@ -483,5 +533,61 @@ namespace Octokit
response,
nextPageUri => Connection.Get<List<T>>(nextPageUri, parameters, accepts));
}
async Task<IReadOnlyPagedCollection<TU>> GetPage<TU>(
Uri uri,
IDictionary<string, string> parameters,
string accepts,
ApiOptions options)
{
Ensure.ArgumentNotNull(uri, "uri");
var connection = Connection;
var response = await connection.Get<List<TU>>(uri, parameters, accepts).ConfigureAwait(false);
return new ReadOnlyPagedCollection<TU>(
response,
nextPageUri =>
{
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(), StringComparison.OrdinalIgnoreCase))
{
return null;
}
}
}
return connection.Get<List<TU>>(nextPageUri, parameters, accepts);
});
}
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);
}
}
}
+33
View File
@@ -71,6 +71,16 @@ namespace Octokit
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<IReadOnlyList<T>> GetAll<T>(Uri uri);
/// <summary>
/// Gets all API resources in the list at the specified URI.
/// </summary>
/// <typeparam name="T">Type of the API resource in the list.</typeparam>
/// <param name="uri">URI of the API resource to get</param>
/// <param name="options">Options for changing the API response</param>
/// <returns><see cref="IReadOnlyList{T}"/> of the The API resources in the list.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<IReadOnlyList<T>> GetAll<T>(Uri uri, ApiOptions options);
/// <summary>
/// Gets all API resources in the list at the specified URI.
/// </summary>
@@ -81,6 +91,17 @@ namespace Octokit
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<IReadOnlyList<T>> GetAll<T>(Uri uri, IDictionary<string, string> parameters);
/// <summary>
/// Gets all API resources in the list at the specified URI.
/// </summary>
/// <typeparam name="T">Type of the API resource in the list.</typeparam>
/// <param name="uri">URI of the API resource to get</param>
/// <param name="parameters">Parameters to add to the API request</param>
/// <param name="options">Options for changing the API response</param>
/// <returns><see cref="IReadOnlyList{T}"/> of the The API resources in the list.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<IReadOnlyList<T>> GetAll<T>(Uri uri, IDictionary<string, string> parameters, ApiOptions options);
/// <summary>
/// Gets all API resources in the list at the specified URI.
/// </summary>
@@ -92,6 +113,18 @@ namespace Octokit
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<IReadOnlyList<T>> GetAll<T>(Uri uri, IDictionary<string, string> parameters, string accepts);
/// <summary>
/// Gets all API resources in the list at the specified URI.
/// </summary>
/// <typeparam name="T">Type of the API resource in the list.</typeparam>
/// <param name="uri">URI of the API resource to get</param>
/// <param name="parameters">Parameters to add to the API request</param>
/// <param name="accepts">Accept header to use for the API request</param>
/// <param name="options">Options for changing the API response</param>
/// <returns><see cref="IReadOnlyList{T}"/> of the The API resources in the list.</returns>
/// <exception cref="ApiException">Thrown when an API error occurs.</exception>
Task<IReadOnlyList<T>> GetAll<T>(Uri uri, IDictionary<string, string> parameters, string accepts, ApiOptions options);
/// <summary>
/// Creates a new API resource in the list at the specified URI.
/// </summary>