Moving folder structure
This commit is contained in:
32
src/Cloudflare/Extensions/EnumExtensions.cs
Normal file
32
src/Cloudflare/Extensions/EnumExtensions.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace AMWD.Net.Api.Cloudflare
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="Enum"/>s.
|
||||
/// </summary>
|
||||
public static class EnumExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the <see cref="EnumMemberAttribute.Value"/> of the <see cref="Enum"/> when available, otherwise the <see cref="Enum.ToString()"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">The enum value.</param>
|
||||
public static string? GetEnumMemberValue(this Enum value)
|
||||
{
|
||||
var fieldInfo = value.GetType().GetField(value.ToString());
|
||||
if (fieldInfo == null)
|
||||
return value.ToString();
|
||||
|
||||
var enumMember = fieldInfo
|
||||
.GetCustomAttributes(typeof(EnumMemberAttribute), inherit: false)
|
||||
.Cast<EnumMemberAttribute>()
|
||||
.FirstOrDefault();
|
||||
|
||||
if (enumMember == null)
|
||||
return value.ToString();
|
||||
|
||||
return enumMember.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
53
src/Cloudflare/Extensions/HttpClientExtensions.cs
Normal file
53
src/Cloudflare/Extensions/HttpClientExtensions.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
#if ! NET6_0_OR_GREATER
|
||||
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace System.Net.Http
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for <see cref="HttpClient"/>s.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Copied from <see href="https://github.com/dotnet/runtime/blob/v6.0.0/src/libraries/System.Net.Http/src/System/Net/Http/HttpClient.cs">.NET 6 runtime / HttpClient</see>.
|
||||
/// </remarks>
|
||||
[Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||
internal static class HttpClientExtensions
|
||||
{
|
||||
private static readonly HttpMethod _httpMethodPatch = new("PATCH");
|
||||
|
||||
/// <summary>
|
||||
/// Sends a PATCH request with a cancellation token to a Uri represented as a string as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="client">A <see cref="HttpClient"/> instance.</param>
|
||||
/// <param name="requestUri">The Uri the request is sent to.</param>
|
||||
/// <param name="content">The HTTP request content sent to the server.</param>
|
||||
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
|
||||
/// <returns>The task object representing the asynchronous operation.</returns>
|
||||
public static Task<HttpResponseMessage> PatchAsync(this HttpClient client, string? requestUri, HttpContent? content, CancellationToken cancellationToken) =>
|
||||
client.PatchAsync(CreateUri(requestUri), content, cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Sends a PATCH request with a cancellation token as an asynchronous operation.
|
||||
/// </summary>
|
||||
/// <param name="client">A <see cref="HttpClient"/> instance.</param>
|
||||
/// <param name="requestUri">The Uri the request is sent to.</param>
|
||||
/// <param name="content">The HTTP request content sent to the server.</param>
|
||||
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
|
||||
/// <returns>The task object representing the asynchrnous operation.</returns>
|
||||
public static Task<HttpResponseMessage> PatchAsync(this HttpClient client, Uri? requestUri, HttpContent? content, CancellationToken cancellationToken)
|
||||
{
|
||||
var request = new HttpRequestMessage(_httpMethodPatch, requestUri)
|
||||
{
|
||||
Version = HttpVersion.Version11,
|
||||
Content = content,
|
||||
};
|
||||
return client.SendAsync(request, cancellationToken);
|
||||
}
|
||||
|
||||
private static Uri? CreateUri(string? uri) =>
|
||||
string.IsNullOrEmpty(uri) ? null : new Uri(uri, UriKind.RelativeOrAbsolute);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
78
src/Cloudflare/Extensions/StringExtensions.cs
Normal file
78
src/Cloudflare/Extensions/StringExtensions.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
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 _idCheckRegex = new(@"^[0-9a-f]{32}$", RegexOptions.Compiled);
|
||||
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));
|
||||
|
||||
id.ValidateLength(32, nameof(id));
|
||||
|
||||
if (!_idCheckRegex.IsMatch(id))
|
||||
throw new ArgumentException("Invalid Cloudflare ID", 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));
|
||||
|
||||
name.ValidateLength(253, 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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate the length of a string.
|
||||
/// </summary>
|
||||
/// <param name="str">The string to check.</param>
|
||||
/// <param name="length">The max. length.</param>
|
||||
/// <param name="paramName">The name of the parameter to check.</param>
|
||||
/// <exception cref="ArgumentException">The <paramref name="str"/> is longer than <paramref name="length"/>.</exception>
|
||||
public static void ValidateLength(this string str, int length, string paramName)
|
||||
{
|
||||
if (str?.Length > length)
|
||||
throw new ArgumentException($"The value of '{paramName}' is too long. Only {length} characters are allowed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user