Skip to content

Commit

Permalink
Ability to provide the service with custom HTTP message handler (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aliaksandr Basau authored and nvborisenko committed Sep 26, 2018
1 parent 7d901c1 commit 4361038
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions ReportPortal.Client/Service.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,20 +13,17 @@ namespace ReportPortal.Client
public partial class Service
{
private readonly HttpClient _httpClient;
private readonly HttpClientHandler _httpHandler;

/// <summary>
/// Constructor to initialize a new object of service.
/// </summary>
/// <param name="uri">Base URI for REST service.</param>
/// <param name="project">A project to manage.</param>
/// <param name="password">A password for user. Can be UID given from user's profile page.</param>
public Service(Uri uri, string project, string password)
/// <param name="messageHandler">The HTTP handler to use for sending all requests.</param>
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();
Expand All @@ -42,6 +38,17 @@ public Service(Uri uri, string project, string password)
#endif
}

/// <summary>
/// Constructor to initialize a new object of service.
/// </summary>
/// <param name="uri">Base URI for REST service.</param>
/// <param name="project">A project to manage.</param>
/// <param name="password">A password for user. Can be UID given from user's profile page.</param>
public Service(Uri uri, string project, string password)
: this(uri, project, password, new RetryHttpClientHandler())
{
}

/// <summary>
/// Constructor to initialize a new object of service.
/// </summary>
Expand All @@ -50,9 +57,8 @@ public Service(Uri uri, string project, string password)
/// <param name="password">A password for user. Can be UID given from user's profile page.</param>
/// <param name="proxy">Proxy for all HTTP requests.</param>
public Service(Uri uri, string project, string password, IWebProxy proxy)
: this(uri, project, password)
: this(uri, project, password, new RetryHttpClientHandler(proxy))
{
_httpHandler.Proxy = proxy;
}

/// <summary>
Expand All @@ -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<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Expand All @@ -80,7 +97,9 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
return response = await base.SendAsync(request, cancellationToken);
}
// timeout
catch(TaskCanceledException) { }
catch (TaskCanceledException)
{
}
}

return response;
Expand Down

0 comments on commit 4361038

Please sign in to comment.