Added some Zone features

This commit is contained in:
2024-10-24 17:12:14 +02:00
parent 83620cb450
commit c6eb6ba05e
49 changed files with 3595 additions and 34 deletions

View File

@@ -1,6 +1,5 @@
using System;
using System.Net.Http;
using System.Text.RegularExpressions;
namespace AMWD.Net.Api.Cloudflare.Auth
{
@@ -9,7 +8,6 @@ namespace AMWD.Net.Api.Cloudflare.Auth
/// </summary>
public class ApiKeyAuthentication : IAuthentication
{
private static readonly Regex _emailCheckRegex = new(@"^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$", RegexOptions.Compiled);
private readonly string _emailAddress;
private readonly string _apiKey;
@@ -20,15 +18,11 @@ namespace AMWD.Net.Api.Cloudflare.Auth
/// <param name="apiKey">The global API key.</param>
public ApiKeyAuthentication(string emailAddress, string apiKey)
{
if (string.IsNullOrWhiteSpace(emailAddress))
throw new ArgumentNullException(nameof(emailAddress));
emailAddress.ValidateCloudflareEmailAddress();
if (string.IsNullOrWhiteSpace(apiKey))
throw new ArgumentNullException(nameof(apiKey));
if (!_emailCheckRegex.IsMatch(emailAddress))
throw new ArgumentException("Invalid email address", nameof(emailAddress));
_emailAddress = emailAddress;
_apiKey = apiKey;
}

View File

@@ -94,15 +94,19 @@ namespace AMWD.Net.Api.Cloudflare
}
/// <inheritdoc/>
public async Task<CloudflareResponse<TResponse>> PostAsync<TResponse, TRequest>(string requestPath, TRequest request, CancellationToken cancellationToken = default)
public async Task<CloudflareResponse<TResponse>> PostAsync<TResponse, TRequest>(string requestPath, TRequest request, IQueryParameterFilter queryFilter = null, CancellationToken cancellationToken = default)
{
ThrowIfDisposed();
ValidateRequestPath(requestPath);
string requestUrl = BuildRequestUrl(requestPath);
string requestUrl = BuildRequestUrl(requestPath, queryFilter);
HttpContent httpRequestContent;
if (request is HttpContent httpContent)
if (request == null)
{
httpRequestContent = null;
}
else if (request is HttpContent httpContent)
{
httpRequestContent = httpContent;
}
@@ -125,7 +129,11 @@ namespace AMWD.Net.Api.Cloudflare
string requestUrl = BuildRequestUrl(requestPath);
HttpContent httpRequestContent;
if (request is HttpContent httpContent)
if (request == null)
{
httpRequestContent = null;
}
else if (request is HttpContent httpContent)
{
httpRequestContent = httpContent;
}
@@ -140,12 +148,12 @@ namespace AMWD.Net.Api.Cloudflare
}
/// <inheritdoc/>
public async Task<CloudflareResponse<TResponse>> DeleteAsync<TResponse>(string requestPath, CancellationToken cancellationToken = default)
public async Task<CloudflareResponse<TResponse>> DeleteAsync<TResponse>(string requestPath, IQueryParameterFilter queryFilter = null, CancellationToken cancellationToken = default)
{
ThrowIfDisposed();
ValidateRequestPath(requestPath);
string requestUrl = BuildRequestUrl(requestPath);
string requestUrl = BuildRequestUrl(requestPath, queryFilter);
var response = await _httpClient.DeleteAsync(requestUrl, cancellationToken).ConfigureAwait(false);
return await GetCloudflareResponse<TResponse>(response, cancellationToken).ConfigureAwait(false);
@@ -285,7 +293,7 @@ namespace AMWD.Net.Api.Cloudflare
return new CloudflareResponse<TRes>
{
Success = true,
ResultInfo = new ResultInfo(),
ResultInfo = new PaginationInfo(),
Result = (TRes)cObj,
};
}

View File

@@ -4,21 +4,21 @@ using Newtonsoft.Json.Converters;
namespace AMWD.Net.Api.Cloudflare
{
/// <summary>
/// The direction to order the entity.
/// The direction to sort the entity.
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum OrderDirection
public enum SortDirection
{
/// <summary>
/// Order in ascending order.
/// Sort in ascending order.
/// </summary>
[EnumMember(Value = "asc")]
Asc = 1,
Ascending = 1,
/// <summary>
/// Order in descending order.
/// Sort in descending order.
/// </summary>
[EnumMember(Value = "desc")]
Desc = 2
Descending = 2
}
}

View File

@@ -0,0 +1,64 @@
using System;
using System.Text.RegularExpressions;
namespace AMWD.Net.Api.Cloudflare
{
/// <summary>
/// Extension methods for <see cref="string"/>s.
/// </summary>
public static class StringExtensions
{
private static readonly Regex _emailCheckRegex = new(@"^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$", RegexOptions.Compiled);
/// <summary>
/// Validate basic information for a Cloudflare ID.
/// </summary>
/// <remarks>
/// An Cloudflare ID has max. 32 characters.
/// </remarks>
/// <param name="id">The string to check.</param>
/// <exception cref="ArgumentNullException">The <paramref name="id"/> is <see langword="null"/> or any kind of whitespace.</exception>
/// <exception cref="ArgumentOutOfRangeException">The <paramref name="id"/> has more than 32 characters.</exception>
public static void ValidateCloudflareId(this string id)
{
if (string.IsNullOrWhiteSpace(id))
throw new ArgumentNullException(nameof(id));
if (id.Length > 32)
throw new ArgumentOutOfRangeException(nameof(id));
}
/// <summary>
/// Validate basic information for a Cloudflare name.
/// </summary>
/// <remarks>
/// An Cloudflare name has max. 253 characters.
/// </remarks>
/// <param name="name">The string to check.</param>
/// <exception cref="ArgumentNullException">The <paramref name="name"/> is <see langword="null"/> or any kind of whitespace.</exception>
/// <exception cref="ArgumentOutOfRangeException">The <paramref name="name"/> has more than 253 characters.</exception>
public static void ValidateCloudflareName(this string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentNullException(nameof(name));
if (name.Length > 253)
throw new ArgumentOutOfRangeException(nameof(name));
}
/// <summary>
/// Validate basic information for an email address.
/// </summary>
/// <param name="emailAddress">The string to check.</param>
/// <exception cref="ArgumentNullException">The <paramref name="emailAddress"/> is <see langword="null"/> or any kind of whitespace.</exception>
/// <exception cref="ArgumentException">The <paramref name="emailAddress"/> does not match the regular expression pattern for an email address.</exception>
public static void ValidateCloudflareEmailAddress(this string emailAddress)
{
if (string.IsNullOrWhiteSpace(emailAddress))
throw new ArgumentNullException(nameof(emailAddress));
if (!_emailCheckRegex.IsMatch(emailAddress))
throw new ArgumentException("Invalid email address", nameof(emailAddress));
}
}
}

View File

@@ -31,8 +31,9 @@ namespace AMWD.Net.Api.Cloudflare
/// <typeparam name="TRequest">The request type.</typeparam>
/// <param name="requestPath">The request path (extending the base URL).</param>
/// <param name="request">The request content.</param>
/// <param name="queryFilter">The query parameters.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
Task<CloudflareResponse<TResponse>> PostAsync<TResponse, TRequest>(string requestPath, TRequest request, CancellationToken cancellationToken = default);
Task<CloudflareResponse<TResponse>> PostAsync<TResponse, TRequest>(string requestPath, TRequest request, IQueryParameterFilter queryFilter = null, CancellationToken cancellationToken = default);
/// <summary>
/// Makes a PUT request to the Cloudflare API.
@@ -55,9 +56,10 @@ namespace AMWD.Net.Api.Cloudflare
/// </remarks>
/// <typeparam name="TResponse">The response type.</typeparam>
/// <param name="requestPath">The request path (extending the base URL).</param>
/// <param name="queryFilter">The query parameters.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
/// <returns></returns>
Task<CloudflareResponse<TResponse>> DeleteAsync<TResponse>(string requestPath, CancellationToken cancellationToken = default);
Task<CloudflareResponse<TResponse>> DeleteAsync<TResponse>(string requestPath, IQueryParameterFilter queryFilter = null, CancellationToken cancellationToken = default);
/// <summary>
/// Makes a PATCH request to the Cloudflare API.

View File

@@ -11,7 +11,7 @@ namespace AMWD.Net.Api.Cloudflare
/// Information about the result of the request.
/// </summary>
[JsonProperty("result_info")]
public ResultInfo ResultInfo { get; set; }
public PaginationInfo ResultInfo { get; set; }
/// <summary>
/// Whether the API call was successful.

View File

@@ -1,9 +1,9 @@
namespace AMWD.Net.Api.Cloudflare
{
/// <summary>
/// Cloudflare Result Information.
/// Cloudflare pagination information.
/// </summary>
public class ResultInfo
public class PaginationInfo
{
/// <summary>
/// Total number of results for the requested service.