From 43610384b7509d2e0b91898bbceecf65e8ba8784 Mon Sep 17 00:00:00 2001 From: Aliaksandr Basau Date: Wed, 26 Sep 2018 04:21:41 -0400 Subject: [PATCH] Ability to provide the service with custom HTTP message handler (#16) --- ReportPortal.Client/Service.cs | 43 ++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/ReportPortal.Client/Service.cs b/ReportPortal.Client/Service.cs index 6efc6fb9..17629e0f 100644 --- a/ReportPortal.Client/Service.cs +++ b/ReportPortal.Client/Service.cs @@ -1,6 +1,5 @@ using System; using System.Net; -using ReportPortal.Client.Extentions; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; @@ -14,7 +13,6 @@ namespace ReportPortal.Client public partial class Service { private readonly HttpClient _httpClient; - private readonly HttpClientHandler _httpHandler; /// /// Constructor to initialize a new object of service. @@ -22,12 +20,10 @@ public partial class Service /// Base URI for REST service. /// A project to manage. /// A password for user. Can be UID given from user's profile page. - public Service(Uri uri, string project, string password) + /// The HTTP handler to use for sending all requests. + public Service(Uri uri, string project, string password, HttpMessageHandler messageHandler) { - _httpHandler = new HttpClientHandler(); - var retryHttpHandler = new RetryHttpClientHandler(_httpHandler); - - _httpClient = new HttpClient(retryHttpHandler); + _httpClient = new HttpClient(messageHandler); _httpClient.BaseAddress = uri; _httpClient.DefaultRequestHeaders.Clear(); @@ -42,6 +38,17 @@ public Service(Uri uri, string project, string password) #endif } + /// + /// Constructor to initialize a new object of service. + /// + /// Base URI for REST service. + /// A project to manage. + /// A password for user. Can be UID given from user's profile page. + public Service(Uri uri, string project, string password) + : this(uri, project, password, new RetryHttpClientHandler()) + { + } + /// /// Constructor to initialize a new object of service. /// @@ -50,9 +57,8 @@ public Service(Uri uri, string project, string password) /// A password for user. Can be UID given from user's profile page. /// Proxy for all HTTP requests. public Service(Uri uri, string project, string password, IWebProxy proxy) - : this(uri, project, password) + : this(uri, project, password, new RetryHttpClientHandler(proxy)) { - _httpHandler.Proxy = proxy; } /// @@ -65,9 +71,20 @@ public Service(Uri uri, string project, string password, IWebProxy proxy) public class RetryHttpClientHandler : DelegatingHandler { + public RetryHttpClientHandler() + : this(new HttpClientHandler()) + { + } + + public RetryHttpClientHandler(IWebProxy proxy) + : this(new HttpClientHandler {Proxy = proxy}) + { + } + public RetryHttpClientHandler(HttpMessageHandler innerHandler) - : base(innerHandler) - { } + : base(innerHandler) + { + } protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { @@ -80,7 +97,9 @@ protected override async Task SendAsync(HttpRequestMessage return response = await base.SendAsync(request, cancellationToken); } // timeout - catch(TaskCanceledException) { } + catch (TaskCanceledException) + { + } } return response;