-
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 #5 from JSkimming/get-zone
Added the Get Zones CloudFlare client method
- Loading branch information
Showing
44 changed files
with
1,521 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
namespace CloudFlare.NET | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
/// <summary> | ||
/// Represents the authorization parameters for accessing the CloudFlare API. | ||
/// </summary> | ||
/// <seealso href="https://api.cloudflare.com/#requests"/> | ||
public class CloudFlareAuth | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CloudFlareAuth"/> class. | ||
/// </summary> | ||
public CloudFlareAuth(string email, string key) | ||
{ | ||
if (string.IsNullOrWhiteSpace(email)) | ||
throw new ArgumentNullException(nameof(email)); | ||
if (string.IsNullOrWhiteSpace(key)) | ||
throw new ArgumentNullException(nameof(key)); | ||
|
||
Email = email; | ||
Key = key; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the Email address associated with your account. | ||
/// </summary> | ||
public string Email { get; } | ||
|
||
/// <summary> | ||
/// Gets the API key generated on the "My Account" page. | ||
/// </summary> | ||
public string Key { 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,39 @@ | ||
namespace CloudFlare.NET | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
/// <inheritdoc/> | ||
public class CloudFlareClient : ICloudFlareClient | ||
{ | ||
private static readonly Lazy<HttpClient> LazyClient = new Lazy<HttpClient>( | ||
() => | ||
{ | ||
var client = new HttpClient( | ||
new HttpClientHandler | ||
{ | ||
AutomaticDecompression = DecompressionMethods.GZip, | ||
}) | ||
; | ||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
return client; | ||
}, | ||
LazyThreadSafetyMode.PublicationOnly); | ||
|
||
private static HttpClient Client => LazyClient.Value; | ||
|
||
/// <inheritdoc/> | ||
public Task<IReadOnlyList<Zone>> GetZonesAsync(CancellationToken cancellationToken, CloudFlareAuth auth = null) | ||
{ | ||
return Client.GetZonesAsync(auth, cancellationToken); | ||
} | ||
} | ||
} |
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,18 @@ | ||
namespace CloudFlare.NET | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
/// <summary> | ||
/// Constants for CloudFlare. | ||
/// </summary> | ||
public static class CloudFlareConstants | ||
{ | ||
/// <summary> | ||
/// Gets the base address of the CloudFlare API. | ||
/// </summary> | ||
/// <seealso href="https://api.cloudflare.com/#endpoints"/> | ||
public static Uri BaseUri { get; } = new Uri("https://api.cloudflare.com/client/v4/", UriKind.Absolute); | ||
} | ||
} |
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,112 @@ | ||
namespace CloudFlare.NET | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
|
||
/// <summary> | ||
/// Represents an error that can occur as a result of a CloudFlare API request. | ||
/// </summary> | ||
/// <seealso href="https://api.cloudflare.com/#responses"/> | ||
public class CloudFlareError : IEquatable<CloudFlareError> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CloudFlareError"/> class. | ||
/// </summary> | ||
public CloudFlareError(int code, string message = null) | ||
{ | ||
Code = code; | ||
Message = message ?? string.Empty; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the error code. | ||
/// </summary> | ||
[JsonProperty("code")] | ||
public int Code { get; } | ||
|
||
/// <summary> | ||
/// Gets the error message. | ||
/// </summary> | ||
[JsonProperty("message")] | ||
public string Message { get; } | ||
|
||
/// <summary> | ||
/// The implicit operator to return the <see cref="Code"/> of a <see cref="CloudFlareError"/>. | ||
/// </summary> | ||
public static implicit operator int(CloudFlareError error) | ||
{ | ||
if (error == null) | ||
throw new ArgumentNullException(nameof(error)); | ||
|
||
return error.Code; | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether two specified <see cref="CloudFlareError"/> objects have the same value. | ||
/// </summary> | ||
/// <param name="left">The first <see cref="CloudFlareError"/> to compare, or <see langword="null"/>.</param> | ||
/// <param name="right">The second <see cref="CloudFlareError"/> to compare, or <see langword="null"/>.</param> | ||
/// <returns> | ||
/// <see langword="true"/> if the value of <paramref name="left"/> is the same as the value of | ||
/// <paramref name="right"/>; otherwise, <see langword="false"/>.</returns> | ||
public static bool operator ==(CloudFlareError left, CloudFlareError right) => Equals(left, right); | ||
|
||
/// <summary> | ||
/// Determines whether two specified <see cref="CloudFlareError"/> objects have different values. | ||
/// </summary> | ||
/// <param name="left">The first <see cref="CloudFlareError"/> to compare, or <see langword="null"/>.</param> | ||
/// <param name="right">The second <see cref="CloudFlareError"/> to compare, or <see langword="null"/>.</param> | ||
/// <returns> | ||
/// <see langword="true"/> if the value of <paramref name="left"/> is different from the value of | ||
/// <paramref name="right"/>; otherwise, <see langword="false"/>. | ||
/// </returns> | ||
public static bool operator !=(CloudFlareError left, CloudFlareError right) => !Equals(left, right); | ||
|
||
/// <summary> | ||
/// Indicates whether the current <see cref="CloudFlareError"/> is equal to another object | ||
/// <see cref="CloudFlareError"/>. | ||
/// </summary> | ||
/// <param name="other">An object to compare with this object.</param> | ||
/// <returns> | ||
/// <see langword="true"/> if the current object is equal to the <paramref name="other"/> parameter; | ||
/// otherwise, | ||
/// <see langword="false"/>. | ||
/// </returns> | ||
public bool Equals(CloudFlareError other) | ||
{ | ||
if (ReferenceEquals(null, other)) | ||
return false; | ||
if (ReferenceEquals(this, other)) | ||
return true; | ||
|
||
return Code == other.Code; | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the specified object is equal to the current object. | ||
/// </summary> | ||
/// <param name="obj">The object to compare with the current object. </param> | ||
/// <returns> | ||
/// <see langword="true"/> if the specified object is equal to the current object; otherwise, | ||
/// <see langword="false"/>. | ||
/// </returns> | ||
public override bool Equals(object obj) | ||
{ | ||
if (ReferenceEquals(null, obj)) | ||
return false; | ||
if (ReferenceEquals(this, obj)) | ||
return true; | ||
if (obj.GetType() != GetType()) | ||
return false; | ||
|
||
return Equals((CloudFlareError)obj); | ||
} | ||
|
||
/// <summary> | ||
/// Gets a hash code for the current object. | ||
/// </summary> | ||
public override int GetHashCode() => Code; | ||
} | ||
} |
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,36 @@ | ||
namespace CloudFlare.NET | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
|
||
/// <summary> | ||
/// Represent the base generic response from CloudFlare indicating success or failure. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the <see cref="Result"/>.</typeparam> | ||
/// <seealso href="https://api.cloudflare.com/#responses"/> | ||
public class CloudFlareResponse<T> : CloudFlareResponseBase | ||
where T : class | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CloudFlareResponse{T}"/> class. | ||
/// </summary> | ||
public CloudFlareResponse( | ||
bool success, | ||
T result = null, | ||
IReadOnlyList<CloudFlareError> errors = null, | ||
IReadOnlyList<string> messages = null, | ||
CloudFlareResultInfo resultInfo = null) | ||
: base(success, errors, messages, resultInfo) | ||
{ | ||
Result = result; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the result of the request. | ||
/// </summary> | ||
[JsonProperty("result")] | ||
public T Result { 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,61 @@ | ||
namespace CloudFlare.NET | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
|
||
/// <summary> | ||
/// Represent the base response from CloudFlare indicating success or failure. | ||
/// </summary> | ||
/// <seealso href="https://api.cloudflare.com/#responses"/> | ||
public class CloudFlareResponseBase | ||
{ | ||
private static readonly IReadOnlyList<CloudFlareError> EmptyErrors = | ||
Enumerable.Empty<CloudFlareError>().ToList(); | ||
|
||
private static readonly CloudFlareResultInfo DefaultResultInfo = | ||
new CloudFlareResultInfo(-1, -1, -1, -1); | ||
|
||
private static readonly IReadOnlyList<string> EmptyMessages = Enumerable.Empty<string>().ToList(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CloudFlareResponseBase"/> class. | ||
/// </summary> | ||
public CloudFlareResponseBase( | ||
bool success, | ||
IReadOnlyList<CloudFlareError> errors = null, | ||
IReadOnlyList<string> messages = null, | ||
CloudFlareResultInfo resultInfo = null) | ||
{ | ||
Success = success; | ||
Errors = errors ?? EmptyErrors; | ||
Messages = messages ?? EmptyMessages; | ||
ResultInfo = resultInfo ?? DefaultResultInfo; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the value indicating whether the request was successful. | ||
/// </summary> | ||
[JsonProperty("success")] | ||
public bool Success { get; } | ||
|
||
/// <summary> | ||
/// Gets the errors for unsuccessful requests. | ||
/// </summary> | ||
[JsonProperty("errors")] | ||
public IReadOnlyList<CloudFlareError> Errors { get; } | ||
|
||
/// <summary> | ||
/// Gets the messages for unsuccessful requests. | ||
/// </summary> | ||
[JsonProperty("messages")] | ||
public IReadOnlyList<string> Messages { get; } | ||
|
||
/// <summary> | ||
/// Gets the result info about the request. | ||
/// </summary> | ||
[JsonProperty("result_info")] | ||
public CloudFlareResultInfo ResultInfo { get; } | ||
} | ||
} |
Oops, something went wrong.