Skip to content

Commit

Permalink
Merge pull request #149 from slawor/feature/httpClientBuilder
Browse files Browse the repository at this point in the history
Created a static class to create HttpClient with proxy settings
  • Loading branch information
Toxantron authored Jun 10, 2022
2 parents 1084ff6 + d63cdc8 commit 1c38798
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,7 @@ private async Task EvaluateResponse(Task<Communication.Endpoints.Endpoint[]> res
{
// Create HttpClient
var proxyConfig = (_endpointService as IProxyConfigAccess)?.ProxyConfig;
if (proxyConfig?.EnableProxy == true && !proxyConfig.UseDefaultWebProxy)
{
var proxy = new WebProxy
{
Address = new Uri($"http://{proxyConfig.Address}:{proxyConfig.Port}"),
BypassProxyOnLocal = false,
UseDefaultCredentials = true
};

HttpClient = new HttpClient(new HttpClientHandler { Proxy = proxy });
}
else
{
HttpClient = new HttpClient();
}

HttpClient.BaseAddress = new Uri(endpoint.Address);
HttpClient = HttpClientBuilder.GetClient(endpoint.Address, proxyConfig);

if (!string.IsNullOrEmpty(_myCulture.IetfLanguageTag))
HttpClient.DefaultRequestHeaders.AcceptLanguage
Expand Down
39 changes: 39 additions & 0 deletions src/Moryx/Communication/Endpoints/Connector/HttpClientBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2021, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using System;
using System.Net;
using System.Net.Http;

namespace Moryx.Communication.Endpoints
{
/// <summary>
/// Helper class to get a configured HttpClient
/// </summary>
public static class HttpClientBuilder
{
/// <summary>
/// Creates a HttpClient based on the proxy config and base address
/// </summary>
public static HttpClient GetClient(string baseAddress, IProxyConfig proxyConfig)
{
var httpClient = new HttpClient();

if (proxyConfig?.EnableProxy == true && !proxyConfig.UseDefaultWebProxy)
{
var proxy = new WebProxy
{
Address = new Uri($"http://{proxyConfig.Address}:{proxyConfig.Port}"),
BypassProxyOnLocal = false,
UseDefaultCredentials = true
};

httpClient = new HttpClient(new HttpClientHandler { Proxy = proxy });
}

httpClient.BaseAddress = new Uri(baseAddress);

return httpClient;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
Expand Down Expand Up @@ -32,25 +31,7 @@ public abstract class VersionServiceManager<TEndpoint> : IVersionServiceManager,
protected VersionServiceManager(IProxyConfig proxyConfig, string host, int port)
{
ProxyConfig = proxyConfig;

// Create HttpClient
if (ProxyConfig?.EnableProxy == true && !ProxyConfig.UseDefaultWebProxy)
{
var proxy = new WebProxy
{
Address = new Uri($"http://{ProxyConfig.Address}:{ProxyConfig.Port}"),
BypassProxyOnLocal = false,
UseDefaultCredentials = true
};

Client = new HttpClient(new HttpClientHandler { Proxy = proxy });
}
else
{
Client = new HttpClient();
}

Client.BaseAddress = new Uri($"http://{host}:{port}/{ServiceName}/");
Client = HttpClientBuilder.GetClient($"http://{host}:{port}/{ServiceName}/", ProxyConfig);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,7 @@ private async Task EvaluateResponse(Task<Endpoint[]> resp)
{
// Create HttpClient
var proxyConfig = (_endpointService as IProxyConfigAccess)?.ProxyConfig;
if (proxyConfig?.EnableProxy == true && !proxyConfig.UseDefaultWebProxy)
{
var proxy = new WebProxy
{
Address = new Uri($"http://{proxyConfig.Address}:{proxyConfig.Port}"),
BypassProxyOnLocal = false,
UseDefaultCredentials = true
};

HttpClient = new HttpClient(new HttpClientHandler { Proxy = proxy });
}
else
{
HttpClient = new HttpClient();
}

HttpClient.BaseAddress = new Uri(endpoint.Address);
HttpClient = HttpClientBuilder.GetClient(endpoint.Address, proxyConfig);

if (!string.IsNullOrEmpty(_myCulture.IetfLanguageTag))
HttpClient.DefaultRequestHeaders.AcceptLanguage
Expand Down

0 comments on commit 1c38798

Please sign in to comment.