diff options
Diffstat (limited to 'src/TinyPNG/TinyPng.cs')
-rw-r--r-- | src/TinyPNG/TinyPng.cs | 106 |
1 files changed, 19 insertions, 87 deletions
diff --git a/src/TinyPNG/TinyPng.cs b/src/TinyPNG/TinyPng.cs index 8f81252..bfd06ef 100644 --- a/src/TinyPNG/TinyPng.cs +++ b/src/TinyPNG/TinyPng.cs @@ -7,7 +7,9 @@ using Newtonsoft.Json; using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using TinyPng.Responses;
+using System.Runtime.CompilerServices;
+[assembly:InternalsVisibleTo("TinyPng.Tests")]
namespace TinyPng
{
public class TinyPngClient : IDisposable
@@ -15,8 +17,8 @@ namespace TinyPng private readonly string _apiKey;
private const string ApiEndpoint = "https://api.tinify.com/shrink";
- public HttpClient httpClient = new HttpClient();
- private readonly JsonSerializerSettings jsonSettings;
+ internal static HttpClient HttpClient = new HttpClient();
+ internal static JsonSerializerSettings JsonSettings;
/// <summary>
/// Wrapper for the tinypng.com API
@@ -33,14 +35,14 @@ namespace TinyPng _apiKey = Convert.ToBase64String(authByteArray);
//add auth to the default outgoing headers.
- httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("basic", _apiKey);
+ HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("basic", _apiKey);
//configure json settings for camelCase.
- jsonSettings = new JsonSerializerSettings
+ JsonSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
- jsonSettings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
+ JsonSettings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
}
@@ -53,11 +55,7 @@ namespace TinyPng {
if (string.IsNullOrEmpty(apiKey))
throw new ArgumentNullException(nameof(apiKey));
-
- if (amazonConfiguration == null)
- throw new ArgumentNullException(nameof(amazonConfiguration));
-
- AmazonS3Configuration = amazonConfiguration;
+ AmazonS3Configuration = amazonConfiguration ?? throw new ArgumentNullException(nameof(amazonConfiguration));
}
/// <summary>
@@ -75,7 +73,7 @@ namespace TinyPng }
/// <summary>
- /// Compress file
+ /// Compress a file on disk
/// </summary>
/// <param name="pathToFile">Path to file on disk</param>
/// <returns>TinyPngApiResult, <see cref="TinyPngApiResult"/></returns>
@@ -107,7 +105,7 @@ namespace TinyPng }
/// <summary>
- /// Compress stream
+ /// Compress a stream
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
@@ -116,27 +114,11 @@ namespace TinyPng if (data == null)
throw new ArgumentNullException(nameof(data));
- var response = await httpClient.PostAsync(ApiEndpoint, CreateContent(data));
+ var response = await HttpClient.PostAsync(ApiEndpoint, CreateContent(data));
if (response.IsSuccessStatusCode)
{
- return new TinyPngCompressResponse(response);
- }
-
- var errorMsg = JsonConvert.DeserializeObject<ApiErrorResponse>(await response.Content.ReadAsStringAsync());
- throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message);
- }
- public async Task<TinyPngImageResponse> Download(TinyPngCompressResponse result)
- {
- if (result == null)
- throw new ArgumentNullException(nameof(result));
-
- var msg = new HttpRequestMessage(HttpMethod.Get, result.Output.Url);
-
- var response = await httpClient.SendAsync(msg);
- if (response.IsSuccessStatusCode)
- {
- return new TinyPngImageResponse(response);
+ return new TinyPngCompressResponse(response, HttpClient);
}
var errorMsg = JsonConvert.DeserializeObject<ApiErrorResponse>(await response.Content.ReadAsStringAsync());
@@ -145,57 +127,6 @@ namespace TinyPng /// <summary>
- /// Uses the TinyPng API to create a resized version of your uploaded image.
- /// </summary>
- /// <param name="result">This is the previous result of running a compression <see cref="Compress(string)"/></param>
- /// <param name="resizeOperation">Supply a strongly typed Resize Operation. See <typeparamref name="CoverResizeOperation"/>, <typeparamref name="FitResizeOperation"/>, <typeparamref name="ScaleHeightResizeOperation"/>, <typeparamref name="ScaleWidthResizeOperation"/></param>
- /// <returns></returns>
- public async Task<TinyPngResizeResponse> Resize(TinyPngCompressResponse result, ResizeOperation resizeOperation)
- {
- if (result == null)
- throw new ArgumentNullException(nameof(result));
- if (resizeOperation == null)
- throw new ArgumentNullException(nameof(resizeOperation));
-
- var requestBody = JsonConvert.SerializeObject(new { resize = resizeOperation }, jsonSettings);
-
- var msg = new HttpRequestMessage(HttpMethod.Post, result.Output.Url);
- msg.Content = new StringContent(requestBody, System.Text.Encoding.UTF8, "application/json");
-
- var response = await httpClient.SendAsync(msg);
- if (response.IsSuccessStatusCode)
- {
- return new TinyPngResizeResponse(response);
- }
-
- var errorMsg = JsonConvert.DeserializeObject<ApiErrorResponse>(await response.Content.ReadAsStringAsync());
- throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message);
- }
-
- /// <summary>
- /// Uses the TinyPng API to create a resized version of your uploaded image.
- /// </summary>
- /// <param name="result">This is the previous result of running a compression <see cref="Compress(string)"/></param>
- /// <param name="width"></param>
- /// <param name="height"></param>
- /// <param name="resizeType"></param>
- /// <returns></returns>
- public async Task<TinyPngResizeResponse> Resize(TinyPngCompressResponse result, int width, int height, ResizeType resizeType = ResizeType.Fit)
- {
- if (result == null)
- throw new ArgumentNullException(nameof(result));
- if (width == 0)
- throw new ArgumentOutOfRangeException(nameof(width));
- if (height == 0)
- throw new ArgumentOutOfRangeException(nameof(height));
-
- var resizeOp = new ResizeOperation(resizeType, width, height);
-
- return await Resize(result, height, width, resizeType);
- }
-
-
- /// <summary>
/// Stores a previously compressed image directly into Amazon S3 storage
/// </summary>
/// <param name="result">The previously compressed image</param>
@@ -213,12 +144,13 @@ namespace TinyPng amazonSettings.Path = path;
- var amazonSettingsAsJson = JsonConvert.SerializeObject(new { store = amazonSettings }, jsonSettings);
-
- var msg = new HttpRequestMessage(HttpMethod.Post, result.Output.Url);
- msg.Content = new StringContent(amazonSettingsAsJson, System.Text.Encoding.UTF8, "application/json");
+ var amazonSettingsAsJson = JsonConvert.SerializeObject(new { store = amazonSettings }, JsonSettings);
- var response = await httpClient.SendAsync(msg);
+ 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);
if (response.IsSuccessStatusCode)
{
@@ -269,7 +201,7 @@ namespace TinyPng {
if (disposing)
{
- httpClient?.Dispose();
+ HttpClient?.Dispose();
}
}
#endregion
|