Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit b212763

Browse files
authored
Allow consumer to inject a HttpMessageHandler (#8)
1 parent b7015fe commit b212763

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

Bindle/BindleClient.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,30 @@ string baseUri
1616
)
1717
{
1818
_baseUri = new Uri(SlashSafe(baseUri));
19+
_httpClient = new HttpClient();
20+
}
21+
22+
public BindleClient(
23+
string baseUri,
24+
HttpMessageHandler messageHandler
25+
)
26+
{
27+
_baseUri = new Uri(SlashSafe(baseUri));
28+
_httpClient = new HttpClient(messageHandler);
1929
}
2030

2131
private const string INVOICE_PATH = "_i";
2232
private const string QUERY_PATH = "_q";
2333
private const string RELATIONSHIP_PATH = "_r";
2434

2535
private readonly Uri _baseUri;
36+
private readonly HttpClient _httpClient;
2637

2738
public async Task<Invoice> GetInvoice(string invoiceId, GetInvoiceOptions options = GetInvoiceOptions.None)
2839
{
2940
var query = GetInvoiceQueryString(options);
30-
var httpClient = new HttpClient();
31-
var uri = new Uri(_baseUri, INVOICE_PATH + "/" + invoiceId + query);
32-
var response = await httpClient.GetAsync(uri);
41+
var uri = new Uri(_baseUri, $"{INVOICE_PATH}/{invoiceId}{query}");
42+
var response = await _httpClient.GetAsync(uri);
3343
if (response == null)
3444
{
3545
throw new Exception("No response from Bindle server");
@@ -51,14 +61,13 @@ public async Task<CreateInvoiceResult> CreateInvoice(Invoice invoice)
5161
throw new Exception("Error serialising invoice to TOML");
5262
}
5363

54-
var httpClient = new HttpClient();
5564
var uri = new Uri(_baseUri, INVOICE_PATH);
5665
var requestContent = new StringContent(invoiceToml, null, "application/toml");
5766
if (requestContent.Headers.ContentType != null)
5867
{
5968
requestContent.Headers.ContentType.CharSet = null; // The Bindle server is VERY strict about the contents of the Content-Type header
6069
}
61-
var response = await httpClient.PostAsync(uri, requestContent);
70+
var response = await _httpClient.PostAsync(uri, requestContent);
6271

6372
if (response == null)
6473
{
@@ -74,16 +83,14 @@ public async Task<CreateInvoiceResult> CreateInvoice(Invoice invoice)
7483

7584
public async Task YankInvoice(string invoiceId)
7685
{
77-
var httpClient = new HttpClient();
78-
var uri = new Uri(_baseUri, INVOICE_PATH + "/" + invoiceId);
79-
await httpClient.DeleteAsync(uri);
86+
var uri = new Uri(_baseUri, $"{INVOICE_PATH}/{invoiceId}");
87+
await _httpClient.DeleteAsync(uri);
8088
}
8189

8290
public async Task<HttpContent> GetParcel(string invoiceId, string parcelId)
8391
{
84-
var httpClient = new HttpClient();
8592
var uri = new Uri(_baseUri, $"{INVOICE_PATH}/{invoiceId}@{parcelId}");
86-
var response = await httpClient.GetAsync(uri);
93+
var response = await _httpClient.GetAsync(uri);
8794
if (response == null)
8895
{
8996
throw new Exception("No response from Bindle server");
@@ -112,16 +119,14 @@ public async Task CreateParcel(string invoiceId, string parcelId, byte[] content
112119

113120
public async Task CreateParcel(string invoiceId, string parcelId, HttpContent content)
114121
{
115-
var httpClient = new HttpClient();
116122
var uri = new Uri(_baseUri, $"{INVOICE_PATH}/{invoiceId}@{parcelId}");
117-
await httpClient.PostAsync(uri, content);
123+
await _httpClient.PostAsync(uri, content);
118124
}
119125

120126
public async Task<IEnumerable<Label>> ListMissingParcels(string invoiceId)
121127
{
122-
var httpClient = new HttpClient();
123128
var uri = new Uri(_baseUri, $"{RELATIONSHIP_PATH}/missing/{invoiceId}");
124-
var response = await httpClient.GetAsync(uri);
129+
var response = await _httpClient.GetAsync(uri);
125130
if (response == null)
126131
{
127132
throw new Exception("No response from Bindle server");

0 commit comments

Comments
 (0)