Skip to content

Commit

Permalink
Merge pull request #9 from JSkimming/get-zone
Browse files Browse the repository at this point in the history
Added the Client method to get a zone
  • Loading branch information
JSkimming committed Aug 19, 2015
2 parents 87525a6 + cfbdd29 commit 7f316f9
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/CloudFlare.NET/CloudFlareClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ public Task<IReadOnlyList<Zone>> GetZonesAsync(CancellationToken cancellationTok
return Client.GetZonesAsync(cancellationToken, auth);
}

/// <inheritdoc/>
public Task<Zone> GetZoneAsync(
IdentifierTag zoneId,
CancellationToken cancellationToken,
CloudFlareAuth auth = null)
{
return Client.GetZoneAsync(zoneId, cancellationToken, auth);
}

/// <inheritdoc/>
public Task<IReadOnlyList<DnsRecord>> GetDnsRecordsAsync(
IdentifierTag zoneId,
Expand Down
3 changes: 1 addition & 2 deletions src/CloudFlare.NET/DnsRecordClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -15,7 +14,7 @@ public static class DnsRecordClientExtensions
/// <summary>
/// Gets the zones for the subscription.
/// </summary>
/// <returns>The zones for the subscription.</returns>
/// <seealso href="https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records"/>
public static Task<IReadOnlyList<DnsRecord>> GetDnsRecordsAsync(
this IDnsRecordClient client,
IdentifierTag zoneId,
Expand Down
1 change: 1 addition & 0 deletions src/CloudFlare.NET/HttpClientDnsRecordExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static class HttpClientDnsRecordExtensions
/// <summary>
/// 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 async Task<IReadOnlyList<DnsRecord>> GetDnsRecordsAsync(
this HttpClient client,
IdentifierTag zoneId,
Expand Down
38 changes: 33 additions & 5 deletions src/CloudFlare.NET/HttpClientZoneExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,60 @@
public static class HttpClientZoneExtensions
{
/// <summary>
/// Gets the base address of the CloudFlare API.
/// Gets the zones for the account specified by the <paramref name="auth"/> details.
/// </summary>
public static Uri ZonesUri { get; } = new Uri(CloudFlareConstants.BaseUri, "zones");
/// <seealso href="https://api.cloudflare.com/#zone-list-zones"/>
public static async Task<IReadOnlyList<Zone>> GetZonesAsync(
this HttpClient client,
CancellationToken cancellationToken,
CloudFlareAuth auth)
{
if (client == null)
throw new ArgumentNullException(nameof(client));
if (auth == null)
throw new ArgumentNullException(nameof(auth));

Uri uri = new Uri(CloudFlareConstants.BaseUri, "zones");
var request = new HttpRequestMessage(HttpMethod.Get, uri);
request.AddAuth(auth);

HttpResponseMessage response =
await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
.ConfigureAwait(false);

return (await response
.GetResultAsync<IReadOnlyList<Zone>>(cancellationToken)
.ConfigureAwait(false))
.Result;
}

/// <summary>
/// Gets the zones for the account specified by the <paramref name="auth"/> details.
/// </summary>
public static async Task<IReadOnlyList<Zone>> GetZonesAsync(
/// <seealso href="https://api.cloudflare.com/#zone-zone-details"/>
public static async Task<Zone> GetZoneAsync(
this HttpClient client,
IdentifierTag zoneId,
CancellationToken cancellationToken,
CloudFlareAuth auth)
{
if (client == null)
throw new ArgumentNullException(nameof(client));
if (zoneId == null)
throw new ArgumentNullException(nameof(zoneId));
if (auth == null)
throw new ArgumentNullException(nameof(auth));

var request = new HttpRequestMessage(HttpMethod.Get, ZonesUri);
Uri uri = new Uri(CloudFlareConstants.BaseUri, $"zones/{zoneId}");
var request = new HttpRequestMessage(HttpMethod.Get, uri);
request.AddAuth(auth);

HttpResponseMessage response =
await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken)
.ConfigureAwait(false);

return (await response
.GetResultAsync<IReadOnlyList<Zone>>(cancellationToken)
.GetResultAsync<Zone>(cancellationToken)
.ConfigureAwait(false))
.Result;
}
Expand Down
8 changes: 7 additions & 1 deletion src/CloudFlare.NET/IZoneClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ public interface IZoneClient
/// <summary>
/// Gets the zones for the subscription.
/// </summary>
/// <returns>The zones for the subscription.</returns>
/// <seealso href="https://api.cloudflare.com/#zone-list-zones"/>
Task<IReadOnlyList<Zone>> GetZonesAsync(CancellationToken cancellationToken, CloudFlareAuth auth = null);

/// <summary>
/// Gets the zone with the specified <paramref name="zoneId"/>.
/// </summary>
/// <seealso href="https://api.cloudflare.com/#zone-zone-details"/>
Task<Zone> GetZoneAsync(IdentifierTag zoneId, CancellationToken cancellationToken, CloudFlareAuth auth);
}
}
19 changes: 18 additions & 1 deletion src/CloudFlare.NET/ZoneClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,30 @@ public static class ZoneClientExtensions
/// <summary>
/// Gets the zones for the subscription.
/// </summary>
/// <returns>The zones for the subscription.</returns>
/// <seealso href="https://api.cloudflare.com/#zone-list-zones"/>
public static Task<IReadOnlyList<Zone>> GetZonesAsync(this IZoneClient client, CloudFlareAuth auth = null)
{
if (client == null)
throw new ArgumentNullException(nameof(client));

return client.GetZonesAsync(CancellationToken.None, auth);
}

/// <summary>
/// Gets the zones for the subscription.
/// </summary>
/// <seealso href="https://api.cloudflare.com/#zone-zone-details"/>
public static Task<Zone> GetZoneAsync(
this IZoneClient client,
IdentifierTag zoneId,
CloudFlareAuth auth = null)
{
if (zoneId == null)
throw new ArgumentNullException(nameof(zoneId));
if (client == null)
throw new ArgumentNullException(nameof(client));

return client.GetZoneAsync(zoneId, CancellationToken.None, auth);
}
}
}
25 changes: 25 additions & 0 deletions src/Tests/CloudFlare.NET.Tests/ZoneClientExtensionsSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,29 @@ public class When_getting_zones : FixtureContext

It should_return_the_zones = () => _actual.ShouldBeTheSameAs(_expected);
}

[Subject(typeof(ZoneClientExtensions))]
public class When_getting_a_zone : FixtureContext
{
static Mock<IZoneClient> _zoneClientMock;
static IdentifierTag _zoneId;
static CloudFlareAuth _auth;
static Zone _expected;
static Zone _actual;

Establish context = () =>
{
_zoneClientMock = _fixture.Create<Mock<IZoneClient>>();
_auth = _fixture.Create<CloudFlareAuth>();
_expected = _fixture.Create<Zone>();
_zoneId = _expected.Id;
_zoneClientMock
.Setup(c => c.GetZoneAsync(_zoneId, CancellationToken.None, _auth))
.ReturnsAsync(_expected);
};

Because of = () => _actual = _zoneClientMock.Object.GetZoneAsync(_zoneId, _auth).Await();

It should_return_the_zones = () => _actual.ShouldBeTheSameAs(_expected);
}
}

0 comments on commit 7f316f9

Please sign in to comment.