Skip to content

Commit 9169298

Browse files
committed
Added a way to directly set headers on request
It'll be used (for example) for changing auth token with time, or when requesting on behalf with an oauth token
1 parent 36f9e30 commit 9169298

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

Dysnomia.Common.WebAPIWrapper/WebAPIQuerier.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public WebAPIQuerier(IHttpClientFactory clientFactory, Dictionary<string, string
1616
_defaultHeaders = defaultHeaders;
1717
}
1818

19-
private HttpClient GetClient() {
19+
private HttpClient GetClient(Dictionary<string, string> headers = null) {
2020
var client = _clientFactory.CreateClient();
2121

2222
if (_defaultHeaders != null) {
@@ -25,6 +25,12 @@ private HttpClient GetClient() {
2525
}
2626
}
2727

28+
if (headers != null) {
29+
foreach (var header in headers) {
30+
client.DefaultRequestHeaders.Add(header.Key, header.Value);
31+
}
32+
}
33+
2834
return client;
2935
}
3036

@@ -45,34 +51,34 @@ protected async Task ThrowAPIErrors(HttpResponseMessage response) {
4551
}
4652
}
4753

48-
protected async Task<string> GetString(string url) {
49-
var response = await GetClient().GetAsync(url);
54+
protected async Task<string> GetString(string url, Dictionary<string, string> headers = null) {
55+
var response = await GetClient(headers).GetAsync(url);
5056

5157
await this.ThrowAPIErrors(response);
5258

5359
return await response.Content.ReadAsStringAsync();
5460
}
5561

56-
protected async Task<T> Get<T>(string url) {
57-
return JsonSerializer.Deserialize<T>(await GetString(url));
62+
protected async Task<T> Get<T>(string url, Dictionary<string, string> headers = null) {
63+
return JsonSerializer.Deserialize<T>(await GetString(url, headers));
5864
}
5965

60-
protected async Task Post(string url, HttpContent content) {
61-
var response = await GetClient().PostAsync(url, content);
66+
protected async Task Post(string url, HttpContent content, Dictionary<string, string> headers = null) {
67+
var response = await GetClient(headers).PostAsync(url, content);
6268

6369
await this.ThrowAPIErrors(response);
6470
}
6571

66-
protected async Task<string> PostString(string url, HttpContent content) {
67-
var response = await GetClient().PostAsync(url, content);
72+
protected async Task<string> PostString(string url, HttpContent content, Dictionary<string, string> headers = null) {
73+
var response = await GetClient(headers).PostAsync(url, content);
6874

6975
await this.ThrowAPIErrors(response);
7076

7177
return await response.Content.ReadAsStringAsync();
7278
}
7379

74-
protected async Task<T> Post<T>(string url, HttpContent content) {
75-
return JsonSerializer.Deserialize<T>(await PostString(url, content));
80+
protected async Task<T> Post<T>(string url, HttpContent content, Dictionary<string, string> headers = null) {
81+
return JsonSerializer.Deserialize<T>(await PostString(url, content, headers));
7682
}
7783
}
7884
}

0 commit comments

Comments
 (0)