Skip to content

Commit

Permalink
Merge pull request #17 from JSkimming/paged-results
Browse files Browse the repository at this point in the history
Added support for paged results
  • Loading branch information
JSkimming committed Sep 3, 2015
2 parents a4de058 + afdc3e3 commit 523a2ed
Show file tree
Hide file tree
Showing 40 changed files with 1,001 additions and 118 deletions.
5 changes: 3 additions & 2 deletions src/CloudFlare.NET.Yaml/CloudFlare.NET.Yaml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<ItemGroup>
<!-- A reference to the entire .NET Framework is automatically included -->
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.6.0.2\lib\portable-net45+wp80+win8+wpa81\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.6.0.4\lib\portable-net45+wp80+win8+wpa81\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Http, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
Expand Down Expand Up @@ -94,7 +94,8 @@
<Analyzer Include="..\packages\AsyncFixer.1.0.0.0\tools\analyzers\System.Composition.Hosting.dll" />
<Analyzer Include="..\packages\AsyncFixer.1.0.0.0\tools\analyzers\System.Composition.Runtime.dll" />
<Analyzer Include="..\packages\AsyncFixer.1.0.0.0\tools\analyzers\System.Composition.TypedParts.dll" />
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0-beta008\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0-beta009\analyzers\dotnet\cs\Newtonsoft.Json.dll" />
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0-beta009\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
4 changes: 2 additions & 2 deletions src/CloudFlare.NET.Yaml/CloudFlare.NET.Yaml.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>$id$</id>
<version>0.0.5</version>
<version>0.1.0</version>
<title>$title$</title>
<authors>James Skimming</authors>
<owners>James Skimming</owners>
Expand All @@ -12,7 +12,7 @@
<description>$description$</description>
<tags>CloudFlare YAML API Client</tags>
<dependencies>
<dependency id="CloudFlare.NET" version="0.0.5" />
<dependency id="CloudFlare.NET" version="0.1.0" />
</dependencies>
</metadata>
</package>
5 changes: 3 additions & 2 deletions src/CloudFlare.NET.Yaml/CloudFlareClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ public static async Task<ZoneData> GetZoneDataAsync(
throw new ArgumentNullException(nameof(zoneId));

Task<Zone> zoneTask = client.GetZoneAsync(zoneId, cancellationToken, auth);
Task<IReadOnlyList<DnsRecord>> dnsRecordsTask = client.GetDnsRecordsAsync(zoneId, cancellationToken, auth);
Task<IEnumerable<DnsRecord>> dnsRecordsTask =
client.GetAllDnsRecordsAsync(zoneId, cancellationToken, auth: auth);

await Task.WhenAll(zoneTask, dnsRecordsTask).ConfigureAwait(false);

return new ZoneData(zoneTask.Result, dnsRecordsTask.Result);
return new ZoneData(zoneTask.Result, dnsRecordsTask.Result.ToArray());
}
}
}
4 changes: 2 additions & 2 deletions src/CloudFlare.NET.Yaml/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.5.0")]
[assembly: AssemblyFileVersion("0.0.5.0")]
[assembly: AssemblyVersion("0.1.0.0")]
[assembly: AssemblyFileVersion("0.1.0.0")]
4 changes: 2 additions & 2 deletions src/CloudFlare.NET.Yaml/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="portable45-net45+win8+wp8+wpa81" />
<package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="portable45-net45+win8+wp8+wpa81" />
<package id="Microsoft.Net.Http" version="2.2.22" targetFramework="portable45-net45+win8+wp8+wpa81" />
<package id="Newtonsoft.Json" version="6.0.2" targetFramework="portable45-net45+win8+wp8+wpa81" />
<package id="StyleCop.Analyzers" version="1.0.0-beta008" targetFramework="portable45-net45+win8+wp8+wpa81" developmentDependency="true" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="portable45-net45+win8+wp8+wpa81" />
<package id="StyleCop.Analyzers" version="1.0.0-beta009" targetFramework="portable45-net45+win8+wp8+wpa81" developmentDependency="true" />
<package id="YamlDotNet" version="3.7.0" targetFramework="portable45-net45+win8+wp8+wpa81" />
</packages>
14 changes: 12 additions & 2 deletions src/CloudFlare.NET/CloudFlare.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<ItemGroup>
<!-- A reference to the entire .NET Framework is automatically included -->
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.6.0.2\lib\portable-net45+wp80+win8+wpa81\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.6.0.4\lib\portable-net45+wp80+win8+wpa81\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Net.Http, Version=1.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
Expand Down Expand Up @@ -84,6 +84,8 @@
<Compile Include="IIdentifier.cs" />
<Compile Include="IModified.cs" />
<Compile Include="IZoneClient.cs" />
<Compile Include="PagedDnsRecordOrderFieldTypes.cs" />
<Compile Include="PagedDnsRecordParameters.cs" />
<Compile Include="PagedParameters.cs" />
<Compile Include="PagedParametersExtensions.cs" />
<Compile Include="PagedParametersMatchType.cs" />
Expand All @@ -101,7 +103,15 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0-beta008\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
<Analyzer Include="..\packages\AsyncFixer.1.0.0.0\tools\analyzers\AsyncFixer.dll" />
<Analyzer Include="..\packages\AsyncFixer.1.0.0.0\tools\analyzers\RoslynUtilities.dll" />
<Analyzer Include="..\packages\AsyncFixer.1.0.0.0\tools\analyzers\System.Composition.AttributedModel.dll" />
<Analyzer Include="..\packages\AsyncFixer.1.0.0.0\tools\analyzers\System.Composition.Convention.dll" />
<Analyzer Include="..\packages\AsyncFixer.1.0.0.0\tools\analyzers\System.Composition.Hosting.dll" />
<Analyzer Include="..\packages\AsyncFixer.1.0.0.0\tools\analyzers\System.Composition.Runtime.dll" />
<Analyzer Include="..\packages\AsyncFixer.1.0.0.0\tools\analyzers\System.Composition.TypedParts.dll" />
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0-beta009\analyzers\dotnet\cs\Newtonsoft.Json.dll" />
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0-beta009\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
2 changes: 1 addition & 1 deletion src/CloudFlare.NET/CloudFlare.NET.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>$id$</id>
<version>0.0.5</version>
<version>0.1.0</version>
<title>$title$</title>
<authors>James Skimming</authors>
<owners>James Skimming</owners>
Expand Down
81 changes: 78 additions & 3 deletions src/CloudFlare.NET/CloudFlareClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

/// <inheritdoc/>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
Expand Down Expand Up @@ -141,14 +142,27 @@ public static HttpClientHandler CreateDefaultHttpClientHandler()
}

/// <inheritdoc/>
public Task<IReadOnlyList<Zone>> GetZonesAsync(
public Task<CloudFlareResponse<IReadOnlyList<Zone>>> GetZonesAsync(
CancellationToken cancellationToken,
PagedZoneParameters parameters = null,
CloudFlareAuth auth = null)
{
return _client.GetZonesAsync(cancellationToken, auth ?? _auth, parameters);
}

/// <inheritdoc/>
public Task<IEnumerable<Zone>> GetAllZonesAsync(
CancellationToken cancellationToken,
PagedZoneParameters parameters = null,
CloudFlareAuth auth = null)
{
return GetAllPagedResultsAsync<Zone, PagedZoneParameters, PagedZoneOrderFieldTypes>(
_client.GetZonesAsync,
cancellationToken,
auth ?? _auth,
parameters);
}

/// <inheritdoc/>
public Task<Zone> GetZoneAsync(
IdentifierTag zoneId,
Expand All @@ -159,12 +173,73 @@ public Task<Zone> GetZoneAsync(
}

/// <inheritdoc/>
public Task<IReadOnlyList<DnsRecord>> GetDnsRecordsAsync(
public Task<CloudFlareResponse<IReadOnlyList<DnsRecord>>> GetDnsRecordsAsync(
IdentifierTag zoneId,
CancellationToken cancellationToken,
PagedDnsRecordParameters parameters = null,
CloudFlareAuth auth = null)
{
return _client.GetDnsRecordsAsync(zoneId, cancellationToken, auth ?? _auth, parameters);
}

/// <inheritdoc/>
public Task<IEnumerable<DnsRecord>> GetAllDnsRecordsAsync(
IdentifierTag zoneId,
CancellationToken cancellationToken,
PagedDnsRecordParameters parameters = null,
CloudFlareAuth auth = null)
{
return _client.GetDnsRecordsAsync(zoneId, cancellationToken, auth ?? _auth);
if (zoneId == null)
throw new ArgumentNullException(nameof(zoneId));

return GetAllPagedResultsAsync<DnsRecord, PagedDnsRecordParameters, PagedDnsRecordOrderFieldTypes>(
(ct, a, p) => _client.GetDnsRecordsAsync(zoneId, ct, a, p),
cancellationToken,
auth ?? _auth,
parameters);
}

private static async Task<IEnumerable<TResult>> GetAllPagedResultsAsync<TResult, TParams, TOrder>(
Func<CancellationToken, CloudFlareAuth, TParams, Task<CloudFlareResponse<IReadOnlyList<TResult>>>> request,
CancellationToken cancellationToken,
CloudFlareAuth auth,
TParams parameters = null)
where TResult : class
where TParams : PagedParameters<TOrder>
where TOrder : struct
{
IEnumerable<TResult> result = Enumerable.Empty<TResult>();
int page = 0;
CloudFlareResultInfo resultInfo;

do
{
++page;

// Create the paged parameters;
JObject jsonParams = JObject.FromObject(new { page, per_page = 100 });

// If parameters have been supplied use them, but override the paged parameters into them.
if (parameters != null)
{
JObject mergedParams = JObject.FromObject(parameters);
mergedParams.Merge(jsonParams);
jsonParams = mergedParams;
}

TParams pagedParams = jsonParams.ToObject<TParams>();
CloudFlareResponse<IReadOnlyList<TResult>> response =
await request(
cancellationToken,
auth,
pagedParams).ConfigureAwait(false);

result = result.Concat(response.Result);
resultInfo = response.ResultInfo;
}
while (resultInfo.Page < resultInfo.TotalPages);

return result;
}
}
}
7 changes: 3 additions & 4 deletions src/CloudFlare.NET/CloudFlareResponseBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class CloudFlareResponseBase
{
private static readonly IReadOnlyList<CloudFlareError> EmptyErrors =
Enumerable.Empty<CloudFlareError>().ToList();
private static readonly IReadOnlyList<CloudFlareError> EmptyErrors = new CloudFlareError[0];

private static readonly CloudFlareResultInfo DefaultResultInfo =
new CloudFlareResultInfo(-1, -1, -1, -1);
new CloudFlareResultInfo(-1, -1, -1, -1, -1);

private static readonly IReadOnlyList<string> EmptyMessages = Enumerable.Empty<string>().ToList();
private static readonly IReadOnlyList<string> EmptyMessages = new string[0];

/// <summary>
/// Initializes a new instance of the <see cref="CloudFlareResponseBase"/> class.
Expand Down
17 changes: 12 additions & 5 deletions src/CloudFlare.NET/CloudFlareResultInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,41 @@ public class CloudFlareResultInfo
/// <summary>
/// Initializes a new instance of the <see cref="CloudFlareResultInfo"/> class.
/// </summary>
public CloudFlareResultInfo(int page, int perPage, int count, int totalCount)
public CloudFlareResultInfo(int page, int totalPages, int perPage, int count, int totalCount)
{
Page = page;
TotalPages = totalPages;
PerPage = perPage;
Count = count;
TotalCount = totalCount;
}

/// <summary>
/// Gets the TBD.
/// Gets the page number of the current <see cref="CloudFlareResponse{T}"/>.
/// </summary>
[JsonProperty("page")]
public int Page { get; }

/// <summary>
/// Gets the TBD.
/// Gets the total number of pages.
/// </summary>
[JsonProperty("total_pages")]
public int TotalPages { get; }

/// <summary>
/// Gets the number of results per page.
/// </summary>
[JsonProperty("per_page")]
public int PerPage { get; }

/// <summary>
/// Gets the TBD.
/// Gets the number of results of the current <see cref="CloudFlareResponse{T}"/>.
/// </summary>
[JsonProperty("count")]
public int Count { get; }

/// <summary>
/// Gets the TBD.
/// Gets the total number of results.
/// </summary>
[JsonProperty("total_count")]
public int TotalCount { get; }
Expand Down
63 changes: 60 additions & 3 deletions src/CloudFlare.NET/DnsRecordClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,74 @@ public static class DnsRecordClientExtensions
/// Gets the zones for the subscription.
/// </summary>
/// <seealso href="https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records"/>
public static Task<IReadOnlyList<DnsRecord>> GetDnsRecordsAsync(
public static Task<CloudFlareResponse<IReadOnlyList<DnsRecord>>> GetDnsRecordsAsync(
this IDnsRecordClient client,
IdentifierTag zoneId,
CloudFlareAuth auth = null)
PagedDnsRecordParameters parameters = null)
{
if (client == null)
throw new ArgumentNullException(nameof(client));
if (zoneId == null)
throw new ArgumentNullException(nameof(zoneId));

return client.GetDnsRecordsAsync(zoneId, CancellationToken.None, auth);
return client.GetDnsRecordsAsync(zoneId, CancellationToken.None, parameters);
}

/// <summary>
/// Gets the zones for the subscription.
/// </summary>
/// <seealso href="https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records"/>
public static Task<CloudFlareResponse<IReadOnlyList<DnsRecord>>> GetDnsRecordsAsync(
this IDnsRecordClient client,
IdentifierTag zoneId,
CloudFlareAuth auth,
PagedDnsRecordParameters parameters = null)
{
if (client == null)
throw new ArgumentNullException(nameof(client));
if (zoneId == null)
throw new ArgumentNullException(nameof(zoneId));
if (auth == null)
throw new ArgumentNullException(nameof(auth));

return client.GetDnsRecordsAsync(zoneId, CancellationToken.None, parameters, auth);
}

/// <summary>
/// Gets the zones for the subscription.
/// </summary>
/// <seealso href="https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records"/>
public static Task<IEnumerable<DnsRecord>> GetAllDnsRecordsAsync(
this IDnsRecordClient client,
IdentifierTag zoneId,
PagedDnsRecordParameters parameters = null)
{
if (client == null)
throw new ArgumentNullException(nameof(client));
if (zoneId == null)
throw new ArgumentNullException(nameof(zoneId));

return client.GetAllDnsRecordsAsync(zoneId, CancellationToken.None, parameters);
}

/// <summary>
/// Gets the zones for the subscription.
/// </summary>
/// <seealso href="https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records"/>
public static Task<IEnumerable<DnsRecord>> GetAllDnsRecordsAsync(
this IDnsRecordClient client,
IdentifierTag zoneId,
CloudFlareAuth auth,
PagedDnsRecordParameters parameters = null)
{
if (client == null)
throw new ArgumentNullException(nameof(client));
if (zoneId == null)
throw new ArgumentNullException(nameof(zoneId));
if (auth == null)
throw new ArgumentNullException(nameof(auth));

return client.GetAllDnsRecordsAsync(zoneId, CancellationToken.None, parameters, auth);
}
}
}
12 changes: 9 additions & 3 deletions src/CloudFlare.NET/HttpClientDnsRecordExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@ public static class HttpClientDnsRecordExtensions
/// Gets the zones for the account specified by the <paramref name="auth"/> details.
/// </summary>
/// <seealso href="https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records"/>
public static Task<IReadOnlyList<DnsRecord>> GetDnsRecordsAsync(
public static Task<CloudFlareResponse<IReadOnlyList<DnsRecord>>> GetDnsRecordsAsync(
this HttpClient client,
IdentifierTag zoneId,
CancellationToken cancellationToken,
CloudFlareAuth auth)
CloudFlareAuth auth,
PagedDnsRecordParameters parameters = null)
{
if (zoneId == null)
throw new ArgumentNullException(nameof(zoneId));

Uri uri = new Uri(CloudFlareConstants.BaseUri, $"zones/{zoneId}/dns_records");
return client.GetAsync<IReadOnlyList<DnsRecord>>(uri, auth, cancellationToken);
if (parameters != null)
{
uri = new UriBuilder(uri) { Query = parameters.ToQuery() }.Uri;
}

return client.GetCloudFlareResponseAsync<IReadOnlyList<DnsRecord>>(uri, auth, cancellationToken);
}
}
}
Loading

0 comments on commit 523a2ed

Please sign in to comment.