-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from JSkimming/get-dnsrecords
Added the Get DNS Records CloudFlare client method
- Loading branch information
Showing
27 changed files
with
599 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
namespace CloudFlare.NET | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
/// <summary> | ||
/// Represents a DNS record for a <see cref="Zone"/>. | ||
/// </summary> | ||
/// <seealso href="https://api.cloudflare.com/#dns-records-for-a-zone-properties"/> | ||
public class DnsRecord | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DnsRecord"/> class. | ||
/// </summary> | ||
public DnsRecord( | ||
IdentifierTag id, | ||
DnsRecordType type, | ||
string name, | ||
bool proxiable, | ||
bool proxied, | ||
int ttl, | ||
bool locked, | ||
IdentifierTag zoneId, | ||
DateTimeOffset createdOn, | ||
DateTimeOffset modifiedOn, | ||
int priority, | ||
string content = null, | ||
string zoneName = null, | ||
JObject data = null, | ||
JObject meta = null) | ||
{ | ||
if (id == null) | ||
throw new ArgumentNullException(nameof(id)); | ||
if (name == null) | ||
throw new ArgumentNullException(nameof(name)); | ||
if (zoneId == null) | ||
throw new ArgumentNullException(nameof(zoneId)); | ||
|
||
Id = id; | ||
Type = type; | ||
Name = name; | ||
Content = content ?? string.Empty; | ||
Proxiable = proxiable; | ||
Proxied = proxied; | ||
Ttl = ttl; | ||
Locked = locked; | ||
ZoneId = zoneId; | ||
ZoneName = zoneName ?? string.Empty; | ||
CreatedOn = createdOn; | ||
ModifiedOn = modifiedOn; | ||
Data = data ?? new JObject(); | ||
Meta = meta ?? new JObject(); | ||
Priority = priority; | ||
} | ||
|
||
/// <summary> | ||
/// API item identifier tag. | ||
/// </summary> | ||
[JsonProperty("id")] | ||
public IdentifierTag Id { get; } | ||
|
||
/// <summary> | ||
/// Record type. | ||
/// </summary> | ||
[JsonProperty("type")] | ||
public DnsRecordType Type { get; } | ||
|
||
/// <summary> | ||
/// A valid host name. | ||
/// </summary> | ||
[JsonProperty("name")] | ||
public string Name { get; } | ||
|
||
/// <summary> | ||
/// Valid content for the given <see cref="Type"/>. | ||
/// </summary> | ||
[JsonProperty("content")] | ||
public string Content { get; } | ||
|
||
/// <summary> | ||
/// Whether the record can be proxied by CloudFlare or not. | ||
/// </summary> | ||
[JsonProperty("proxiable")] | ||
public bool Proxiable { get; } | ||
|
||
/// <summary> | ||
/// Whether the record is receiving the performance and security benefits of CloudFlare | ||
/// </summary> | ||
[JsonProperty("proxied")] | ||
public bool Proxied { get; } | ||
|
||
/// <summary> | ||
/// Time to live for DNS record. Value of 1 is 'automatic'. | ||
/// </summary> | ||
[JsonProperty("ttl")] | ||
public int Ttl { get; } | ||
|
||
/// <summary> | ||
/// Whether this record can be modified/deleted (<see langword="true"/> means it's managed by CloudFlare). | ||
/// </summary> | ||
[JsonProperty("locked")] | ||
public bool Locked { get; } | ||
|
||
/// <summary> | ||
/// API item identifier tag. | ||
/// </summary> | ||
[JsonProperty("zone_id")] | ||
public IdentifierTag ZoneId { get; } | ||
|
||
/// <summary> | ||
/// The domain of the record. | ||
/// </summary> | ||
[JsonProperty("zone_name")] | ||
public string ZoneName { get; } | ||
|
||
/// <summary> | ||
/// When the record was created. | ||
/// </summary> | ||
[JsonProperty("created_on")] | ||
public DateTimeOffset CreatedOn { get; } | ||
|
||
/// <summary> | ||
/// When the record was last modified. | ||
/// </summary> | ||
[JsonProperty("modified_on")] | ||
public DateTimeOffset ModifiedOn { get; } | ||
|
||
/// <summary> | ||
/// Metadata about the record. | ||
/// </summary> | ||
[JsonProperty("data")] | ||
public JObject Data { get; } | ||
|
||
/// <summary> | ||
/// Extra CloudFlare-specific information about the record. | ||
/// </summary> | ||
[JsonProperty("meta")] | ||
public JObject Meta { get; } | ||
|
||
/// <summary> | ||
/// The priority of this is a <see cref="DnsRecordType.MX"/> record. | ||
/// </summary> | ||
[JsonProperty("priority")] | ||
public int Priority { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
namespace CloudFlare.NET | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
/// <summary> | ||
/// Helper extension methods on <see cref="IDnsRecordClient"/>. | ||
/// </summary> | ||
public static class DnsRecordClientExtensions | ||
{ | ||
/// <summary> | ||
/// Gets the zones for the subscription. | ||
/// </summary> | ||
/// <returns>The zones for the subscription.</returns> | ||
public static Task<IReadOnlyList<DnsRecord>> GetDnsRecordsAsync( | ||
this IDnsRecordClient client, | ||
IdentifierTag zoneId, | ||
CloudFlareAuth auth = null) | ||
{ | ||
if (client == null) | ||
throw new ArgumentNullException(nameof(client)); | ||
if (zoneId == null) | ||
throw new ArgumentNullException(nameof(zoneId)); | ||
|
||
return client.GetDnsRecordsAsync(zoneId, CancellationToken.None, auth); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace CloudFlare.NET | ||
{ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
// ReSharper disable InconsistentNaming | ||
#pragma warning disable 1591 | ||
|
||
/// <summary> | ||
/// The type of a <see cref="DnsRecord"/>. | ||
/// </summary> | ||
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1602:EnumerationItemsMustBeDocumented", | ||
Justification = "Names a self-explanatory.")] | ||
public enum DnsRecordType | ||
{ | ||
A, | ||
AAAA, | ||
CNAME, | ||
TXT, | ||
SRV, | ||
LOC, | ||
MX, | ||
NS, | ||
SPF, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace CloudFlare.NET | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
/// <summary> | ||
/// Extension methods on <see cref="HttpClient"/> to wrap the Zone APIs | ||
/// </summary> | ||
/// <seealso href="https://api.cloudflare.com/#dns-records-for-a-zone"/> | ||
public static class HttpClientDnsRecordExtensions | ||
{ | ||
/// <summary> | ||
/// Gets the zones for the account specified by the <paramref name="auth"/> details. | ||
/// </summary> | ||
public static async Task<IReadOnlyList<DnsRecord>> GetDnsRecordsAsync( | ||
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)); | ||
|
||
Uri uri = new Uri(CloudFlareConstants.BaseUri, $"zones/{zoneId}/dns_records"); | ||
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<DnsRecord>>(cancellationToken) | ||
.ConfigureAwait(false)) | ||
.Result; | ||
} | ||
} | ||
} |
Oops, something went wrong.