Skip to content

Commit

Permalink
Merge pull request #207 from digipost/proxy_support
Browse files Browse the repository at this point in the history
Add proxy-support to ClientConfig
  • Loading branch information
eivinhb authored Oct 4, 2023
2 parents e299a59 + 15200ed commit 9a94439
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
16 changes: 14 additions & 2 deletions Digipost.Api.Client.Common/ClientConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Digipost.Api.Client.Common
using System.Net;

namespace Digipost.Api.Client.Common
{
/// <summary>
/// Contains configuration for sending digital post.
Expand Down Expand Up @@ -30,11 +32,21 @@ public ClientConfig(Broker broker, Environment environment)
/// </summary>
public Broker Broker { get; set; }

/// <summary>
/// If set, the proxy will be used for all requests. Remember to set <see cref="Credential" /> as well.
/// </summary>
public WebProxy WebProxy { get; set; }

/// <summary>
/// Will be used if both this and <see cref="WebProxy" /> is set.
/// </summary>
public NetworkCredential Credential { get; set; }

/// <summary>
/// If set to true, all requests and responses are logged with log level DEBUG.
/// </summary>
public bool LogRequestAndResponse { get; set; }

/// <summary>
/// If set to true, document metadata xml will not be validated.
/// Used to test sending documents as if you've not imported the DataTypes project.
Expand Down
2 changes: 2 additions & 0 deletions Digipost.Api.Client.Docs/CertificateLoading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public void LoadCertificateFromThumbprint()
{
var clientConfig = new ClientConfig(broker, Environment.Production);
var client = new DigipostClient(clientConfig, thumbprint: "84e492a972b7e...");


}

public void LoadCertificateFrom()
Expand Down
20 changes: 14 additions & 6 deletions Digipost.Api.Client/DigipostClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public DigipostClient(ClientConfig clientConfig, X509Certificate2 enterpriseCert
_entrypointCache = new MemoryCache(new MemoryCacheOptions());

_clientConfig = clientConfig;
var httpClient = GetHttpClient(enterpriseCertificate);
var httpClient = GetHttpClient(enterpriseCertificate, clientConfig.WebProxy, clientConfig.Credential);
_requestHelper = new RequestHelper(httpClient, _loggerFactory);
}

Expand All @@ -55,15 +55,23 @@ private SendMessageApi _sendMessageApi()
return new SendMessageApi(new SendRequestHelper(_requestHelper), _loggerFactory, GetRoot(new ApiRootUri()));
}

private HttpClient GetHttpClient(X509Certificate2 enterpriseCertificate)
private HttpClient GetHttpClient(X509Certificate2 enterpriseCertificate, WebProxy proxy = null, NetworkCredential credential = null)
{
var allDelegationHandlers = new List<DelegatingHandler> {new LoggingHandler(_clientConfig, _loggerFactory), new AuthenticationHandler(_clientConfig, enterpriseCertificate, _loggerFactory)};

var httpMessageHandler = new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
if (proxy != null)
{
proxy.Credentials = credential;
httpMessageHandler.Proxy = proxy;
httpMessageHandler.UseProxy = true;
httpMessageHandler.UseDefaultCredentials = false;
}
var httpClient = HttpClientFactory.Create(
new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
},
httpMessageHandler,
allDelegationHandlers.ToArray()
);

Expand Down
24 changes: 13 additions & 11 deletions docs/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (7.0.4.3)
activesupport (7.0.8)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
addressable (2.8.4)
addressable (2.8.5)
public_suffix (>= 2.0.2, < 6.0)
base64 (0.1.1)
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.11.1)
colorator (1.1.0)
commonmarker (0.23.9)
commonmarker (0.23.10)
concurrent-ruby (1.2.2)
dnsruby (1.70.0)
simpleidn (~> 0.2.1)
Expand All @@ -23,12 +24,13 @@ GEM
ethon (0.16.0)
ffi (>= 1.15.0)
eventmachine (1.2.7)
execjs (2.8.1)
faraday (2.7.4)
execjs (2.9.1)
faraday (2.7.11)
base64
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
ffi (1.15.5)
ffi (1.16.3)
forwardable-extended (2.6.0)
gemoji (3.0.1)
github-pages (228)
Expand Down Expand Up @@ -86,7 +88,7 @@ GEM
activesupport (>= 2)
nokogiri (>= 1.4)
http_parser.rb (0.8.0)
i18n (1.12.0)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
jekyll (3.9.3)
addressable (~> 2.4)
Expand Down Expand Up @@ -209,20 +211,20 @@ GEM
jekyll (>= 3.5, < 5.0)
jekyll-feed (~> 0.9)
jekyll-seo-tag (~> 2.1)
minitest (5.18.0)
nokogiri (1.14.3-x86_64-darwin)
minitest (5.20.0)
nokogiri (1.15.4-x86_64-darwin)
racc (~> 1.4)
octokit (4.25.1)
faraday (>= 1, < 3)
sawyer (~> 0.9)
pathutil (0.16.2)
forwardable-extended (~> 2.6)
public_suffix (4.0.7)
racc (1.6.2)
racc (1.7.1)
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
rexml (3.2.5)
rexml (3.2.6)
rouge (3.26.0)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
Expand Down
12 changes: 12 additions & 0 deletions docs/_v14_0/1_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,15 @@ var root = client.GetRoot(new ApiRootUri(12345));
```

You can use this resource to safely test your integration or to fetch Digipost public key.

### Proxy support
We have basic proxy support in the client. To use a proxy you need to define it in ClientConfig with a NetworkCredential for authentication.

```csharp
var clientConfig = new ClientConfig(broker, Environment.Production)
{
WebProxy = new WebProxy("10.0.0.1"),
Credential = new NetworkCredential("foo", "bar")
};
var client = new DigipostClient(clientConfig, thumbprint: "84e492a972b7e...");
```

0 comments on commit 9a94439

Please sign in to comment.