Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zone list parameters #14

Merged
merged 6 commits into from
Aug 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Analyzers.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
<Rule Id="SA1611" Action="None" />
<Rule Id="SA1615" Action="None" />
<Rule Id="SA1633" Action="None" />
<Rule Id="SA1412" Action="Info" />
</Rules>
</RuleSet>
6 changes: 3 additions & 3 deletions src/CloudFlare.NET.Yaml/CloudFlare.NET.Yaml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@
<HintPath>..\packages\Microsoft.Net.Http.2.2.22\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.Primitives.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="YamlDotNet, Version=3.6.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\YamlDotNet.3.6.1\lib\portable-net45+netcore45+wpa81+wp8+MonoAndroid1+MonoTouch1\YamlDotNet.dll</HintPath>
<Reference Include="YamlDotNet, Version=3.7.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\YamlDotNet.3.7.0\lib\portable-net45+netcore45+wpa81+wp8+MonoAndroid1+MonoTouch1\YamlDotNet.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
Expand Down Expand Up @@ -94,7 +94,7 @@
<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-beta003\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0-beta008\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.4</version>
<version>0.0.5</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.3" />
<dependency id="CloudFlare.NET" version="0.0.5" />
</dependencies>
</metadata>
</package>
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.4.0")]
[assembly: AssemblyFileVersion("0.0.4.0")]
[assembly: AssemblyVersion("0.0.5.0")]
[assembly: AssemblyFileVersion("0.0.5.0")]
6 changes: 3 additions & 3 deletions src/CloudFlare.NET.Yaml/packages.config
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AsyncFixer" version="1.0.0.0" targetFramework="portable45-net45+win8+wp8+wpa81" developmentDependency="true" />
<package id="AsyncFixer" version="1.0.0.0" targetFramework="portable45-net45+win8+wp8+wpa81" developmentDependency="true" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.0" targetFramework="portable45-net45+win8+wp8+wpa81" />
<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-beta003" targetFramework="portable45-net45+win8+wp8+wpa81" developmentDependency="true" />
<package id="YamlDotNet" version="3.6.1" 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="YamlDotNet" version="3.7.0" targetFramework="portable45-net45+win8+wp8+wpa81" />
</packages>
9 changes: 8 additions & 1 deletion src/CloudFlare.NET/CloudFlare.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,24 @@
<Compile Include="IIdentifier.cs" />
<Compile Include="IModified.cs" />
<Compile Include="IZoneClient.cs" />
<Compile Include="PagedParameters.cs" />
<Compile Include="PagedParametersExtensions.cs" />
<Compile Include="PagedParametersMatchType.cs" />
<Compile Include="PagedParametersOrderType.cs" />
<Compile Include="PagedZoneOrderFieldTypes.cs" />
<Compile Include="PagedZoneParameters.cs" />
<Compile Include="Serialization\IsoDateTimeOffsetConverter.cs" />
<Compile Include="Serialization\ToStringJsonConverter.cs" />
<Compile Include="Zone.cs" />
<Compile Include="ZoneClientExtensions.cs" />
<Compile Include="ZoneStatusType.cs" />
<None Include="app.config" />
<None Include="CloudFlare.NET.nuspec" />
<None Include="packages.config" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0-beta003\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0-beta008\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
6 changes: 5 additions & 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.4</version>
<version>0.0.5</version>
<title>$title$</title>
<authors>James Skimming</authors>
<owners>James Skimming</owners>
Expand All @@ -11,5 +11,9 @@
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<tags>CloudFlare API Client</tags>
<dependencies>
<!-- Need to add explicitly, otherwise it is removed. -->
<dependency id="Microsoft.Net.Http" version="2.2.22" />
</dependencies>
</metadata>
</package>
15 changes: 9 additions & 6 deletions src/CloudFlare.NET/CloudFlareClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
/// <inheritdoc/>
public class CloudFlareClient : ICloudFlareClient
{
private readonly HttpClient _client;

private readonly CloudFlareAuth _auth;

private static readonly Lazy<HttpClient> LazyClient = new Lazy<HttpClient>(
() => CreateDefaultHttpClient(),
LazyThreadSafetyMode.PublicationOnly);

private readonly HttpClient _client;

private readonly CloudFlareAuth _auth;

/// <summary>
/// Initializes a new instance of the <see cref="CloudFlareClient"/> class.
/// </summary>
Expand Down Expand Up @@ -127,9 +127,12 @@ public static HttpClientHandler CreateDefaultHttpClientHandler()
}

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

/// <inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion src/CloudFlare.NET/DnsRecordType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/// The type of a <see cref="DnsRecord"/>.
/// </summary>
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:EnumerationItemsMustBeDocumented",
Justification = "Names a self-explanatory.")]
Justification = "Names are self-explanatory.")]
[JsonConverter(typeof(StringEnumConverter))]
public enum DnsRecordType
{
Expand Down
8 changes: 7 additions & 1 deletion src/CloudFlare.NET/HttpClientZoneExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ public static class HttpClientZoneExtensions
public static async Task<IReadOnlyList<Zone>> GetZonesAsync(
this HttpClient client,
CancellationToken cancellationToken,
CloudFlareAuth auth)
CloudFlareAuth auth,
PagedZoneParameters parameters = null)
{
if (client == null)
throw new ArgumentNullException(nameof(client));
if (auth == null)
throw new ArgumentNullException(nameof(auth));

Uri uri = new Uri(CloudFlareConstants.BaseUri, "zones");
if (parameters != null)
{
uri = new UriBuilder(uri) { Query = parameters.ToQuery() }.Uri;
}

var request = new HttpRequestMessage(HttpMethod.Get, uri);
request.AddAuth(auth);

Expand Down
5 changes: 4 additions & 1 deletion src/CloudFlare.NET/IZoneClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public interface IZoneClient
/// Gets the zones for the subscription.
/// </summary>
/// <seealso href="https://api.cloudflare.com/#zone-list-zones"/>
Task<IReadOnlyList<Zone>> GetZonesAsync(CancellationToken cancellationToken, CloudFlareAuth auth = null);
Task<IReadOnlyList<Zone>> GetZonesAsync(
CancellationToken cancellationToken,
PagedZoneParameters parameters = null,
CloudFlareAuth auth = null);

/// <summary>
/// Gets the zone with the specified <paramref name="zoneId"/>.
Expand Down
62 changes: 62 additions & 0 deletions src/CloudFlare.NET/PagedParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace CloudFlare.NET
{
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

/// <summary>
/// Specifies the parameters of a request supports paging.
/// </summary>
/// <typeparam name="TOrder">The type of the <see cref="Order"/> property.</typeparam>
public abstract class PagedParameters<TOrder>
where TOrder : struct
{
/// <summary>
/// Initializes a new instance of the <see cref="PagedParameters{TOrder}"/> class.
/// </summary>
protected PagedParameters(
int page,
int perPage,
TOrder order,
PagedParametersOrderType direction,
PagedParametersMatchType match)
{
Page = page;
PerPage = perPage;
Order = order;
Direction = direction;
Match = match;
}

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

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

/// <summary>
/// Field to order results by.
/// </summary>
[JsonProperty("order")]
public TOrder Order { get; }

/// <summary>
/// Gets the direction to order.
/// </summary>
[JsonProperty("direction")]
public PagedParametersOrderType Direction { get; }

/// <summary>
/// Gets the value of whether to match all search requirements or at least one (any).
/// </summary>
[JsonProperty("match")]
public PagedParametersMatchType Match { get; }
}
}
62 changes: 62 additions & 0 deletions src/CloudFlare.NET/PagedParametersExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace CloudFlare.NET
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

/// <summary>
/// Extension methinks on paged parameter objects.
/// </summary>
public static class PagedParametersExtensions
{
/// <summary>
/// <para>Converts the <paramref name="parameters"/> to their equivalent key value pair representation.</para>
/// <para>NOTE. Default values are excluded</para>
/// </summary>
/// <typeparam name="TOrder">The type of the <see cref="PagedParameters{TOrder}.Order"/> property.</typeparam>
public static IEnumerable<KeyValuePair<string, string>> ToKvp<TOrder>(this PagedParameters<TOrder> parameters)
where TOrder : struct
{
if (parameters == null)
throw new ArgumentNullException(nameof(parameters));

// Remove default values to ensure they're not passed as parameters.
JsonSerializer serializer = JsonSerializer.CreateDefault(new JsonSerializerSettings
{
DefaultValueHandling = DefaultValueHandling.Ignore,
});

JObject json = JObject.FromObject(parameters, serializer);

return
((IDictionary<string, JToken>)json).Select(
kvp => new KeyValuePair<string, string>(kvp.Key, kvp.Value.Value<string>()));
}

/// <summary>
/// <para>Converts the <paramref name="parameters"/> to their equivalent query string representation.</para>
/// <para>NOTE. Default values are excluded</para>
/// </summary>
/// <typeparam name="TOrder">The type of the <see cref="PagedParameters{TOrder}.Order"/> property.</typeparam>
public static string ToQuery<TOrder>(this PagedParameters<TOrder> parameters)
where TOrder : struct
{
IEnumerable<KeyValuePair<string, string>> keyValuePairs = parameters.ToKvp();

StringBuilder stringBuilder = new StringBuilder();
foreach (KeyValuePair<string, string> keyValuePair in keyValuePairs)
{
if (stringBuilder.Length > 0)
stringBuilder.Append('&');
stringBuilder.Append(Uri.EscapeDataString(keyValuePair.Key));
stringBuilder.Append('=');
stringBuilder.Append(Uri.EscapeDataString(keyValuePair.Value));
}

return stringBuilder.ToString();
}
}
}
23 changes: 23 additions & 0 deletions src/CloudFlare.NET/PagedParametersMatchType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace CloudFlare.NET
{
using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

// ReSharper disable InconsistentNaming
#pragma warning disable 1591

/// <summary>
/// The types by which a paged results are matched.
/// </summary>
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:EnumerationItemsMustBeDocumented",
Justification = "Names are self-explanatory.")]
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1303:ConstFieldNamesMustBeginWithUpperCaseLetter",
Justification = "Named to match serialized values.")]
[JsonConverter(typeof(StringEnumConverter))]
public enum PagedParametersMatchType
{
all,
any,
}
}
23 changes: 23 additions & 0 deletions src/CloudFlare.NET/PagedParametersOrderType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace CloudFlare.NET
{
using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

// ReSharper disable InconsistentNaming
#pragma warning disable 1591

/// <summary>
/// The types by which a paged results an be ordered.
/// </summary>
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:EnumerationItemsMustBeDocumented",
Justification = "Names are self-explanatory.")]
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1303:ConstFieldNamesMustBeginWithUpperCaseLetter",
Justification = "Named to match serialized values.")]
[JsonConverter(typeof(StringEnumConverter))]
public enum PagedParametersOrderType
{
asc,
desc,
}
}
25 changes: 25 additions & 0 deletions src/CloudFlare.NET/PagedZoneOrderFieldTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace CloudFlare.NET
{
using System.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

// ReSharper disable InconsistentNaming
#pragma warning disable 1591

/// <summary>
/// The fields by which zones can be ordered.
/// </summary>
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:EnumerationItemsMustBeDocumented",
Justification = "Names are self-explanatory.")]
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1303:ConstFieldNamesMustBeginWithUpperCaseLetter",
Justification = "Named to match serialized values.")]
[JsonConverter(typeof(StringEnumConverter))]
public enum PagedZoneOrderFieldTypes
{
none,
name,
status,
email,
}
}
Loading