Try your search with a different keyword or use * as a wildcard.
using System.Net;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
using Nop.Core;
namespace Nop.Plugin.Misc.CloudflareImages.Services;
///
/// Represents the HTTP client to request Cloudflare Images
///
public class CloudflareImagesHttpClient
{
#region Fields
private readonly CloudflareImagesSettings _cloudflareImagesSettings;
private readonly HttpClient _httpClient;
#endregion
#region Ctor
public CloudflareImagesHttpClient(CloudflareImagesSettings cloudflareImagesSettings, HttpClient httpClient)
{
_cloudflareImagesSettings = cloudflareImagesSettings;
httpClient.Timeout = TimeSpan.FromSeconds(_cloudflareImagesSettings.RequestTimeout ?? CloudflareImagesDefaults.RequestTimeout);
_httpClient = httpClient;
}
#endregion
#region Utilities
///
/// HTTP request services
///
/// Request URL
/// Data to send
/// Request type. null == HttpMethod.Get
/// The asynchronous task whose result contains response details
private async Task RequestAsync(string apiUri = "", HttpContent data = null, HttpMethod httpMethod = null)
{
httpMethod ??= HttpMethod.Get;
var requestUri = new Uri(string.Format(CloudflareImagesDefaults.BaseApiUrl, _cloudflareImagesSettings.AccountId, apiUri));
var request = new HttpRequestMessage
{
Method = httpMethod,
RequestUri = requestUri
};
request.Headers.TryAddWithoutValidation(HeaderNames.Authorization, $"Bearer {_cloudflareImagesSettings.AccessToken}");
if (httpMethod == HttpMethod.Post)
request.Content = data;
var httpResponse = await _httpClient.SendAsync(request);
var response = await httpResponse.Content.ReadAsStringAsync();
if (httpResponse.StatusCode == HttpStatusCode.OK)
return response;
if (!string.IsNullOrEmpty(response))
throw new NopException(response);
throw new NopException($"{CloudflareImagesDefaults.SystemName} unknown error.");
}
#endregion
#region Methods
///
/// Save a picture thumb
///
/// Content
///
/// A task that represents the asynchronous operation
/// The task result contains the response details
///
public async Task SaveThumbAsync(MultipartFormDataContent dataContent)
{
try
{
var result = await RequestAsync(string.Empty, dataContent, HttpMethod.Post);
return JsonConvert.DeserializeObject(result);
}
catch
{
return null;
}
}
///
/// Delete picture thumb
///
/// Image identifier
/// A task that represents the asynchronous operation
public async Task DeleteThumbAsync(string imageId)
{
try
{
await RequestAsync($"/{imageId}", null, HttpMethod.Delete);
}
catch
{
// ignored
}
}
#endregion
#region Nested classes
public class CloudflareImagesResponse
{
[JsonProperty("errors")]
public List Errors { get; set; } = new();
[JsonProperty("messages")]
public List Messages { get; set; } = new();
[JsonProperty("result")]
public Image Result { get; set; }
///
/// Gets or sets whether the API call was successful
///
[JsonProperty("success")]
public bool Success { get; set; }
}
public class ResponseInfo
{
[JsonProperty("code")]
public int Code { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
[JsonProperty("documentation_url")]
public string DocumentationUrl { get; set; }
}
public class Image
{
///
/// Image unique identifier
///
[JsonProperty("id")]
public string Id { get; set; }
///
/// Image file name
///
[JsonProperty("filename")]
public string FileName { get; set; }
}
#endregion
}