diff --git a/Directory.Build.props b/Directory.Build.props index 1c30f9e..c25f525 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 0.7.0 + 0.8.0 Code Happy, LLC Code Happy, LLC HTML/CSS To Image API diff --git a/README.md b/README.md index c5cbbb2..8a6ba76 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ var html_request = new CreateHtmlCssImageRequest() ViewportHeight = 200 }; -var html_image = await client.CreateImageAsync(html_request); +using var html_image = await client.CreateImageAsync(html_request); if(html_image.Success) { @@ -58,4 +58,4 @@ Check out the package READMEs for more details on integrating: > Check out the [HTML/CSS To Image Docs](https://docs.htmlcsstoimage.com) for more details on the API's capabilities. > [!TIP] -> Get started for free at [htmlcsstoimage.com](https://htmlcsstoimage.com). \ No newline at end of file +> Get started for free at [htmlcsstoimage.com](https://htmlcsstoimage.com). diff --git a/src/HtmlCssToImage/Client/HtmlCssToImageClient.Images.cs b/src/HtmlCssToImage/Client/HtmlCssToImageClient.Images.cs index dff4513..5d356cc 100644 --- a/src/HtmlCssToImage/Client/HtmlCssToImageClient.Images.cs +++ b/src/HtmlCssToImage/Client/HtmlCssToImageClient.Images.cs @@ -8,6 +8,57 @@ namespace HtmlCssToImage; public partial class HtmlCssToImageClient { + /// + public async Task> DeleteImageAsync(string imageId, CancellationToken cancellationToken = default) + { + var response = await _client.DeleteAsync( + $"{CREATE_OR_GET_PATH}/{Uri.EscapeDataString(imageId)}", + cancellationToken).ConfigureAwait(false); + + return await CreateDeleteImageResultAsync(response, cancellationToken).ConfigureAwait(false); + } + + /// + public async Task> DeleteImageBatchAsync(IEnumerable imageIds, + CancellationToken cancellationToken = default) + { + var request = new DeleteImageBatchRequest + { + Ids = imageIds.ToArray() + }; + var request_message = new HttpRequestMessage(HttpMethod.Delete, BATCH_URL) + { + Content = JsonContent.Create(request, JsonContext.Default.DeleteImageBatchRequest) + }; + var response = await _client.SendAsync(request_message, cancellationToken).ConfigureAwait(false); + + return await CreateDeleteImageResultAsync(response, cancellationToken).ConfigureAwait(false); + } + + private static async Task> CreateDeleteImageResultAsync(HttpResponseMessage response, + CancellationToken cancellationToken) + { + var result = new ApiResult + { + HttpResponseMessage = response, + StatusCode = (int)response.StatusCode, + Success = response.IsSuccessStatusCode + }; + + if (response.IsSuccessStatusCode) + { + result.Response = true; + } + else + { + result.ErrorDetails = await response.Content.ReadFromJsonAsync( + JsonContext.Default.ErrorDetails, + cancellationToken).ConfigureAwait(false); + } + + return result; + } + /// public async Task> CreateImageBatchAsync(T? defaultOptions, IEnumerable variations, CancellationToken cancellationToken = default) where T : IBatchAllowedImageRequest @@ -27,11 +78,11 @@ public partial class HtmlCssToImageClient HttpResponseMessage response; if (request is CreateImageBatchRequest url_request) { - response = await _client.PostAsJsonAsync(CREATE_BATCH_URL, url_request, JsonContext.Default.CreateImageBatchRequestCreateUrlImageRequest, cancellationToken).ConfigureAwait(false); + response = await _client.PostAsJsonAsync(BATCH_URL, url_request, JsonContext.Default.CreateImageBatchRequestCreateUrlImageRequest, cancellationToken).ConfigureAwait(false); } else if (request is CreateImageBatchRequest html_request) { - response = await _client.PostAsJsonAsync(CREATE_BATCH_URL, html_request, JsonContext.Default.CreateImageBatchRequestCreateHtmlCssImageRequest, cancellationToken).ConfigureAwait(false); + response = await _client.PostAsJsonAsync(BATCH_URL, html_request, JsonContext.Default.CreateImageBatchRequestCreateHtmlCssImageRequest, cancellationToken).ConfigureAwait(false); } else { @@ -100,4 +151,4 @@ await response.Content.ReadFromJsonAsync( return result; } -} \ No newline at end of file +} diff --git a/src/HtmlCssToImage/Client/HtmlCssToImageClient.cs b/src/HtmlCssToImage/Client/HtmlCssToImageClient.cs index a930bad..70c25ae 100644 --- a/src/HtmlCssToImage/Client/HtmlCssToImageClient.cs +++ b/src/HtmlCssToImage/Client/HtmlCssToImageClient.cs @@ -39,7 +39,7 @@ internal HtmlCssToImageClient(HttpClient client, HtmlCssToImageOptions options, internal const string CREATE_OR_GET_PATH = "/v1/image"; private const string CREATE_AND_RENDER_PATH = $"{CREATE_OR_GET_PATH}/create-and-render"; private const string CREATE_URL = $"{CREATE_OR_GET_PATH}?includeId=true"; - private const string CREATE_BATCH_URL = $"{CREATE_OR_GET_PATH}/batch"; + private const string BATCH_URL = $"{CREATE_OR_GET_PATH}/batch"; private const string TEMPLATE_VERSION_QUERY_PARAM = "template_version"; } diff --git a/src/HtmlCssToImage/IHtmlCssToImageClient.cs b/src/HtmlCssToImage/IHtmlCssToImageClient.cs index 03f159b..a474598 100644 --- a/src/HtmlCssToImage/IHtmlCssToImageClient.cs +++ b/src/HtmlCssToImage/IHtmlCssToImageClient.cs @@ -24,6 +24,22 @@ public interface IHtmlCssToImageClient /// An object containing the response with metadata such as the image's URL and ID, as well as the status of the operation. public Task> CreateImageAsync(T request, CancellationToken cancellationToken = default) where T : ICreateImageRequestBase; + /// + /// Deletes an existing image and clears it from the CDN. + /// + /// The ID of the image to delete. + /// A cancellation token that can be used to cancel the operation. + /// An ApiResult<bool?> whose response is when the delete request is accepted. + public Task> DeleteImageAsync(string imageId, CancellationToken cancellationToken = default); + + /// + /// Deletes multiple existing images in one API call and clears them from the CDN. + /// + /// The IDs of the images to delete. + /// A cancellation token that can be used to cancel the operation. + /// An ApiResult<bool?> whose response is when the batch delete request is accepted. + public Task> DeleteImageBatchAsync(IEnumerable imageIds, CancellationToken cancellationToken = default); + /// /// Sends a batch request to create multiple images using the specified parameters. /// diff --git a/src/HtmlCssToImage/JsonContext.cs b/src/HtmlCssToImage/JsonContext.cs index 4d5d97f..b895f0d 100644 --- a/src/HtmlCssToImage/JsonContext.cs +++ b/src/HtmlCssToImage/JsonContext.cs @@ -31,6 +31,7 @@ namespace HtmlCssToImage; /// - /// - /// - (for specific batch image types) +/// - /// - /// - /// - @@ -46,6 +47,7 @@ namespace HtmlCssToImage; [JsonSerializable(typeof(CreateTemplatedImageRequest))] [JsonSerializable(typeof(CreateImageBatchRequest))] [JsonSerializable(typeof(CreateImageBatchRequest))] +[JsonSerializable(typeof(DeleteImageBatchRequest))] [JsonSerializable(typeof(CreateImageBatchResponse))] [JsonSerializable(typeof(CreateImageResponse))] [JsonSerializable(typeof(ErrorDetails))] @@ -53,4 +55,4 @@ namespace HtmlCssToImage; [JsonSerializable(typeof(CreateTemplateRequest))] [JsonSerializable(typeof(CreateTemplateResponse))] [JsonSerializable(typeof(PaginatedResponse