Skip to content

Commit dfe1453

Browse files
author
Eirini
committed
refactor: refactor Client constructor to support custom HttpClient and add validation for proxy and HttpClient conflicts
1 parent 03dd4c4 commit dfe1453

File tree

3 files changed

+120
-4
lines changed

3 files changed

+120
-4
lines changed

src/Tinify/Client.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal sealed class ErrorData
3232

3333
private readonly HttpClient _client;
3434

35-
public Client(string key, string appIdentifier = null, string proxy = null, HttpClient customHttpClient = null)
35+
public Client(string key, string appIdentifier = null, string proxy = null)
3636
{
3737
var handler = new HttpClientHandler();
3838
if (proxy != null)
@@ -41,14 +41,36 @@ public Client(string key, string appIdentifier = null, string proxy = null, Http
4141
handler.UseProxy = true;
4242
}
4343

44+
_client = new HttpClient(handler)
45+
{
46+
BaseAddress = ApiEndpoint,
47+
Timeout = Timeout.InfiniteTimeSpan,
48+
};
49+
50+
51+
var userAgent = UserAgent;
52+
if (appIdentifier != null)
53+
{
54+
userAgent += " " + appIdentifier;
55+
}
56+
57+
_client.DefaultRequestHeaders.Add("User-Agent", userAgent);
58+
59+
var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("api:" + key));
60+
_client.DefaultRequestHeaders.Add("Authorization", "Basic " + credentials);
61+
}
62+
63+
public Client(string key, string appIdentifier = null, HttpClient customHttpClient = null)
64+
{
65+
4466
if (customHttpClient != null)
4567
{
4668
_client = customHttpClient;
4769
_client.BaseAddress = ApiEndpoint;
4870
}
4971
else
5072
{
51-
_client = new HttpClient(handler)
73+
_client = new HttpClient()
5274
{
5375
BaseAddress = ApiEndpoint,
5476
Timeout = Timeout.InfiniteTimeSpan,
@@ -67,6 +89,8 @@ public Client(string key, string appIdentifier = null, string proxy = null, Http
6789
_client.DefaultRequestHeaders.Add("Authorization", "Basic " + credentials);
6890
}
6991

92+
93+
7094
public Task<HttpResponseMessage> Request(Method method, string url)
7195
{
7296
return Request(method, new Uri(url, UriKind.Relative));

src/Tinify/Tinify.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Net.Http;
23
using System.Threading.Tasks;
34

@@ -15,6 +16,25 @@ public class Tinify
1516
private static string key;
1617
private static string appIdentifier;
1718
private static string proxy;
19+
private static HttpClient httpClient;
20+
21+
public static HttpClient HttpClient
22+
{
23+
get
24+
{
25+
return httpClient;
26+
}
27+
28+
set
29+
{
30+
if (value != null && proxy != null)
31+
{
32+
throw new ArgumentException("Cannot set HttpClient when Proxy is already configured. Please set either HttpClient or Proxy, not both.");
33+
}
34+
httpClient = value;
35+
ResetClient();
36+
}
37+
}
1838

1939
public static string Key
2040
{
@@ -53,6 +73,10 @@ public static string Proxy
5373

5474
set
5575
{
76+
if (value != null && httpClient != null)
77+
{
78+
throw new ArgumentException("Cannot set Proxy when HttpClient is already configured. Please set either HttpClient or Proxy, not both.");
79+
}
5680
proxy = value;
5781
ResetClient();
5882
}
@@ -88,7 +112,10 @@ public static Client Client
88112
{
89113
if (client == null)
90114
{
91-
client = new Client(key, appIdentifier, proxy);
115+
if (httpClient != null)
116+
client = new Client(key, appIdentifier, httpClient);
117+
else
118+
client = new Client(key, appIdentifier, proxy);
92119
}
93120
}
94121
return client;

test/Tinify.Tests/TinifyTest.cs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public void TearDown()
1717
{
1818
Tinify.Key = null;
1919
Tinify.Proxy = null;
20+
Tinify.HttpClient = null;
2021
}
2122
}
2223

@@ -124,6 +125,70 @@ public void WithInvalidProxy_Should_ThrowException()
124125
}
125126
}
126127

128+
[TestFixture]
129+
public class Tinify_ProxyHttpClientConflict : Reset
130+
{
131+
[Test]
132+
public void WithProxyFirst_HttpClientSecond_Should_ThrowException()
133+
{
134+
Tinify.Proxy = "http://localhost:8080";
135+
136+
var httpClient = new HttpClient();
137+
var error = Assert.Throws<ArgumentException>(() =>
138+
{
139+
Tinify.HttpClient = httpClient;
140+
});
141+
142+
Assert.AreEqual(
143+
"Cannot set HttpClient when Proxy is already configured. Please set either HttpClient or Proxy, not both.",
144+
error?.Message
145+
);
146+
}
147+
148+
[Test]
149+
public void WithHttpClientFirst_ProxySecond_Should_ThrowException()
150+
{
151+
var httpClient = new HttpClient();
152+
Tinify.HttpClient = httpClient;
153+
154+
var error = Assert.Throws<ArgumentException>(() =>
155+
{
156+
Tinify.Proxy = "http://localhost:8080";
157+
});
158+
159+
Assert.AreEqual(
160+
"Cannot set Proxy when HttpClient is already configured. Please set either HttpClient or Proxy, not both.",
161+
error?.Message
162+
);
163+
}
164+
165+
[Test]
166+
public void CanSetProxyAfterHttpClientIsCleared()
167+
{
168+
var httpClient = new HttpClient();
169+
Tinify.HttpClient = httpClient;
170+
Tinify.HttpClient = null;
171+
172+
Assert.DoesNotThrow(() =>
173+
{
174+
Tinify.Proxy = "http://localhost:8080";
175+
});
176+
}
177+
178+
[Test]
179+
public void CanSetHttpClientAfterProxyIsCleared()
180+
{
181+
Tinify.Proxy = "http://localhost:8080";
182+
Tinify.Proxy = null;
183+
184+
var httpClient = new HttpClient();
185+
Assert.DoesNotThrow(() =>
186+
{
187+
Tinify.HttpClient = httpClient;
188+
});
189+
}
190+
}
191+
127192
[TestFixture]
128193
public class Tinify_Validate : Reset
129194
{
@@ -148,7 +213,7 @@ public void WithLimitedKey_Should_ReturnTrue()
148213

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

0 commit comments

Comments
 (0)