Пример #1
0
 /// <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));
 }
Пример #2
0
        /// <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));

            if (amazonConfiguration == null)
                throw new ArgumentNullException(nameof(amazonConfiguration));

            AmazonS3Configuration = amazonConfiguration;
        }
Пример #3
0
        /// <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);
        }
Пример #4
0
        /// <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);
            msg.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);
        }