11using System ;
2- using System . IO ;
32using System . Net ;
43using System . Linq ;
54using System . Text ;
@@ -14,7 +13,7 @@ namespace ReversoAPI.Web.Shared.Infrastructure.Http
1413{
1514 public class APIConnector : IAPIConnector
1615 {
17- private readonly HttpClient _httpClient ;
16+ private readonly IHttpClient _httpClient ;
1817
1918 private const int RetryAttemptCount = 4 ;
2019 private static readonly HttpStatusCode [ ] _httpStatusCodesWorthRetrying =
@@ -26,36 +25,23 @@ public class APIConnector : IAPIConnector
2625 HttpStatusCode . GatewayTimeout
2726 } ;
2827
29- private APIConnector ( HttpClient httpClient )
28+ public APIConnector ( IHttpClient httpClient )
3029 {
3130 _httpClient = httpClient ;
3231 }
3332
34- public static APIConnector Create ( IHttpClientCacheWrapper httpClientCache )
35- {
36- return new APIConnector ( httpClientCache . GetHttpClient ( ) ) ;
37- }
38-
3933 public async Task < HttpResponse > GetAsync ( Uri uri , CancellationToken cancellationToken = default )
4034 {
4135 if ( uri == null ) throw new ArgumentNullException ( nameof ( uri ) ) ;
4236
43- using var response = await Policy
37+ var response = await Policy
4438 . Handle < HttpRequestException > ( )
45- . OrResult < HttpResponseMessage > ( r => _httpStatusCodesWorthRetrying . Contains ( r . StatusCode ) )
39+ . OrResult < HttpResponse > ( r => _httpStatusCodesWorthRetrying . Contains ( r . StatusCode ) )
4640 . WaitAndRetryAsync ( RetryAttemptCount , attempt => TimeSpan . FromSeconds ( Math . Pow ( 2 , attempt ) ) )
4741 . ExecuteAsync ( ( ) => _httpClient . GetAsync ( uri , cancellationToken ) )
4842 . ConfigureAwait ( false ) ;
4943
50- var content = await response . Content
51- . ReadAsStreamAsync ( )
52- . ConfigureAwait ( false ) ;
53-
54- return new HttpResponse
55- {
56- ContentType = response . Content . Headers . ContentType . MediaType ,
57- Content = await MakeACopyAsync ( content , cancellationToken ) . ConfigureAwait ( false ) ,
58- } ;
44+ return response ;
5945 }
6046
6147 public async Task < HttpResponse > PostAsync ( Uri uri , object payload , CancellationToken cancellationToken = default )
@@ -66,35 +52,14 @@ public async Task<HttpResponse> PostAsync(Uri uri, object payload, CancellationT
6652 var json = JsonConvert . SerializeObject ( payload ) ;
6753 var data = new StringContent ( json , Encoding . UTF8 , "application/json" ) ;
6854
69- using var response = await Policy
55+ var response = await Policy
7056 . Handle < HttpRequestException > ( )
71- . OrResult < HttpResponseMessage > ( r => _httpStatusCodesWorthRetrying . Contains ( r . StatusCode ) )
57+ . OrResult < HttpResponse > ( r => _httpStatusCodesWorthRetrying . Contains ( r . StatusCode ) )
7258 . WaitAndRetryAsync ( RetryAttemptCount , attempt => TimeSpan . FromSeconds ( Math . Pow ( 2 , attempt ) ) )
7359 . ExecuteAsync ( ( ) => _httpClient . PostAsync ( uri , data , cancellationToken ) )
7460 . ConfigureAwait ( false ) ;
7561
76- var content = await response . Content
77- . ReadAsStreamAsync ( )
78- . ConfigureAwait ( false ) ;
79-
80- return new HttpResponse
81- {
82- ContentType = response . Content . Headers . ContentType . MediaType ,
83- Content = await MakeACopyAsync ( content , cancellationToken ) . ConfigureAwait ( false ) ,
84- } ;
85- }
86-
87- // TODO: Rid of this. HttpResponseMessage disposing leads to close Content stream.
88- private async Task < Stream > MakeACopyAsync ( Stream stream , CancellationToken cancellationToken = default )
89- {
90- var ms = new MemoryStream ( ) ;
91-
92- await stream
93- . CopyToAsync ( ms , cancellationToken )
94- . ConfigureAwait ( false ) ;
95-
96- ms . Position = 0 ;
97- return ms ;
62+ return response ;
9863 }
9964 }
10065}
0 commit comments