diff --git a/src/TinyPNG/AmazonS3Configuration.cs b/src/TinyPNG/AmazonS3Configuration.cs index d402e54..37e64a6 100644 --- a/src/TinyPNG/AmazonS3Configuration.cs +++ b/src/TinyPNG/AmazonS3Configuration.cs @@ -2,29 +2,21 @@ namespace TinyPng { - public class AmazonS3Configuration + public class AmazonS3Configuration(string awsAccessKeyId, + string awsSecretAccessKey, + string defaultBucket, + string defaultRegion) { [JsonPropertyName("service")] public const string Service = "s3"; - public AmazonS3Configuration(string awsAccessKeyId, - string awsSecretAccessKey, - string defaultBucket, - string defaultRegion) - { - AwsAccessKeyId = awsAccessKeyId; - AwsSecretAccessKey = awsSecretAccessKey; - Bucket = defaultBucket; - Region = defaultRegion; - } - [JsonPropertyName("aws_access_key_id")] - public string AwsAccessKeyId { get; } + public string AwsAccessKeyId { get; } = awsAccessKeyId; [JsonPropertyName("aws_secret_access_key")] - public string AwsSecretAccessKey { get; } - public string Region { get; set; } + public string AwsSecretAccessKey { get; } = awsSecretAccessKey; + public string Region { get; set; } = defaultRegion; [JsonIgnore] - public string Bucket { get; set; } + public string Bucket { get; set; } = defaultBucket; [JsonIgnore] public string Path { get; set; } diff --git a/src/TinyPNG/CustomJsonStringEnumConverter.cs b/src/TinyPNG/CustomJsonStringEnumConverter.cs index b533a00..4015d8c 100644 --- a/src/TinyPNG/CustomJsonStringEnumConverter.cs +++ b/src/TinyPNG/CustomJsonStringEnumConverter.cs @@ -12,22 +12,13 @@ namespace TinyPng /// Pinched from https://stackoverflow.com/questions/59059989/system-text-json-how-do-i-specify-a-custom-name-for-an-enum-value /// This is because System.Text.Json doesn't support EnumMemberAttribute or a way to customise what an Enum value is serialised as. /// - internal class CustomJsonStringEnumConverter : JsonConverterFactory + internal class CustomJsonStringEnumConverter(JsonNamingPolicy namingPolicy = null, bool allowIntegerValues = true) : JsonConverterFactory { - private readonly JsonNamingPolicy namingPolicy; - private readonly bool allowIntegerValues; - private readonly JsonStringEnumConverter baseConverter; + private readonly JsonStringEnumConverter _baseConverter = new(namingPolicy, allowIntegerValues); public CustomJsonStringEnumConverter() : this(null, true) { } - public CustomJsonStringEnumConverter(JsonNamingPolicy namingPolicy = null, bool allowIntegerValues = true) - { - this.namingPolicy = namingPolicy; - this.allowIntegerValues = allowIntegerValues; - this.baseConverter = new JsonStringEnumConverter(namingPolicy, allowIntegerValues); - } - - public override bool CanConvert(Type typeToConvert) => baseConverter.CanConvert(typeToConvert); + public override bool CanConvert(Type typeToConvert) => _baseConverter.CanConvert(typeToConvert); public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) { @@ -42,26 +33,20 @@ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializer } else { - return baseConverter.CreateConverter(typeToConvert, options); + return _baseConverter.CreateConverter(typeToConvert, options); } } } - public class JsonNamingPolicyDecorator : JsonNamingPolicy + public class JsonNamingPolicyDecorator(JsonNamingPolicy underlyingNamingPolicy) : JsonNamingPolicy { - readonly JsonNamingPolicy underlyingNamingPolicy; - - public JsonNamingPolicyDecorator(JsonNamingPolicy underlyingNamingPolicy) => this.underlyingNamingPolicy = underlyingNamingPolicy; - public override string ConvertName(string name) => underlyingNamingPolicy == null ? name : underlyingNamingPolicy.ConvertName(name); } - internal class DictionaryLookupNamingPolicy : JsonNamingPolicyDecorator + internal class DictionaryLookupNamingPolicy(Dictionary dictionary, JsonNamingPolicy underlyingNamingPolicy) : JsonNamingPolicyDecorator(underlyingNamingPolicy) { - readonly Dictionary dictionary; - - public DictionaryLookupNamingPolicy(Dictionary dictionary, JsonNamingPolicy underlyingNamingPolicy) : base(underlyingNamingPolicy) => this.dictionary = dictionary ?? throw new ArgumentNullException(); + readonly Dictionary _dictionary = dictionary ?? throw new ArgumentNullException(); - public override string ConvertName(string name) => dictionary.TryGetValue(name, out var value) ? value : base.ConvertName(name); + public override string ConvertName(string name) => _dictionary.TryGetValue(name, out var value) ? value : base.ConvertName(name); } } diff --git a/src/TinyPNG/Extensions/ConvertExtensions.cs b/src/TinyPNG/Extensions/ConvertExtensions.cs index 0f9eed0..9cd5023 100644 --- a/src/TinyPNG/Extensions/ConvertExtensions.cs +++ b/src/TinyPNG/Extensions/ConvertExtensions.cs @@ -33,20 +33,20 @@ public static async Task Convert(this Task(await response.Content.ReadAsStreamAsync(), TinyPngClient.JsonOptions); + ApiErrorResponse errorMsg = await JsonSerializer.DeserializeAsync(await response.Content.ReadAsStreamAsync(), TinyPngClient._jsonOptions); throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message); diff --git a/src/TinyPNG/Extensions/DownloadExtensions.cs b/src/TinyPNG/Extensions/DownloadExtensions.cs index 2829723..452782a 100644 --- a/src/TinyPNG/Extensions/DownloadExtensions.cs +++ b/src/TinyPNG/Extensions/DownloadExtensions.cs @@ -9,7 +9,7 @@ namespace TinyPng; public static class DownloadExtensions { - private const string JpegType = "image/jpeg"; + private const string _jpegType = "image/jpeg"; /// /// Downloads the result of a TinyPng Compression operation @@ -44,7 +44,7 @@ public static async Task Download(this TinyPngCompressResp Content = CreateContent(metadata, compressResponse.Output.Type) }; - var response = await compressResponse.HttpClient.SendAsync(msg).ConfigureAwait(false); + var response = await compressResponse._httpClient.SendAsync(msg).ConfigureAwait(false); if (response.IsSuccessStatusCode) { @@ -68,15 +68,15 @@ private static HttpContent CreateContent(PreserveMetadata metadata, string type) } if (metadata.HasFlag(PreserveMetadata.Creation)) { - if (type != JpegType) - throw new InvalidOperationException($"Creation metadata can only be preserved with type {JpegType}"); + if (type != _jpegType) + throw new InvalidOperationException($"Creation metadata can only be preserved with type {_jpegType}"); preserve.Add("creation"); } if (metadata.HasFlag(PreserveMetadata.Location)) { - if (type != JpegType) - throw new InvalidOperationException($"Location metadata can only be preserved with type {JpegType}"); + if (type != _jpegType) + throw new InvalidOperationException($"Location metadata can only be preserved with type {_jpegType}"); preserve.Add("location"); } diff --git a/src/TinyPNG/Extensions/ResizeExtensions.cs b/src/TinyPNG/Extensions/ResizeExtensions.cs index baa0128..099ee42 100644 --- a/src/TinyPNG/Extensions/ResizeExtensions.cs +++ b/src/TinyPNG/Extensions/ResizeExtensions.cs @@ -29,20 +29,20 @@ public static async Task Resize(this Task(await response.Content.ReadAsStreamAsync(), TinyPngClient.JsonOptions); + ApiErrorResponse errorMsg = await JsonSerializer.DeserializeAsync(await response.Content.ReadAsStreamAsync(), TinyPngClient._jsonOptions); throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message); } diff --git a/src/TinyPNG/ResizeOperations/FitResizeOperation.cs b/src/TinyPNG/ResizeOperations/FitResizeOperation.cs index d8f25df..892e1e5 100644 --- a/src/TinyPNG/ResizeOperations/FitResizeOperation.cs +++ b/src/TinyPNG/ResizeOperations/FitResizeOperation.cs @@ -1,6 +1,5 @@ namespace TinyPng.ResizeOperations; -public class FitResizeOperation : ResizeOperation +public class FitResizeOperation(int width, int height) : ResizeOperation(ResizeType.Fit, width, height) { - public FitResizeOperation(int width, int height) : base(ResizeType.Fit, width, height) { } } diff --git a/src/TinyPNG/ResizeOperations/ScaleHeightResizeOperation.cs b/src/TinyPNG/ResizeOperations/ScaleHeightResizeOperation.cs index 44c932e..b964070 100644 --- a/src/TinyPNG/ResizeOperations/ScaleHeightResizeOperation.cs +++ b/src/TinyPNG/ResizeOperations/ScaleHeightResizeOperation.cs @@ -1,6 +1,5 @@ namespace TinyPng.ResizeOperations; -public class ScaleHeightResizeOperation : ResizeOperation +public class ScaleHeightResizeOperation(int height) : ResizeOperation(ResizeType.Scale, null, height) { - public ScaleHeightResizeOperation(int height) : base(ResizeType.Scale, null, height) { } } diff --git a/src/TinyPNG/ResizeOperations/ScaleWidthResizeOperation.cs b/src/TinyPNG/ResizeOperations/ScaleWidthResizeOperation.cs index 823882b..8e138a8 100644 --- a/src/TinyPNG/ResizeOperations/ScaleWidthResizeOperation.cs +++ b/src/TinyPNG/ResizeOperations/ScaleWidthResizeOperation.cs @@ -1,6 +1,5 @@ namespace TinyPng.ResizeOperations; -public class ScaleWidthResizeOperation : ResizeOperation +public class ScaleWidthResizeOperation(int width) : ResizeOperation(ResizeType.Scale, width, null) { - public ScaleWidthResizeOperation(int width) : base(ResizeType.Scale, width, null) { } } diff --git a/src/TinyPNG/Responses/TinyPngCompressResponse.cs b/src/TinyPNG/Responses/TinyPngCompressResponse.cs index 5c5dc44..4b03a82 100644 --- a/src/TinyPNG/Responses/TinyPngCompressResponse.cs +++ b/src/TinyPNG/Responses/TinyPngCompressResponse.cs @@ -10,11 +10,11 @@ public class TinyPngCompressResponse : TinyPngResponse public TinyPngApiOutput Output { get; private set; } public TinyPngApiResult ApiResult { get; private set; } - internal readonly HttpClient HttpClient; + internal readonly HttpClient _httpClient; public TinyPngCompressResponse(HttpResponseMessage msg, HttpClient httpClient) : base(msg) { - HttpClient = httpClient; + _httpClient = httpClient; //this is a cute trick to handle async in a ctor and avoid deadlocks ApiResult = Task.Run(() => Deserialize(msg)).GetAwaiter().GetResult(); @@ -24,6 +24,6 @@ public TinyPngCompressResponse(HttpResponseMessage msg, HttpClient httpClient) : } private async Task Deserialize(HttpResponseMessage response) { - return await JsonSerializer.DeserializeAsync(await response.Content.ReadAsStreamAsync(), TinyPngClient.JsonOptions); + return await JsonSerializer.DeserializeAsync(await response.Content.ReadAsStreamAsync(), TinyPngClient._jsonOptions); } } diff --git a/src/TinyPNG/Responses/TinyPngConvertResponse.cs b/src/TinyPNG/Responses/TinyPngConvertResponse.cs index 7a255f4..6ea9d9f 100644 --- a/src/TinyPNG/Responses/TinyPngConvertResponse.cs +++ b/src/TinyPNG/Responses/TinyPngConvertResponse.cs @@ -2,12 +2,8 @@ using System.Net.Http; namespace TinyPng.Responses; -public class TinyPngConvertResponse : TinyPngImageResponse +public class TinyPngConvertResponse(HttpResponseMessage msg) : TinyPngImageResponse(msg) { - public TinyPngConvertResponse(HttpResponseMessage msg) : base(msg) - { - } - public string ContentType => HttpResponseMessage.Content.Headers.ContentType.MediaType; public string ImageHeight => HttpResponseMessage.Content.Headers.GetValues("Image-Height").FirstOrDefault(); public string ImageWidth => HttpResponseMessage.Content.Headers.GetValues("Image-Width").FirstOrDefault(); diff --git a/src/TinyPNG/Responses/TinyPngImageResponse.cs b/src/TinyPNG/Responses/TinyPngImageResponse.cs index cc340fc..892bde4 100644 --- a/src/TinyPNG/Responses/TinyPngImageResponse.cs +++ b/src/TinyPNG/Responses/TinyPngImageResponse.cs @@ -5,9 +5,6 @@ namespace TinyPng.Responses; /// /// This is a response which contains actual image data /// -public class TinyPngImageResponse : TinyPngResponse +public class TinyPngImageResponse(HttpResponseMessage msg) : TinyPngResponse(msg) { - public TinyPngImageResponse(HttpResponseMessage msg) : base(msg) - { - } } diff --git a/src/TinyPNG/Responses/TinyPngResizeResponse.cs b/src/TinyPNG/Responses/TinyPngResizeResponse.cs index cd1828d..a09fa51 100644 --- a/src/TinyPNG/Responses/TinyPngResizeResponse.cs +++ b/src/TinyPNG/Responses/TinyPngResizeResponse.cs @@ -2,10 +2,6 @@ namespace TinyPng.Responses; -public class TinyPngResizeResponse : TinyPngImageResponse +public class TinyPngResizeResponse(HttpResponseMessage msg) : TinyPngImageResponse(msg) { - public TinyPngResizeResponse(HttpResponseMessage msg) : base(msg) - { - - } } diff --git a/src/TinyPNG/Responses/TinyPngResponse.cs b/src/TinyPNG/Responses/TinyPngResponse.cs index c6a600e..4151057 100644 --- a/src/TinyPNG/Responses/TinyPngResponse.cs +++ b/src/TinyPNG/Responses/TinyPngResponse.cs @@ -8,17 +8,16 @@ public class TinyPngResponse { internal HttpResponseMessage HttpResponseMessage { get; } - private readonly int compressionCount; - - public int CompressionCount => compressionCount; + private readonly int _compressionCount; + public int CompressionCount => _compressionCount; protected TinyPngResponse(HttpResponseMessage msg) { if (msg.Headers.TryGetValues("Compression-Count", out IEnumerable compressionCountHeaders)) { - int.TryParse(compressionCountHeaders.First(), out compressionCount); + int.TryParse(compressionCountHeaders.First(), out _compressionCount); } HttpResponseMessage = msg; } diff --git a/src/TinyPNG/TinyPNG.csproj b/src/TinyPNG/TinyPNG.csproj index f766b09..3186074 100644 --- a/src/TinyPNG/TinyPNG.csproj +++ b/src/TinyPNG/TinyPNG.csproj @@ -42,8 +42,7 @@ - - + diff --git a/src/TinyPNG/TinyPngClient.cs b/src/TinyPNG/TinyPngClient.cs index 572863a..468b5a6 100644 --- a/src/TinyPNG/TinyPngClient.cs +++ b/src/TinyPNG/TinyPngClient.cs @@ -15,10 +15,10 @@ namespace TinyPng; public class TinyPngClient { - private const string ApiEndpoint = "https://api.tinify.com/shrink"; + private const string _apiEndpoint = "https://api.tinify.com/shrink"; - private readonly HttpClient HttpClient; - internal static readonly JsonSerializerOptions JsonOptions; + private readonly HttpClient _httpClient; + internal static readonly JsonSerializerOptions _jsonOptions; /// /// Configures the client to use these AmazonS3 settings when storing images in S3 @@ -28,14 +28,14 @@ public class TinyPngClient static TinyPngClient() { //configure json settings for camelCase. - JsonOptions = new JsonSerializerOptions + _jsonOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }; - JsonOptions.Converters.Add(new CustomJsonStringEnumConverter(JsonNamingPolicy.CamelCase)); + _jsonOptions.Converters.Add(new CustomJsonStringEnumConverter(JsonNamingPolicy.CamelCase)); } @@ -49,7 +49,7 @@ public TinyPngClient(string apiKey, HttpClient httpClient = null) if (string.IsNullOrEmpty(apiKey)) throw new ArgumentNullException(nameof(apiKey)); - HttpClient = httpClient ?? new HttpClient(); + _httpClient = httpClient ?? new HttpClient(); ConfigureHttpClient(apiKey); } @@ -62,7 +62,7 @@ private void ConfigureHttpClient(string apiKey) var apiKeyEncoded = Convert.ToBase64String(authByteArray); //add auth to the default outgoing headers. - HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("basic", apiKeyEncoded); + _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("basic", apiKeyEncoded); } /// @@ -133,16 +133,16 @@ public Task Compress(Uri url) return CompressInternal(CreateContent(url)); static HttpContent CreateContent(Uri source) => new StringContent( - JsonSerializer.Serialize(new { source = new { url = source } }, JsonOptions), + JsonSerializer.Serialize(new { source = new { url = source } }, _jsonOptions), Encoding.UTF8, "application/json"); } private async Task CompressInternal(HttpContent contentData) { - var response = await HttpClient.PostAsync(ApiEndpoint, contentData).ConfigureAwait(false); + var response = await _httpClient.PostAsync(_apiEndpoint, contentData).ConfigureAwait(false); if (response.IsSuccessStatusCode) - return new TinyPngCompressResponse(response, HttpClient); + return new TinyPngCompressResponse(response, _httpClient); var errorMsg = await JsonSerializer.DeserializeAsync(await response.Content.ReadAsStreamAsync().ConfigureAwait(false)); throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message); @@ -166,13 +166,13 @@ public async Task SaveCompressedImageToAmazonS3(TinyPngCompressResponse res amazonSettings.Path = path; - var amazonSettingsAsJson = JsonSerializer.Serialize(new { store = amazonSettings }, JsonOptions); + var amazonSettingsAsJson = JsonSerializer.Serialize(new { store = amazonSettings }, _jsonOptions); var msg = new HttpRequestMessage(HttpMethod.Post, result.Output.Url) { Content = new StringContent(amazonSettingsAsJson, System.Text.Encoding.UTF8, "application/json") }; - var response = await HttpClient.SendAsync(msg).ConfigureAwait(false); + var response = await _httpClient.SendAsync(msg).ConfigureAwait(false); if (response.IsSuccessStatusCode) { diff --git a/tests/TinyPng.Tests/Extensions.cs b/tests/TinyPng.Tests/Extensions.cs index 6868533..7a5d265 100644 --- a/tests/TinyPng.Tests/Extensions.cs +++ b/tests/TinyPng.Tests/Extensions.cs @@ -53,7 +53,7 @@ public static FakeResponseHandler CompressAndFail(this FakeResponseHandler fakeR public static FakeResponseHandler Download(this FakeResponseHandler fakeResponse) { - FileStream compressedCatStream = File.OpenRead(TinyPngTests.CompressedCat); + FileStream compressedCatStream = File.OpenRead(TinyPngTests._compressedCat); HttpResponseMessage outputResponseMessage = new() { Content = new StreamContent(compressedCatStream), @@ -78,7 +78,7 @@ public static FakeResponseHandler DownloadAndFail(this FakeResponseHandler fakeR public static FakeResponseHandler Resize(this FakeResponseHandler fakeResponse) { - FileStream resizedCatStream = File.OpenRead(TinyPngTests.ResizedCat); + FileStream resizedCatStream = File.OpenRead(TinyPngTests._resizedCat); HttpResponseMessage resizeMessage = new() { StatusCode = System.Net.HttpStatusCode.OK, diff --git a/tests/TinyPng.Tests/FakeResponseHandler.cs b/tests/TinyPng.Tests/FakeResponseHandler.cs index 7a458d6..dd91fb2 100644 --- a/tests/TinyPng.Tests/FakeResponseHandler.cs +++ b/tests/TinyPng.Tests/FakeResponseHandler.cs @@ -8,25 +8,25 @@ namespace TinyPng.Tests; public class FakeResponseHandler : DelegatingHandler { - private readonly Dictionary _FakeGetResponses = new(); - private readonly Dictionary _FakePostResponses = new(); + private readonly Dictionary _fakeGetResponses = new(); + private readonly Dictionary _fakePostResponses = new(); public void AddFakeGetResponse(Uri uri, HttpResponseMessage responseMessage) { - _FakeGetResponses.Add(uri, responseMessage); + _fakeGetResponses.Add(uri, responseMessage); } public void AddFakePostResponse(Uri uri, HttpResponseMessage responseMessage) { - _FakePostResponses.Add(uri, responseMessage); + _fakePostResponses.Add(uri, responseMessage); } protected override Task SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) { - var result = request.Method == HttpMethod.Get && _FakeGetResponses.ContainsKey(request.RequestUri) - ? _FakeGetResponses[request.RequestUri] - : request.Method == HttpMethod.Post && _FakePostResponses.ContainsKey(request.RequestUri) - ? _FakePostResponses[request.RequestUri] + var result = request.Method == HttpMethod.Get && _fakeGetResponses.ContainsKey(request.RequestUri) + ? _fakeGetResponses[request.RequestUri] + : request.Method == HttpMethod.Post && _fakePostResponses.ContainsKey(request.RequestUri) + ? _fakePostResponses[request.RequestUri] : new HttpResponseMessage(HttpStatusCode.NotFound) { RequestMessage = request }; return Task.FromResult(result); diff --git a/tests/TinyPng.Tests/TinyPng.Tests.csproj b/tests/TinyPng.Tests/TinyPng.Tests.csproj index 18b0c64..02a37f5 100644 --- a/tests/TinyPng.Tests/TinyPng.Tests.csproj +++ b/tests/TinyPng.Tests/TinyPng.Tests.csproj @@ -25,13 +25,13 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/tests/TinyPng.Tests/TinyPngTests.cs b/tests/TinyPng.Tests/TinyPngTests.cs index 48f410b..1bbf292 100644 --- a/tests/TinyPng.Tests/TinyPngTests.cs +++ b/tests/TinyPng.Tests/TinyPngTests.cs @@ -9,12 +9,12 @@ namespace TinyPng.Tests; public class TinyPngTests { - private const string apiKey = "lolwat"; + private const string _apiKey = "lolwat"; - internal const string Cat = "Resources/cat.jpg"; - internal const string CompressedCat = "Resources/compressedcat.jpg"; - internal const string ResizedCat = "Resources/resizedcat.jpg"; - internal const string SavedCatPath = "Resources/savedcat.jpg"; + internal const string _cat = "Resources/cat.jpg"; + internal const string _compressedCat = "Resources/compressedcat.jpg"; + internal const string _resizedCat = "Resources/resizedcat.jpg"; + internal const string _savedCatPath = "Resources/savedcat.jpg"; [Fact] public void TinyPngClientThrowsWhenNoApiKeySupplied() @@ -45,9 +45,9 @@ public void HandleScenarioOfExistingAuthHeaderOnTheClient() [Fact] public async Task Compression() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress())); - Responses.TinyPngCompressResponse result = await pngx.Compress(Cat); + Responses.TinyPngCompressResponse result = await pngx.Compress(_cat); Assert.Equal("image/jpeg", result.Input.Type); Assert.Equal(400, result.Output.Width); @@ -57,18 +57,18 @@ public async Task Compression() [Fact] public async Task CanBeCalledMultipleTimesWithoutExploding() { - TinyPngClient pngx1 = new(apiKey, new HttpClient(new FakeResponseHandler().Compress())); + TinyPngClient pngx1 = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress())); - Responses.TinyPngCompressResponse result1 = await pngx1.Compress(Cat); + Responses.TinyPngCompressResponse result1 = await pngx1.Compress(_cat); Assert.Equal("image/jpeg", result1.Input.Type); Assert.Equal(400, result1.Output.Width); Assert.Equal(400, result1.Output.Height); - TinyPngClient pngx2 = new(apiKey, new HttpClient(new FakeResponseHandler().Compress())); + TinyPngClient pngx2 = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress())); - Responses.TinyPngCompressResponse result2 = await pngx2.Compress(Cat); + Responses.TinyPngCompressResponse result2 = await pngx2.Compress(_cat); Assert.Equal("image/jpeg", result2.Input.Type); Assert.Equal(400, result2.Output.Width); @@ -78,9 +78,9 @@ public async Task CanBeCalledMultipleTimesWithoutExploding() [Fact] public async Task CompressionCount() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress())); - Responses.TinyPngCompressResponse result = await pngx.Compress(Cat); + Responses.TinyPngCompressResponse result = await pngx.Compress(_cat); Assert.Equal(99, result.CompressionCount); } @@ -88,9 +88,9 @@ public async Task CompressionCount() [Fact] public async Task CompressionWithBytes() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress())); - byte[] bytes = await File.ReadAllBytesAsync(Cat); + byte[] bytes = await File.ReadAllBytesAsync(_cat); Responses.TinyPngCompressResponse result = await pngx.Compress(bytes); @@ -102,9 +102,9 @@ public async Task CompressionWithBytes() [Fact] public async Task CompressionWithStreams() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress())); - await using FileStream fileStream = File.OpenRead(Cat); + await using FileStream fileStream = File.OpenRead(_cat); Responses.TinyPngCompressResponse result = await pngx.Compress(fileStream); @@ -116,7 +116,7 @@ public async Task CompressionWithStreams() [Fact] public async Task CompressionFromUrl() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress())); Responses.TinyPngCompressResponse result = await pngx.Compress(new Uri("https://sample.com/image.jpg")); @@ -126,35 +126,27 @@ public async Task CompressionFromUrl() } [Fact] - public void CompressionShouldThrowIfNoPathToFile() + public async Task CompressionShouldThrowIfNoPathToFile() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress())); - _ = Assert.ThrowsAsync(async () => await pngx.Compress(string.Empty)); + await Assert.ThrowsAsync(async () => await pngx.Compress(string.Empty)); } [Fact] - public void CompressionShouldThrowIfNoUrlToFile() + public async Task CompressionShouldThrowIfNoNonSuccessStatusCode() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().CompressAndFail())); - _ = Assert.ThrowsAsync(async () => await pngx.Compress(new Uri(string.Empty))); - } - - [Fact] - public void CompressionShouldThrowIfNoNonSuccessStatusCode() - { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().CompressAndFail())); - - _ = Assert.ThrowsAsync(async () => await pngx.Compress(Cat)); + await Assert.ThrowsAsync(async () => await pngx.Compress(_cat)); } [Fact] public async Task CompressionAndDownload() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().Download())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().Download())); - byte[] downloadResult = await pngx.Compress(Cat) + byte[] downloadResult = await pngx.Compress(_cat) .Download() .GetImageByteData(); @@ -164,9 +156,9 @@ public async Task CompressionAndDownload() [Fact] public async Task CompressionAndDownloadAndGetUnderlyingStream() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().Download())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().Download())); - Stream downloadResult = await pngx.Compress(Cat) + Stream downloadResult = await pngx.Compress(_cat) .Download() .GetImageStreamData(); @@ -176,61 +168,61 @@ public async Task CompressionAndDownloadAndGetUnderlyingStream() [Fact] public async Task CompressionAndDownloadAndWriteToDisk() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().Download())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().Download())); try { - await pngx.Compress(Cat) + await pngx.Compress(_cat) .Download() - .SaveImageToDisk(SavedCatPath); + .SaveImageToDisk(_savedCatPath); } finally { //try cleanup any saved file - File.Delete(SavedCatPath); + File.Delete(_savedCatPath); } } [Fact] - public void ResizingOperationThrows() + public async Task ResizingOperationThrows() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().Resize())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().Resize())); - _ = Assert.ThrowsAsync(async () => await pngx.Compress((string)null).Resize(150, 150)); - _ = Assert.ThrowsAsync(async () => await pngx.Compress((string)null).Resize(null)); - _ = Assert.ThrowsAsync(async () => await pngx.Compress(Cat).Resize(null)); + await Assert.ThrowsAsync(async () => await pngx.Compress((string)null).Resize(150, 150)); + await Assert.ThrowsAsync(async () => await pngx.Compress((string)null).Resize(null)); + await Assert.ThrowsAsync(async () => await pngx.Compress(_cat).Resize(null)); Task nullCompressResponse = null; - _ = Assert.ThrowsAsync(async () => await nullCompressResponse.Resize(150, 150)); + await Assert.ThrowsAsync(async () => await nullCompressResponse.Resize(150, 150)); - _ = Assert.ThrowsAsync(async () => await pngx.Compress(Cat).Resize(0, 150)); - _ = Assert.ThrowsAsync(async () => await pngx.Compress(Cat).Resize(150, 0)); + await Assert.ThrowsAsync(async () => await pngx.Compress(_cat).Resize(0, 150)); + await Assert.ThrowsAsync(async () => await pngx.Compress(_cat).Resize(150, 0)); } [Fact] - public void DownloadingOperationThrows() + public async Task DownloadingOperationThrows() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().Download())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().Download())); - _ = Assert.ThrowsAsync(async () => await pngx.Compress((string)null).Download()); + await Assert.ThrowsAsync(async () => await pngx.Compress((string)null).Download()); Task nullCompressResponse = null; - _ = Assert.ThrowsAsync(async () => await nullCompressResponse.Download()); + await Assert.ThrowsAsync(async () => await nullCompressResponse.Download()); } [Fact] - public void DownloadingOperationThrowsOnNonSuccessStatusCode() + public async Task DownloadingOperationThrowsOnNonSuccessStatusCode() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().DownloadAndFail())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().DownloadAndFail())); - _ = Assert.ThrowsAsync(async () => await pngx.Compress(Cat).Download()); + await Assert.ThrowsAsync(async () => await pngx.Compress(_cat).Download()); } [Fact] public async Task ResizingOperation() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().Resize())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().Resize())); - byte[] resizedImageByteData = await pngx.Compress(Cat).Resize(150, 150).GetImageByteData(); + byte[] resizedImageByteData = await pngx.Compress(_cat).Resize(150, 150).GetImageByteData(); Assert.Equal(5970, resizedImageByteData.Length); } @@ -238,9 +230,9 @@ public async Task ResizingOperation() [Fact] public async Task ResizingScaleHeightOperation() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().Resize())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().Resize())); - byte[] resizedImageByteData = await pngx.Compress(Cat).Resize(new ScaleHeightResizeOperation(150)).GetImageByteData(); + byte[] resizedImageByteData = await pngx.Compress(_cat).Resize(new ScaleHeightResizeOperation(150)).GetImageByteData(); Assert.Equal(5970, resizedImageByteData.Length); } @@ -248,9 +240,9 @@ public async Task ResizingScaleHeightOperation() [Fact] public async Task ResizingScaleWidthOperation() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().Resize())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().Resize())); - byte[] resizedImageByteData = await pngx.Compress(Cat).Resize(new ScaleWidthResizeOperation(150)).GetImageByteData(); + byte[] resizedImageByteData = await pngx.Compress(_cat).Resize(new ScaleWidthResizeOperation(150)).GetImageByteData(); Assert.Equal(5970, resizedImageByteData.Length); } @@ -258,9 +250,9 @@ public async Task ResizingScaleWidthOperation() [Fact] public async Task ResizingFitResizeOperation() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().Resize())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().Resize())); - byte[] resizedImageByteData = await pngx.Compress(Cat).Resize(new FitResizeOperation(150, 150)).GetImageByteData(); + byte[] resizedImageByteData = await pngx.Compress(_cat).Resize(new FitResizeOperation(150, 150)).GetImageByteData(); Assert.Equal(5970, resizedImageByteData.Length); } @@ -268,20 +260,20 @@ public async Task ResizingFitResizeOperation() [Fact] public async Task ResizingCoverResizeOperation() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().Resize())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().Resize())); - byte[] resizedImageByteData = await pngx.Compress(Cat).Resize(new CoverResizeOperation(150, 150)).GetImageByteData(); + byte[] resizedImageByteData = await pngx.Compress(_cat).Resize(new CoverResizeOperation(150, 150)).GetImageByteData(); Assert.Equal(5970, resizedImageByteData.Length); } [Fact] - public void ResizingCoverResizeOperationThrowsWithInvalidParams() + public async Task ResizingCoverResizeOperationThrowsWithInvalidParams() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().Resize())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().Resize())); - _ = Assert.ThrowsAsync(async () => await pngx.Compress(Cat).Resize(new CoverResizeOperation(0, 150))); - _ = Assert.ThrowsAsync(async () => await pngx.Compress(Cat).Resize(new CoverResizeOperation(150, 0))); + await Assert.ThrowsAsync(async () => await pngx.Compress(_cat).Resize(new CoverResizeOperation(0, 150))); + await Assert.ThrowsAsync(async () => await pngx.Compress(_cat).Resize(new CoverResizeOperation(150, 0))); } [Fact] @@ -293,27 +285,27 @@ public void CompressAndStoreToS3ShouldThrowIfNoApiKeyProvided() [Fact] public async Task CompressAndStoreToS3ShouldThrowIfS3HasNotBeenConfigured() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().S3())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().S3())); - Responses.TinyPngCompressResponse result = await pngx.Compress(Cat); + Responses.TinyPngCompressResponse result = await pngx.Compress(_cat); - _ = await Assert.ThrowsAsync(async () => await pngx.SaveCompressedImageToAmazonS3(null, "bucket/path.jpg")); - _ = await Assert.ThrowsAsync(async () => await pngx.SaveCompressedImageToAmazonS3(result, string.Empty)); - _ = await Assert.ThrowsAsync(async () => await pngx.SaveCompressedImageToAmazonS3(result, "bucket/path.jpg")); + await Assert.ThrowsAsync(async () => await pngx.SaveCompressedImageToAmazonS3(null, "bucket/path.jpg")); + await Assert.ThrowsAsync(async () => await pngx.SaveCompressedImageToAmazonS3(result, string.Empty)); + await Assert.ThrowsAsync(async () => await pngx.SaveCompressedImageToAmazonS3(result, "bucket/path.jpg")); } - private const string ApiKey = "lolwat"; - private const string ApiAccessKey = "lolwat"; + private const string _awsAccessKeyId = "lolwat"; + private const string _awsSecretAccessKey = "lolwat"; [Fact] public async Task CompressAndStoreToS3() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().S3())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().S3())); - Responses.TinyPngCompressResponse result = await pngx.Compress(Cat); + Responses.TinyPngCompressResponse result = await pngx.Compress(_cat); string sendToAmazon = (await pngx.SaveCompressedImageToAmazonS3(result, - new AmazonS3Configuration(ApiKey, ApiAccessKey, "tinypng-test-bucket", "ap-southeast-2"), + new AmazonS3Configuration(_awsAccessKeyId, _awsSecretAccessKey, "tinypng-test-bucket", "ap-southeast-2"), "path.jpg")).ToString(); Assert.Equal("https://s3-ap-southeast-2.amazonaws.com/tinypng-test-bucket/path.jpg", sendToAmazon); @@ -322,21 +314,21 @@ public async Task CompressAndStoreToS3() [Fact] public async Task CompressAndStoreToS3FooBar() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().S3AndFail())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().S3AndFail())); - Responses.TinyPngCompressResponse result = await pngx.Compress(Cat); + Responses.TinyPngCompressResponse result = await pngx.Compress(_cat); - _ = await Assert.ThrowsAsync(async () => + await Assert.ThrowsAsync(async () => await pngx.SaveCompressedImageToAmazonS3(result, - new AmazonS3Configuration(ApiKey, ApiAccessKey, "tinypng-test-bucket", "ap-southeast-2"), "path")); + new AmazonS3Configuration(_awsAccessKeyId, _awsSecretAccessKey, "tinypng-test-bucket", "ap-southeast-2"), "path")); } [Fact] public async Task CompressAndStoreToS3Throws() { - TinyPngClient pngx = new(apiKey, new HttpClient(new FakeResponseHandler().Compress().S3())); + TinyPngClient pngx = new(_apiKey, new HttpClient(new FakeResponseHandler().Compress().S3())); - Responses.TinyPngCompressResponse result = await pngx.Compress(Cat); + Responses.TinyPngCompressResponse result = await pngx.Compress(_cat); _ = await Assert.ThrowsAsync(async () => await pngx.SaveCompressedImageToAmazonS3(result, null, string.Empty)); @@ -347,11 +339,11 @@ public async Task CompressAndStoreToS3Throws() [Fact] public async Task CompressAndStoreToS3WithOptionsPassedIntoConstructor() { - TinyPngClient pngx = new(apiKey, - new AmazonS3Configuration(ApiKey, ApiAccessKey, "tinypng-test-bucket", "ap-southeast-2"), + TinyPngClient pngx = new(_apiKey, + new AmazonS3Configuration(_awsAccessKeyId, _awsSecretAccessKey, "tinypng-test-bucket", "ap-southeast-2"), new HttpClient(new FakeResponseHandler().Compress().S3())); - Responses.TinyPngCompressResponse result = await pngx.Compress(Cat); + Responses.TinyPngCompressResponse result = await pngx.Compress(_cat); string sendToAmazon = (await pngx.SaveCompressedImageToAmazonS3(result, "path.jpg")).ToString(); Assert.Equal("https://s3-ap-southeast-2.amazonaws.com/tinypng-test-bucket/path.jpg", sendToAmazon);