Skip to content

Commit

Permalink
Merge pull request #5 from JSkimming/get-zone
Browse files Browse the repository at this point in the history
Added the Get Zones CloudFlare client method
  • Loading branch information
JSkimming committed Aug 3, 2015
2 parents 4686498 + 923ae70 commit 7ba6e62
Show file tree
Hide file tree
Showing 44 changed files with 1,521 additions and 60 deletions.
8 changes: 4 additions & 4 deletions coverage.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
@SET coverage_results=%~dp0src\TestResults\Test.Coverage.xml

@IF NOT EXIST "%~dp0src\TestResults" MD "%~dp0src\TestResults"
::@echo "%mspec_exe%" "%test_assemblies%" --html "%spec_results%"
::@"%mspec_exe%" "%test_assemblies%" --html "%spec_results%"
::@echo "%mspec_exe%" "%test_assemblies%" --timeinfo --silent --html "%spec_results%"
::@"%mspec_exe%" "%test_assemblies%" --timeinfo --silent --html "%spec_results%"
::@echo "%xunit_exe%" "%test_assemblies%" -noshadow -html "%xunit_results%"
::@"%xunit_exe%" "%test_assemblies%" -noshadow -html "%xunit_results%"

@echo "%cover_exe%" -register:user "-target:%mspec_exe%" "-targetargs:%test_assemblies% --html %spec_results%" -filter:%coverage_filter% "-output:%coverage_results%"
@"%cover_exe%" -register:user "-target:%mspec_exe%" "-targetargs:%test_assemblies% --html %spec_results%" -filter:%coverage_filter% "-output:%coverage_results%"
@echo "%cover_exe%" -register:user "-target:%mspec_exe%" "-targetargs:%test_assemblies% --timeinfo --silent --html %spec_results%" -filter:%coverage_filter% "-output:%coverage_results%"
@"%cover_exe%" -register:user "-target:%mspec_exe%" "-targetargs:%test_assemblies% --timeinfo --silent --html %spec_results%" -filter:%coverage_filter% "-output:%coverage_results%"

@echo "%cover_exe%" -register:user "-target:%xunit_exe%" "-targetargs:%test_assemblies% -noshadow -html %xunit_results%" -mergeoutput -filter:%coverage_filter% "-output:%coverage_results%"
@"%cover_exe%" -register:user "-target:%xunit_exe%" "-targetargs:%test_assemblies% -noshadow -html %xunit_results%" -mergeoutput -filter:%coverage_filter% "-output:%coverage_results%"
Expand Down
2 changes: 2 additions & 0 deletions src/Analyzers.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<Rule Id="SA1503" Action="None" />
<Rule Id="SA1600" Action="None" />
<Rule Id="SA1609" Action="None" />
<Rule Id="SA1611" Action="None" />
<Rule Id="SA1615" Action="None" />
<Rule Id="SA1633" Action="None" />
</Rules>
</RuleSet>
14 changes: 13 additions & 1 deletion src/CloudFlare.NET/CloudFlare.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,24 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CloudFlareAuth.cs" />
<Compile Include="CloudFlareClient.cs" />
<Compile Include="CloudFlareConstants.cs" />
<Compile Include="CloudFlareError.cs" />
<Compile Include="CloudFlareResponse.cs" />
<Compile Include="CloudFlareResponseBase.cs" />
<Compile Include="CloudFlareResultInfo.cs" />
<Compile Include="HttpClientZoneExtensions.cs" />
<Compile Include="ICloudFlareClient.cs" />
<Compile Include="IdentifierTag.cs" />
<Compile Include="IdentifierTag.IEquatable.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="IZoneClient.cs" />
<Compile Include="Zone.cs" />
<Compile Include="ZoneClientExtensions.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-beta001\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
Expand Down
37 changes: 37 additions & 0 deletions src/CloudFlare.NET/CloudFlareAuth.cs
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; }
}
}
39 changes: 39 additions & 0 deletions src/CloudFlare.NET/CloudFlareClient.cs
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);
}
}
}
18 changes: 18 additions & 0 deletions src/CloudFlare.NET/CloudFlareConstants.cs
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);
}
}
112 changes: 112 additions & 0 deletions src/CloudFlare.NET/CloudFlareError.cs
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;
}
}
36 changes: 36 additions & 0 deletions src/CloudFlare.NET/CloudFlareResponse.cs
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; }
}
}
61 changes: 61 additions & 0 deletions src/CloudFlare.NET/CloudFlareResponseBase.cs
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; }
}
}
Loading

0 comments on commit 7ba6e62

Please sign in to comment.