summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChad Tolkien <chad@tolkien.id.au>2017-05-08 21:13:37 +1000
committerChad Tolkien <chad@tolkien.id.au>2017-05-08 21:13:37 +1000
commitd8e74add357556ae9cbd34230e499f8083758193 (patch)
treeef54e97cdb7c1a02afe07e5b2c18e4719895e135
parent873860813db5b60e2bab435eb8e8c95100466cfc (diff)
downloadTinyPNG-d8e74add357556ae9cbd34230e499f8083758193.zip
TinyPNG-d8e74add357556ae9cbd34230e499f8083758193.tar.gz
TinyPNG-d8e74add357556ae9cbd34230e499f8083758193.tar.bz2
configure await all the things
-rw-r--r--src/TinyPNG/TinyPngClient.cs (renamed from src/TinyPNG/TinyPng.cs)435
1 files changed, 215 insertions, 220 deletions
diff --git a/src/TinyPNG/TinyPng.cs b/src/TinyPNG/TinyPngClient.cs
index 927bf20..ffafd57 100644
--- a/src/TinyPNG/TinyPng.cs
+++ b/src/TinyPNG/TinyPngClient.cs
@@ -1,220 +1,215 @@
-using System;
-using System.IO;
-using System.Net.Http;
-using System.Net.Http.Headers;
-using System.Threading.Tasks;
-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
- {
- private const string ApiEndpoint = "https://api.tinify.com/shrink";
-
- internal static HttpClient HttpClient;
- internal static JsonSerializerSettings JsonSettings;
-
- /// <summary>
- /// Wrapper for the tinypng.com API
- /// </summary>
- /// <param name="apiKey">Your tinypng.com API key, signup here: https://tinypng.com/developers </param>
- public TinyPngClient(string apiKey)
- {
- if (string.IsNullOrEmpty(apiKey))
- throw new ArgumentNullException(nameof(apiKey));
-
- ConfigureHttpClient(apiKey);
-
- //configure json settings for camelCase.
- JsonSettings = new JsonSerializerSettings
- {
- ContractResolver = new CamelCasePropertyNamesContractResolver()
- };
- JsonSettings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
-
- }
-
- private static void ConfigureHttpClient(string apiKey)
- {
- //configure basic auth api key formatting.
- var auth = $"api:{apiKey}";
- var authByteArray = System.Text.Encoding.ASCII.GetBytes(auth);
- var apiKeyEncoded = Convert.ToBase64String(authByteArray);
-
- if (HttpClient == null)
- {
- HttpClient = new HttpClient();
- }
-
- //add auth to the default outgoing headers.
- HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("basic", apiKeyEncoded);
- }
-
- /// <summary>
- /// Wrapper for the tinypng.com API
- /// </summary>
- /// <param name="apiKey">Your tinypng.com API key, signup here: https://tinypng.com/developers </param>
- /// <param name="amazonConfiguration">Configures defaults to use for storing images on Amazon S3</param>
- public TinyPngClient(string apiKey, AmazonS3Configuration amazonConfiguration) : this(apiKey)
- {
- if (string.IsNullOrEmpty(apiKey))
- throw new ArgumentNullException(nameof(apiKey));
- AmazonS3Configuration = amazonConfiguration ?? throw new ArgumentNullException(nameof(amazonConfiguration));
- }
-
- /// <summary>
- /// Configures the client to use these AmazonS3 settings when storing images in S3
- /// </summary>
- public AmazonS3Configuration AmazonS3Configuration { get; set; }
-
- private HttpContent CreateContent(byte[] source)
- {
- return new ByteArrayContent(source);
- }
- private HttpContent CreateContent(Stream source)
- {
- return new StreamContent(source);
- }
-
- /// <summary>
- /// Compress a file on disk
- /// </summary>
- /// <param name="pathToFile">Path to file on disk</param>
- /// <returns>TinyPngApiResult, <see cref="TinyPngApiResult"/></returns>
- public async Task<TinyPngCompressResponse> Compress(string pathToFile)
- {
- if (string.IsNullOrEmpty(pathToFile))
- throw new ArgumentNullException(nameof(pathToFile));
-
- using (var file = File.OpenRead(pathToFile))
- {
- return await Compress(file);
- }
- }
-
- /// <summary>
- /// Compress byte array of image
- /// </summary>
- /// <param name="data">Byte array of the data to compress</param>
- /// <returns></returns>
- public async Task<TinyPngCompressResponse> Compress(byte[] data)
- {
- if (data == null)
- throw new ArgumentNullException(nameof(data));
-
- using (var stream = new MemoryStream(data))
- {
- return await Compress(stream);
- }
- }
-
- /// <summary>
- /// Compress a stream
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public async Task<TinyPngCompressResponse> Compress(Stream data)
- {
- if (data == null)
- throw new ArgumentNullException(nameof(data));
-
- var response = await HttpClient.PostAsync(ApiEndpoint, CreateContent(data));
-
- if (response.IsSuccessStatusCode)
- {
- return new TinyPngCompressResponse(response, HttpClient);
- }
-
- var errorMsg = JsonConvert.DeserializeObject<ApiErrorResponse>(await response.Content.ReadAsStringAsync());
- throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message);
- }
-
-
- /// <summary>
- /// Stores a previously compressed image directly into Amazon S3 storage
- /// </summary>
- /// <param name="result">The previously compressed image</param>
- /// <param name="amazonSettings">The settings for the amazon connection</param>
- /// <param name="path">The path and bucket to store in: bucket/file.png format</param>
- /// <returns></returns>
- public async Task<Uri> SaveCompressedImageToAmazonS3(TinyPngCompressResponse result, AmazonS3Configuration amazonSettings, string path)
- {
- if (result == null)
- throw new ArgumentNullException(nameof(result));
- if (amazonSettings == null)
- throw new ArgumentNullException(nameof(amazonSettings));
- if (string.IsNullOrEmpty(path))
- throw new ArgumentNullException(nameof(path));
-
- amazonSettings.Path = path;
-
- var amazonSettingsAsJson = JsonConvert.SerializeObject(new { store = amazonSettings }, JsonSettings);
-
- 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)
- {
- return response.Headers.Location;
- }
-
- var errorMsg = JsonConvert.DeserializeObject<ApiErrorResponse>(await response.Content.ReadAsStringAsync());
- throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message);
- }
-
- /// <summary>
- /// Stores a previously compressed image directly into Amazon S3 storage
- /// </summary>
- /// <param name="result">The previously compressed image</param>
- /// <param name="path">The path to storage the image as</param>
- /// <param name="bucketOverride">Optional: To override the previously configured bucket</param>
- /// <param name="regionOverride">Optional: To override the previously configured region</param>
- /// <returns></returns>
- public async Task<Uri> SaveCompressedImageToAmazonS3(TinyPngCompressResponse result, string path, string bucketOverride = "", string regionOverride = "")
- {
- if (result == null)
- throw new ArgumentNullException(nameof(result));
- if (AmazonS3Configuration == null)
- throw new InvalidOperationException("AmazonS3Configuration has not been configured");
- if (string.IsNullOrEmpty(path))
- throw new ArgumentNullException(nameof(path));
-
- var amazonSettings = AmazonS3Configuration.Clone();
- amazonSettings.Path = path;
-
- if (!string.IsNullOrEmpty(regionOverride))
- amazonSettings.Region = regionOverride;
-
- if (!string.IsNullOrEmpty(bucketOverride))
- amazonSettings.Bucket = bucketOverride;
-
- return await SaveCompressedImageToAmazonS3(result, amazonSettings, path);
- }
-
- #region IDisposable Support
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
-
- protected virtual void Dispose(bool disposing)
- {
- if (disposing)
- {
- HttpClient?.Dispose();
- HttpClient = null;
- }
- }
- #endregion
- }
-
-}
+using System;
+using System.IO;
+using System.Net.Http;
+using System.Net.Http.Headers;
+using System.Threading.Tasks;
+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
+ {
+ private const string ApiEndpoint = "https://api.tinify.com/shrink";
+
+ internal static HttpClient HttpClient;
+ internal static JsonSerializerSettings JsonSettings;
+
+ /// <summary>
+ /// Wrapper for the tinypng.com API
+ /// </summary>
+ /// <param name="apiKey">Your tinypng.com API key, signup here: https://tinypng.com/developers </param>
+ public TinyPngClient(string apiKey)
+ {
+ if (string.IsNullOrEmpty(apiKey))
+ throw new ArgumentNullException(nameof(apiKey));
+
+ ConfigureHttpClient(apiKey);
+
+ //configure json settings for camelCase.
+ JsonSettings = new JsonSerializerSettings
+ {
+ ContractResolver = new CamelCasePropertyNamesContractResolver()
+ };
+ JsonSettings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
+ }
+
+ private static void ConfigureHttpClient(string apiKey)
+ {
+ //configure basic auth api key formatting.
+ var auth = $"api:{apiKey}";
+ var authByteArray = System.Text.Encoding.ASCII.GetBytes(auth);
+ var apiKeyEncoded = Convert.ToBase64String(authByteArray);
+
+ HttpClient = HttpClient ?? new HttpClient();
+
+ //add auth to the default outgoing headers.
+ HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("basic", apiKeyEncoded);
+ }
+
+ /// <summary>
+ /// Wrapper for the tinypng.com API
+ /// </summary>
+ /// <param name="apiKey">Your tinypng.com API key, signup here: https://tinypng.com/developers </param>
+ /// <param name="amazonConfiguration">Configures defaults to use for storing images on Amazon S3</param>
+ public TinyPngClient(string apiKey, AmazonS3Configuration amazonConfiguration) : this(apiKey)
+ {
+ if (string.IsNullOrEmpty(apiKey))
+ throw new ArgumentNullException(nameof(apiKey));
+ AmazonS3Configuration = amazonConfiguration ?? throw new ArgumentNullException(nameof(amazonConfiguration));
+ }
+
+ /// <summary>
+ /// Configures the client to use these AmazonS3 settings when storing images in S3
+ /// </summary>
+ public AmazonS3Configuration AmazonS3Configuration { get; set; }
+
+ private HttpContent CreateContent(byte[] source)
+ {
+ return new ByteArrayContent(source);
+ }
+
+ private HttpContent CreateContent(Stream source)
+ {
+ return new StreamContent(source);
+ }
+
+ /// <summary>
+ /// Compress a file on disk
+ /// </summary>
+ /// <param name="pathToFile">Path to file on disk</param>
+ /// <returns>TinyPngApiResult, <see cref="TinyPngApiResult"/></returns>
+ public async Task<TinyPngCompressResponse> Compress(string pathToFile)
+ {
+ if (string.IsNullOrEmpty(pathToFile))
+ throw new ArgumentNullException(nameof(pathToFile));
+
+ using (var file = File.OpenRead(pathToFile))
+ {
+ return await Compress(file).ConfigureAwait(false);
+ }
+ }
+
+ /// <summary>
+ /// Compress byte array of image
+ /// </summary>
+ /// <param name="data">Byte array of the data to compress</param>
+ /// <returns></returns>
+ public async Task<TinyPngCompressResponse> Compress(byte[] data)
+ {
+ if (data == null)
+ throw new ArgumentNullException(nameof(data));
+
+ using (var stream = new MemoryStream(data))
+ {
+ return await Compress(stream).ConfigureAwait(false);
+ }
+ }
+
+ /// <summary>
+ /// Compress a stream
+ /// </summary>
+ /// <param name="data"></param>
+ /// <returns></returns>
+ public async Task<TinyPngCompressResponse> Compress(Stream data)
+ {
+ if (data == null)
+ throw new ArgumentNullException(nameof(data));
+
+ var response = await HttpClient.PostAsync(ApiEndpoint, CreateContent(data)).ConfigureAwait(false);
+
+ if (response.IsSuccessStatusCode)
+ {
+ return new TinyPngCompressResponse(response, HttpClient);
+ }
+
+ var errorMsg = JsonConvert.DeserializeObject<ApiErrorResponse>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
+ throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message);
+ }
+
+ /// <summary>
+ /// Stores a previously compressed image directly into Amazon S3 storage
+ /// </summary>
+ /// <param name="result">The previously compressed image</param>
+ /// <param name="amazonSettings">The settings for the amazon connection</param>
+ /// <param name="path">The path and bucket to store in: bucket/file.png format</param>
+ /// <returns></returns>
+ public async Task<Uri> SaveCompressedImageToAmazonS3(TinyPngCompressResponse result, AmazonS3Configuration amazonSettings, string path)
+ {
+ if (result == null)
+ throw new ArgumentNullException(nameof(result));
+ if (amazonSettings == null)
+ throw new ArgumentNullException(nameof(amazonSettings));
+ if (string.IsNullOrEmpty(path))
+ throw new ArgumentNullException(nameof(path));
+
+ amazonSettings.Path = path;
+
+ var amazonSettingsAsJson = JsonConvert.SerializeObject(new { store = amazonSettings }, JsonSettings);
+
+ 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);
+
+ if (response.IsSuccessStatusCode)
+ {
+ return response.Headers.Location;
+ }
+
+ var errorMsg = JsonConvert.DeserializeObject<ApiErrorResponse>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
+ throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message);
+ }
+
+ /// <summary>
+ /// Stores a previously compressed image directly into Amazon S3 storage
+ /// </summary>
+ /// <param name="result">The previously compressed image</param>
+ /// <param name="path">The path to storage the image as</param>
+ /// <param name="bucketOverride">Optional: To override the previously configured bucket</param>
+ /// <param name="regionOverride">Optional: To override the previously configured region</param>
+ /// <returns></returns>
+ public Task<Uri> SaveCompressedImageToAmazonS3(TinyPngCompressResponse result, string path, string bucketOverride = "", string regionOverride = "")
+ {
+ if (result == null)
+ throw new ArgumentNullException(nameof(result));
+ if (AmazonS3Configuration == null)
+ throw new InvalidOperationException("AmazonS3Configuration has not been configured");
+ if (string.IsNullOrEmpty(path))
+ throw new ArgumentNullException(nameof(path));
+
+ var amazonSettings = AmazonS3Configuration.Clone();
+ amazonSettings.Path = path;
+
+ if (!string.IsNullOrEmpty(regionOverride))
+ amazonSettings.Region = regionOverride;
+
+ if (!string.IsNullOrEmpty(bucketOverride))
+ amazonSettings.Bucket = bucketOverride;
+
+ return SaveCompressedImageToAmazonS3(result, amazonSettings, path);
+ }
+
+ #region IDisposable Support
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ HttpClient?.Dispose();
+ HttpClient = null;
+ }
+ }
+ #endregion
+ }
+}