Skip to content

Commit 0fee7cf

Browse files
committed
add an option to specify base url at build time
1 parent 3ad4d4e commit 0fee7cf

4 files changed

Lines changed: 47 additions & 1 deletion

File tree

Simple.HttpClientFactory.Tests/BasicClientBuilderTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,27 @@ public async Task Can_do_http_get_with_plain_client()
6969
Assert.Equal("Hello world!", await response.Content.ReadAsStringAsync());
7070
}
7171

72+
[Fact]
73+
public async Task Can_do_http_get_with_plain_client_with_base_url()
74+
{
75+
var client = HttpClientFactory.Create(new Uri(_server.Urls[0])).Build();
76+
var response = await client.GetAsync("/hello/world");
77+
78+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
79+
Assert.Equal("Hello world!", await response.Content.ReadAsStringAsync());
80+
}
81+
82+
[Fact]
83+
public async Task Can_do_http_get_with_plain_client_with_base_url_alternative_syntax()
84+
{
85+
var client = HttpClientFactory.Create().WithBaseUrl(new Uri(_server.Urls[0])).Build();
86+
var response = await client.GetAsync("/hello/world");
87+
88+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
89+
Assert.Equal("Hello world!", await response.Content.ReadAsStringAsync());
90+
}
91+
92+
7293
[Fact]
7394
public async Task Can_do_http_post_with_plain_client()
7495
{

Simple.HttpClientFactory/HttpClientBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace Simple.HttpClientFactory
1515
internal class HttpClientBuilder : IHttpClientBuilder
1616
{
1717
private TimeSpan? _timeout;
18+
private Uri _baseUrl;
1819
private readonly List<X509Certificate2> _certificates = new List<X509Certificate2>();
1920
private readonly List<IAsyncPolicy<HttpResponseMessage>> _policies = new List<IAsyncPolicy<HttpResponseMessage>>();
2021
private readonly List<DelegatingHandler> _middlewareHandlers = new List<DelegatingHandler>();
@@ -29,6 +30,12 @@ public IHttpClientBuilder WithDefaultHeader(string name, string value)
2930
return this;
3031
}
3132

33+
public IHttpClientBuilder WithBaseUrl(Uri baserUrl)
34+
{
35+
_baseUrl = new Uri(baserUrl.ToString());
36+
return this;
37+
}
38+
3239
public IHttpClientBuilder WithDefaultHeaders(IReadOnlyDictionary<string, string> headers)
3340
{
3441
foreach(var kvp in headers)
@@ -258,6 +265,9 @@ HttpClient InitializeClientOnlyWithMiddleware()
258265
else
259266
createdClient = new HttpClient(clientHandler, true);
260267

268+
if(_baseUrl != null)
269+
createdClient.BaseAddress = _baseUrl;
270+
261271
return createdClient;
262272
}
263273
}

Simple.HttpClientFactory/HttpClientFactory.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net.Http;
1+
using System;
2+
using System.Net.Http;
23

34
namespace Simple.HttpClientFactory
45
{
@@ -18,5 +19,14 @@ public static class HttpClientFactory
1819
/// <returns></returns>
1920
public static IHttpClientBuilder Create(params DelegatingHandler[] handlers) =>
2021
new HttpClientBuilder().WithMessageHandlers(handlers);
22+
23+
/// <summary>
24+
/// Create HttpClient builder with initial message processing pipeline
25+
/// </summary>
26+
/// <param name="baseUrl">Base url to use in the client</param>
27+
/// <param name="handlers">Http message handlers to chain into HttpClient's processing pipeline</param>
28+
/// <returns></returns>
29+
public static IHttpClientBuilder Create(Uri baseUrl, params DelegatingHandler[] handlers) =>
30+
new HttpClientBuilder().WithMessageHandlers(handlers).WithBaseUrl(baseUrl);
2131
}
2232
}

Simple.HttpClientFactory/IHttpClientBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ namespace Simple.HttpClientFactory
88
{
99
public interface IHttpClientBuilder
1010
{
11+
/// <summary>
12+
/// Specify base url to use in the client
13+
/// </summary>
14+
IHttpClientBuilder WithBaseUrl(Uri baserUrl);
15+
1116
/// <summary>
1217
/// Add default headers to be added to each request
1318
/// </summary>

0 commit comments

Comments
 (0)