forked from gorilik324/TradingBot-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseHttpClient.cs
More file actions
83 lines (65 loc) · 3.17 KB
/
Copy pathBaseHttpClient.cs
File metadata and controls
83 lines (65 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using TradingBot.HttpClients.Core.Builders;
using TradingBot.HttpClients.Core.Extensions;
using TradingBot.HttpClients.Core.HttpRequestHandlers;
using TradingBot.HttpClients.Core.ResponseModels;
namespace TradingBot.HttpClients.Core
{
public class BaseHttpClient : HttpClient
{
protected virtual IHttpRequestHandler HttpRequestHandler => new BaseHttpRequestHandler();
public BaseHttpClient() : base(new HttpClientHandlerBuilder().WithAllowAutoRedirect()
.WithAutomaticDecompression().UseCertificateCustomValidation().UseSslProtocols().Build())
{
}
public BaseHttpClient(string uri) : base(new HttpClientHandlerBuilder().WithAllowAutoRedirect()
.WithAutomaticDecompression().UseCertificateCustomValidation().UseSslProtocols().Build())
{
if (string.IsNullOrEmpty(uri)) throw new ArgumentNullException(nameof(uri));
BaseAddress = new Uri(uri);
}
protected virtual async Task<ResponseModel<TResult>> GetAsync<TResult>(string uri,
CancellationToken cancellationToken = default)
{
if (uri == null) throw new ArgumentNullException(nameof(uri));
return await HttpRequestHandler.HandleAsync<TResult>(() => GetAsync(uri, cancellationToken));
}
protected virtual async Task<ResponseModel<TResult>> PostAsync<TResult>(string uri, object content,
CancellationToken cancellationToken = default)
{
if (uri == null) throw new ArgumentNullException(nameof(uri));
return await HttpRequestHandler.HandleAsync<TResult>(() => PostAsync(uri, content.ToStringContent(),
cancellationToken));
}
protected virtual async Task<ResponseModel<TResult>> PutAsync<TResult>(string uri, object content,
CancellationToken cancellationToken = default)
{
if (uri == null) throw new ArgumentNullException(nameof(uri));
return await HttpRequestHandler.HandleAsync<TResult>(() => PutAsync(uri, content.ToStringContent(),
cancellationToken));
}
protected virtual async Task<ResponseModel<TResult>> DeleteAsync<TResult>(string uri,
CancellationToken cancellationToken = default)
{
if (uri == null) throw new ArgumentNullException(nameof(uri));
return await HttpRequestHandler.HandleAsync<TResult>(() => DeleteAsync(uri, cancellationToken));
}
public void UseHeaders(Dictionary<string, string> headers)
{
if (headers == null) throw new ArgumentNullException(nameof(headers));
DefaultRequestHeaders.Clear();
foreach (var header in headers)
{
DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
public void OverrideHeaders(Dictionary<string, string> headers)
{
if (headers == null) throw new ArgumentNullException(nameof(headers));
foreach (var header in headers)
{
DefaultRequestHeaders.Remove(header.Key);
DefaultRequestHeaders.Add(header.Key, header.Value);
}
}
}
}