Implemented PATCH as defined in .NET 6.0 for .NET Standard 2.0
This commit is contained in:
@@ -17,9 +17,9 @@ namespace AMWD.Net.Api.Cloudflare
|
||||
/// <summary>
|
||||
/// Implements the Core of the Cloudflare API client.
|
||||
/// </summary>
|
||||
public partial class CloudflareClient : ICloudflareClient, IDisposable
|
||||
public class CloudflareClient : ICloudflareClient, IDisposable
|
||||
{
|
||||
private static readonly JsonSerializerSettings _jsonSerializerSettings = new JsonSerializerSettings
|
||||
private static readonly JsonSerializerSettings _jsonSerializerSettings = new()
|
||||
{
|
||||
Culture = CultureInfo.InvariantCulture,
|
||||
Formatting = Formatting.None,
|
||||
@@ -100,23 +100,9 @@ namespace AMWD.Net.Api.Cloudflare
|
||||
ValidateRequestPath(requestPath);
|
||||
|
||||
string requestUrl = BuildRequestUrl(requestPath, queryFilter);
|
||||
var httpContent = ConvertRequest(request);
|
||||
|
||||
HttpContent httpRequestContent;
|
||||
if (request == null)
|
||||
{
|
||||
httpRequestContent = null;
|
||||
}
|
||||
else if (request is HttpContent httpContent)
|
||||
{
|
||||
httpRequestContent = httpContent;
|
||||
}
|
||||
else
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(request, _jsonSerializerSettings);
|
||||
httpRequestContent = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
}
|
||||
|
||||
var response = await _httpClient.PostAsync(requestUrl, httpRequestContent, cancellationToken).ConfigureAwait(false);
|
||||
var response = await _httpClient.PostAsync(requestUrl, httpContent, cancellationToken).ConfigureAwait(false);
|
||||
return await GetCloudflareResponse<TResponse>(response, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -127,23 +113,9 @@ namespace AMWD.Net.Api.Cloudflare
|
||||
ValidateRequestPath(requestPath);
|
||||
|
||||
string requestUrl = BuildRequestUrl(requestPath);
|
||||
var httpContent = ConvertRequest(request);
|
||||
|
||||
HttpContent httpRequestContent;
|
||||
if (request == null)
|
||||
{
|
||||
httpRequestContent = null;
|
||||
}
|
||||
else if (request is HttpContent httpContent)
|
||||
{
|
||||
httpRequestContent = httpContent;
|
||||
}
|
||||
else
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(request, _jsonSerializerSettings);
|
||||
httpRequestContent = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
}
|
||||
|
||||
var response = await _httpClient.PutAsync(requestUrl, httpRequestContent, cancellationToken).ConfigureAwait(false);
|
||||
var response = await _httpClient.PutAsync(requestUrl, httpContent, cancellationToken).ConfigureAwait(false);
|
||||
return await GetCloudflareResponse<TResponse>(response, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -166,31 +138,9 @@ namespace AMWD.Net.Api.Cloudflare
|
||||
ValidateRequestPath(requestPath);
|
||||
|
||||
string requestUrl = BuildRequestUrl(requestPath);
|
||||
var httpContent = ConvertRequest(request);
|
||||
|
||||
HttpContent httpRequestContent;
|
||||
if (request is HttpContent httpContent)
|
||||
{
|
||||
httpRequestContent = httpContent;
|
||||
}
|
||||
else
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(request, _jsonSerializerSettings);
|
||||
httpRequestContent = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
}
|
||||
|
||||
#if NET6_0_OR_GREATER
|
||||
var response = await _httpClient.PatchAsync(requestUrl, httpRequestContent, cancellationToken).ConfigureAwait(false);
|
||||
#else
|
||||
var httpRequestMessage = new HttpRequestMessage
|
||||
{
|
||||
Version = HttpVersion.Version11,
|
||||
Method = new HttpMethod("PATCH"),
|
||||
RequestUri = new Uri(requestUrl),
|
||||
Content = httpRequestContent,
|
||||
};
|
||||
var response = await _httpClient.SendAsync(httpRequestMessage, cancellationToken).ConfigureAwait(false);
|
||||
#endif
|
||||
|
||||
var response = await _httpClient.PatchAsync(requestUrl, httpContent, cancellationToken).ConfigureAwait(false);
|
||||
return await GetCloudflareResponse<TResponse>(response, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -266,24 +216,29 @@ namespace AMWD.Net.Api.Cloudflare
|
||||
return client;
|
||||
}
|
||||
|
||||
private async Task<CloudflareResponse<TRes>> GetCloudflareResponse<TRes>(HttpResponseMessage response, CancellationToken cancellationToken)
|
||||
private static async Task<CloudflareResponse<TRes>> GetCloudflareResponse<TRes>(HttpResponseMessage httpResponse, CancellationToken cancellationToken)
|
||||
{
|
||||
#if NET6_0_OR_GREATER
|
||||
string content = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||||
string content = await httpResponse.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||||
#else
|
||||
string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
string content = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
#endif
|
||||
switch (response.StatusCode)
|
||||
switch (httpResponse.StatusCode)
|
||||
{
|
||||
case HttpStatusCode.Forbidden:
|
||||
case HttpStatusCode.Unauthorized:
|
||||
var errorResponse = JsonConvert.DeserializeObject<CloudflareResponse<object>>(content);
|
||||
var errorResponse = JsonConvert.DeserializeObject<CloudflareResponse<object>>(content, _jsonSerializerSettings)
|
||||
?? throw new CloudflareException("Response is not a valid Cloudflare API response.");
|
||||
|
||||
throw new AuthenticationException(string.Join(Environment.NewLine, errorResponse.Errors.Select(e => $"{e.Code}: {e.Message}")));
|
||||
|
||||
default:
|
||||
try
|
||||
{
|
||||
return JsonConvert.DeserializeObject<CloudflareResponse<TRes>>(content);
|
||||
var response = JsonConvert.DeserializeObject<CloudflareResponse<TRes>>(content)
|
||||
?? throw new CloudflareException("Response is not a valid Cloudflare API response.");
|
||||
|
||||
return response;
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -328,5 +283,17 @@ namespace AMWD.Net.Api.Cloudflare
|
||||
|
||||
return $"{requestPath}?{query}";
|
||||
}
|
||||
|
||||
private static HttpContent ConvertRequest<T>(T request)
|
||||
{
|
||||
if (request == null)
|
||||
return null;
|
||||
|
||||
if (request is HttpContent httpContent)
|
||||
return httpContent;
|
||||
|
||||
string json = JsonConvert.SerializeObject(request, _jsonSerializerSettings);
|
||||
return new StringContent(json, Encoding.UTF8, "application/json");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
53
Cloudflare/Extensions/HttpClientExtensions.cs
Normal file
53
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
|
||||
@@ -35,7 +35,7 @@ namespace AMWD.Net.Api.Cloudflare
|
||||
/// <summary>
|
||||
/// The base Cloudflare response with a result.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <typeparam name="T">The result type.</typeparam>
|
||||
public class CloudflareResponse<T> : CloudflareResponse
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user