@@ -49,15 +49,42 @@ public class ApiClient
4949 /// </summary>
5050 private const string DELIMITER_HEADER_VALUE = "," ;
5151
52- private static HttpClient client ;
52+ private readonly HttpClient client ;
5353
5454 private readonly ApiContext apiContext ;
5555
5656 public ApiClient ( ApiContext apiContext )
5757 {
5858 this . apiContext = apiContext ;
59+ client = CreateHttpClient ( ) ;
5960 }
6061
62+ private HttpClient CreateHttpClient ( )
63+ {
64+ return new HttpClient ( CreateHttpClientHandler ( ) )
65+ {
66+ BaseAddress = new Uri ( apiContext . GetBaseUri ( ) )
67+ } ;
68+ }
69+
70+ private HttpClientHandler CreateHttpClientHandler ( )
71+ {
72+ // TODO: Add HTTP Public Key Pinning. It is needed to prevent possible man-in-the-middle attacks using
73+ // the fake (or mis-issued) certificates.
74+ // More info: https://timtaubert.de/blog/2014/10/http-public-key-pinning-explained/
75+ // Simply put, we reduce the amount of certificates which are accepted in bunq API responses.
76+ var handler = new HttpClientHandler ( ) ;
77+
78+ if ( apiContext . Proxy != null )
79+ {
80+ handler . Proxy = new BunqProxy ( apiContext . Proxy ) ;
81+ handler . UseProxy = true ;
82+ }
83+
84+ return handler ;
85+ }
86+
87+
6188 /// <summary>
6289 /// Executes a POST request and returns the resulting HTTP response message.
6390 /// </summary>
@@ -90,7 +117,6 @@ private BunqResponseRaw SendRequest(HttpRequestMessage requestMessage,
90117 SetDefaultHeaders ( requestMessage ) ;
91118 SetHeaders ( requestMessage , customHeaders ) ;
92119 SetSessionHeaders ( requestMessage ) ;
93- InitializeHttpClientIfNeeded ( apiContext ) ;
94120 var responseMessage = client . SendAsync ( requestMessage ) . Result ;
95121 AssertResponseSuccess ( responseMessage ) ;
96122 ValidateResponse ( responseMessage ) ;
@@ -194,21 +220,6 @@ private string GenerateSignature(HttpRequestMessage requestMessage)
194220 return SecurityUtils . GenerateSignature ( requestMessage , apiContext . InstallationContext . KeyPairClient ) ;
195221 }
196222
197- private static void InitializeHttpClientIfNeeded ( ApiContext apiContext )
198- {
199- if ( client == null )
200- {
201- // TODO: Add HTTP Public Key Pinning. It is needed to prevent possible man-in-the-middle attacks using
202- // the fake (or mis-issued) certificates.
203- // More info: https://timtaubert.de/blog/2014/10/http-public-key-pinning-explained/
204- // Simply put, we reduce the amount of certificates which are accepted in bunq API responses.
205- client = new HttpClient
206- {
207- BaseAddress = new Uri ( apiContext . GetBaseUri ( ) )
208- } ;
209- }
210- }
211-
212223 private static void AssertResponseSuccess ( HttpResponseMessage responseMessage )
213224 {
214225 if ( responseMessage . IsSuccessStatusCode ) return ;
0 commit comments