Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.9.0

* Removed custom TLS stack
* Allowed option for callers of library to inject own http client

## 1.8.1

* Upgrade System.Text.JSON
Expand All @@ -9,7 +14,6 @@
* CVE-2024-43485 - Upgrade System.Text.JSON
* Removed .NET 6


## 1.7.0
* Added .NET 8 as test target
* Dropped .NET 6
Expand Down
51 changes: 43 additions & 8 deletions src/Tinify/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@ internal sealed class ErrorData
public static readonly Uri ApiEndpoint = new("https://api.tinify.com");

public static readonly short RetryCount = 1;
public static ushort RetryDelay { get; internal set; }= 500;
public static ushort RetryDelay { get; internal set; } = 500;

public static readonly string UserAgent = Internal.Platform.UserAgent;

private readonly HttpClient _client;

public Client(string key)
: this(key, (string)null, (string)null)
{
}

public Client(string key, string appIdentifier = null)
: this(key, appIdentifier, (string)null)
{
}

public Client(string key, string appIdentifier = null, string proxy = null)
{
var handler = new HttpClientHandler();
if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
// TLS is extremely spotty and differs per version on MacOS
handler.ServerCertificateCustomValidationCallback = Internal.SSL.ValidationCallback;
};

if (proxy != null)
{
handler.Proxy = new Internal.Proxy(proxy);
Expand All @@ -66,6 +70,37 @@ public Client(string key, string appIdentifier = null, string proxy = null)
_client.DefaultRequestHeaders.Add("Authorization", "Basic " + credentials);
}

public Client(string key, string appIdentifier = null, HttpClient customHttpClient = null)
{

if (customHttpClient != null)
{
_client = customHttpClient;
_client.BaseAddress = ApiEndpoint;
}
else
{
_client = new HttpClient()
{
BaseAddress = ApiEndpoint,
Timeout = Timeout.InfiniteTimeSpan,
};
}

var userAgent = UserAgent;
if (appIdentifier != null)
{
userAgent += " " + appIdentifier;
}

_client.DefaultRequestHeaders.Add("User-Agent", userAgent);

var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("api:" + key));
_client.DefaultRequestHeaders.Add("Authorization", "Basic " + credentials);
}



public Task<HttpResponseMessage> Request(Method method, string url)
{
return Request(method, new Uri(url, UriKind.Relative));
Expand Down Expand Up @@ -155,7 +190,7 @@ public async Task<HttpResponseMessage> Request(Method method, Uri url, HttpConte
{
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
data = JsonSerializer.Deserialize<ErrorData>(content) ??
new ErrorData() {Message = "Response content was empty.", Error = "ParseError"};
new ErrorData() { Message = "Response content was empty.", Error = "ParseError" };
}
catch (Exception err)
{
Expand Down
60 changes: 0 additions & 60 deletions src/Tinify/Internal/SSL.cs

This file was deleted.

29 changes: 28 additions & 1 deletion src/Tinify/Tinify.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;

Expand All @@ -15,6 +16,25 @@
private static string key;
private static string appIdentifier;
private static string proxy;
private static HttpClient httpClient;

public static HttpClient HttpClient
{
get
{
return httpClient;
}

set
{
if (value != null && proxy != null)
{
throw new ArgumentException("Cannot set HttpClient when Proxy is already configured. Please set either HttpClient or Proxy, not both.");
}
httpClient = value;
ResetClient();
}
}

public static string Key
{
Expand Down Expand Up @@ -53,6 +73,10 @@

set
{
if (value != null && httpClient != null)
{
throw new ArgumentException("Cannot set Proxy when HttpClient is already configured. Please set either HttpClient or Proxy, not both.");
}
proxy = value;
ResetClient();
}
Expand Down Expand Up @@ -88,7 +112,10 @@
{
if (client == null)
{
client = new Client(key, appIdentifier, proxy);
if (httpClient != null)
client = new Client(key, appIdentifier, httpClient);
else
client = new Client(key, appIdentifier, proxy);
}
}
return client;
Expand Down Expand Up @@ -123,7 +150,7 @@
{
return true;
}
throw err;

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu-22.04, 3.1.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu-22.04, 3.1.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (macOS-latest, 8.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (macOS-latest, 8.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (windows-latest, 8.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (windows-latest, 8.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (windows-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (windows-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (windows-latest, 3.1.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (windows-latest, 3.1.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest, 8.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest, 8.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (macOS-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (macOS-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (macOS-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (macOS-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (macOS-latest, 8.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (macOS-latest, 8.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest, 8.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest, 8.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (windows-latest, 8.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (windows-latest, 8.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu-22.04, 3.1.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (ubuntu-22.04, 3.1.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (windows-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (windows-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (windows-latest, 3.1.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / tests (windows-latest, 3.1.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / integrationTests (ubuntu-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / integrationTests (ubuntu-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / integrationTests (macOS-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / integrationTests (macOS-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / integrationTests (windows-latest, 3.1.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / integrationTests (windows-latest, 3.1.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / integrationTests (ubuntu-22.04, 3.1.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / integrationTests (ubuntu-22.04, 3.1.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / integrationTests (windows-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)

Check warning on line 153 in src/Tinify/Tinify.cs

View workflow job for this annotation

GitHub Actions / integrationTests (windows-latest, 9.0.x)

Re-throwing caught exception changes stack information (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2200)
}
catch (ClientException)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Tinify/Tinify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
<PropertyGroup>
<Description>.NET client for the Tinify API. Tinify compresses your images intelligently. Read more at https://tinify.com.</Description>
<AssemblyTitle>Tinify</AssemblyTitle>
<VersionPrefix>1.8.1</VersionPrefix>
<VersionPrefix>1.9.0</VersionPrefix>
<Authors>Tinify</Authors>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Tinify</AssemblyName>
<AssemblyTitle>Tinify</AssemblyTitle>
<Product>Tinify</Product>
<Copyright>Copyright © 2017-2022</Copyright>
<Copyright>Copyright © 2017-2025</Copyright>
<PackageId>Tinify</PackageId>
<PackageTags>tinify;tinypng;tinyjpg;compress;images;api</PackageTags>
<PackageIcon>tinifyicon.png</PackageIcon>
Expand Down
67 changes: 66 additions & 1 deletion test/Tinify.Tests/TinifyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void TearDown()
{
Tinify.Key = null;
Tinify.Proxy = null;
Tinify.HttpClient = null;
}
}

Expand Down Expand Up @@ -124,6 +125,70 @@ public void WithInvalidProxy_Should_ThrowException()
}
}

[TestFixture]
public class Tinify_ProxyHttpClientConflict : Reset
{
[Test]
public void WithProxyFirst_HttpClientSecond_Should_ThrowException()
{
Tinify.Proxy = "http://localhost:8080";

var httpClient = new HttpClient();
var error = Assert.Throws<ArgumentException>(() =>
{
Tinify.HttpClient = httpClient;
});

Assert.AreEqual(
"Cannot set HttpClient when Proxy is already configured. Please set either HttpClient or Proxy, not both.",
error?.Message
);
}

[Test]
public void WithHttpClientFirst_ProxySecond_Should_ThrowException()
{
var httpClient = new HttpClient();
Tinify.HttpClient = httpClient;

var error = Assert.Throws<ArgumentException>(() =>
{
Tinify.Proxy = "http://localhost:8080";
});

Assert.AreEqual(
"Cannot set Proxy when HttpClient is already configured. Please set either HttpClient or Proxy, not both.",
error?.Message
);
}

[Test]
public void CanSetProxyAfterHttpClientIsCleared()
{
var httpClient = new HttpClient();
Tinify.HttpClient = httpClient;
Tinify.HttpClient = null;

Assert.DoesNotThrow(() =>
{
Tinify.Proxy = "http://localhost:8080";
});
}

[Test]
public void CanSetHttpClientAfterProxyIsCleared()
{
Tinify.Proxy = "http://localhost:8080";
Tinify.Proxy = null;

var httpClient = new HttpClient();
Assert.DoesNotThrow(() =>
{
Tinify.HttpClient = httpClient;
});
}
}

[TestFixture]
public class Tinify_Validate : Reset
{
Expand All @@ -148,7 +213,7 @@ public void WithLimitedKey_Should_ReturnTrue()

Helper.MockClient(Tinify.Client);
Helper.MockHandler.Expect("https://api.tinify.com/shrink").Respond(
(HttpStatusCode) 429,
(HttpStatusCode)429,
new StringContent("{\"error\":\"Too may requests\",\"message\":\"Your monthly limit has been exceeded\"}")
);

Expand Down