forked from JohnnyCrazy/SpotifyAPI-NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetHttpClient.cs
More file actions
145 lines (124 loc) · 4.19 KB
/
NetHttpClient.cs
File metadata and controls
145 lines (124 loc) · 4.19 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SpotifyAPI.Web.Http
{
public class NetHttpClient : IHTTPClient
{
private readonly HttpMessageHandler? _httpMessageHandler;
private readonly HttpClient _httpClient;
public NetHttpClient()
{
_httpClient = new HttpClient();
}
public NetHttpClient(HttpClient httpClient)
{
_httpClient = httpClient;
}
public NetHttpClient(IProxyConfig proxyConfig)
{
Ensure.ArgumentNotNull(proxyConfig, nameof(proxyConfig));
_httpMessageHandler = CreateMessageHandler(proxyConfig);
_httpClient = new HttpClient(_httpMessageHandler);
}
public async Task<IResponse> DoRequest(IRequest request, CancellationToken cancel)
{
Ensure.ArgumentNotNull(request, nameof(request));
using HttpRequestMessage requestMsg = BuildRequestMessage(request);
var responseMsg = await _httpClient
.SendAsync(requestMsg, HttpCompletionOption.ResponseContentRead, cancel)
.ConfigureAwait(false);
return await BuildResponse(responseMsg, cancel).ConfigureAwait(false);
}
private static async Task<IResponse> BuildResponse(HttpResponseMessage responseMsg, CancellationToken cancel)
{
Ensure.ArgumentNotNull(responseMsg, nameof(responseMsg));
// We only support text stuff for now
using var content = responseMsg.Content;
var headers = responseMsg.Headers.ToDictionary(header => header.Key, header => header.Value.First());
#if NETSTANDARD2_1
var body = await responseMsg.Content.ReadAsStringAsync().ConfigureAwait(false);
#else
var body = await responseMsg.Content.ReadAsStringAsync(cancel).ConfigureAwait(false);
#endif
var contentType = content.Headers?.ContentType?.MediaType;
return new Response(headers)
{
ContentType = contentType,
StatusCode = responseMsg.StatusCode,
Body = body
};
}
private static HttpRequestMessage BuildRequestMessage(IRequest request)
{
Ensure.ArgumentNotNull(request, nameof(request));
var fullUri = new Uri(request.BaseAddress, request.Endpoint).ApplyParameters(request.Parameters);
var requestMsg = new HttpRequestMessage(request.Method, fullUri);
foreach (var header in request.Headers)
{
requestMsg.Headers.Add(header.Key, header.Value);
}
switch (request.Body)
{
case HttpContent body:
requestMsg.Content = body;
break;
case string body:
requestMsg.Content = new StringContent(body, Encoding.UTF8, "application/json");
break;
case Stream body:
requestMsg.Content = new StreamContent(body);
break;
}
return requestMsg;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_httpClient?.Dispose();
_httpMessageHandler?.Dispose();
}
}
public void SetRequestTimeout(TimeSpan timeout)
{
_httpClient.Timeout = timeout;
}
private static HttpMessageHandler CreateMessageHandler(IProxyConfig proxyConfig)
{
var proxy = new WebProxy
{
Address = new UriBuilder(proxyConfig.Host) { Port = proxyConfig.Port }.Uri,
UseDefaultCredentials = true,
BypassProxyOnLocal = proxyConfig.BypassProxyOnLocal
};
if (!string.IsNullOrEmpty(proxyConfig.User) || !string.IsNullOrEmpty(proxyConfig.Password))
{
proxy.UseDefaultCredentials = false;
proxy.Credentials = new NetworkCredential(proxyConfig.User, proxyConfig.Password);
}
var httpClientHandler = new HttpClientHandler
{
PreAuthenticate = proxy.UseDefaultCredentials,
UseDefaultCredentials = proxy.UseDefaultCredentials,
UseProxy = true,
Proxy = proxy,
};
if (proxyConfig.SkipSSLCheck)
{
httpClientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
}
return httpClientHandler;
}
}
}