Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] Demonstrate how users can provide their own proxy in custom HttpClient #565

Merged
merged 5 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions EasyPost.Tests/ClientTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -109,6 +111,51 @@ public void TestHttpClientOverride()
Assert.Equal(httpClient, overrideClient.CustomHttpClient);
}

[Fact]
public async void TestHttpClientCustomProxy()
{
const string proxyAddress = "localhost:8888";

// Define a custom proxy in a custom HttpClientHandler in a custom HttpClient
HttpClientHandler handler = new()
{
UseProxy = true,
Proxy = new WebProxy($"http://{proxyAddress}"),
};
HttpClient httpClient = new(handler: handler);

Client client = new(new ClientConfiguration(FakeApikey)
{
CustomHttpClient = httpClient,
});

Assert.Equal(httpClient, client.CustomHttpClient);

// Assert that the proxy is set in the HttpClient by attempting to make a request (should fail due to invalid proxy address)
try
{
await client.Address.Create(new Parameters.Address.Create());
nwithan8 marked this conversation as resolved.
Show resolved Hide resolved
Assert.Fail("Expected HttpRequestException");
}
catch (HttpRequestException e)
{
// GitHub runner will reject the connection attempt, this is considered a pass
if (e.Message.Contains("No connection could be made because the target machine actively refused it"))
{
Assert.True(true);
}
else // Evaluate the error message
{
#if NET5_0_OR_GREATER
Assert.Equal($"Connection refused ({proxyAddress})", e.Message);
#else
// Message is inconsistent in .NET Framework, so just assert that the exception was thrown
Assert.True(true);
#endif
}
}
}

[Fact]
public async Task TestRequestHooks()
{
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,28 @@ client.Hooks.OnRequestExecuting += (sender, args) => { /* ... */ };
client.Hooks.OnRequestExecuting -= OnRequestExecutingHandler;
```

### Proxies

If you need to use a proxy to make requests to the EasyPost API, you can define a custom `HttpClientHandler` on a custom `HttpClient` passed to the `ClientConfiguration` constructor.

```csharp
// Define a custom HttpClientHandler with details about the proxy
HttpClientHandler handler = new()
{
UseProxy = true,
Proxy = new WebProxy($"http://localhost:8888"),
nwithan8 marked this conversation as resolved.
Show resolved Hide resolved
};

// Define a custom HttpClient with the custom handler
HttpClient httpClient = new(handler: handler);

// Pass the custom HttpClient to the ClientConfiguration constructor when creating a new EasyPost Client
Client client = new(new ClientConfiguration(FakeApikey)
{
CustomHttpClient = httpClient,
});
```

## Documentation

API documentation can be found at: <https://easypost.com/docs/api>.
Expand Down
Loading