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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.ApiInteraction
{
/// <summary>
/// Class AsyncHttpClient
/// </summary>
public class AsyncHttpClient : IAsyncHttpClient
{
/// <summary>
/// Gets or sets the HTTP client.
/// </summary>
/// <value>The HTTP client.</value>
private HttpClient HttpClient { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" /> class.
/// </summary>
public AsyncHttpClient(HttpMessageHandler handler)
{
HttpClient = new HttpClient(handler);
}
/// <summary>
/// Initializes a new instance of the <see cref="ApiClient" /> class.
/// </summary>
public AsyncHttpClient()
{
HttpClient = new HttpClient();
}
/// <summary>
/// Gets the stream async.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="logger">The logger.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{Stream}.</returns>
/// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
public async Task<Stream> GetStreamAsync(string url, ILogger logger, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
logger.Debug("Sending Http Get to {0}", url);
try
{
var msg = await HttpClient.GetAsync(url, cancellationToken).ConfigureAwait(false);
EnsureSuccessStatusCode(msg);
return await msg.Content.ReadAsStreamAsync().ConfigureAwait(false);
}
catch (HttpRequestException ex)
{
logger.ErrorException("Error getting response from " + url, ex);
throw new HttpException(ex.Message, ex);
}
catch (OperationCanceledException ex)
{
throw GetCancellationException(url, cancellationToken, ex, logger);
}
catch (Exception ex)
{
logger.ErrorException("Error requesting {0}", ex, url);
throw;
}
}
/// <summary>
/// Posts the async.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="contentType">Type of the content.</param>
/// <param name="postContent">Content of the post.</param>
/// <param name="logger">The logger.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{Stream}.</returns>
/// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
public async Task<Stream> PostAsync(string url, string contentType, string postContent, ILogger logger, CancellationToken cancellationToken)
{
logger.Debug("Sending Http Post to {0}", url);
var content = new StringContent(postContent, Encoding.UTF8, contentType);
try
{
var msg = await HttpClient.PostAsync(url, content).ConfigureAwait(false);
EnsureSuccessStatusCode(msg);
return await msg.Content.ReadAsStreamAsync().ConfigureAwait(false);
}
catch (HttpRequestException ex)
{
logger.ErrorException("Error getting response from " + url, ex);
throw new HttpException(ex.Message, ex);
}
catch (OperationCanceledException ex)
{
throw GetCancellationException(url, cancellationToken, ex, logger);
}
catch (Exception ex)
{
logger.ErrorException("Error posting {0}", ex, url);
throw;
}
}
/// <summary>
/// Deletes the async.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="logger">The logger.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
public async Task DeleteAsync(string url, ILogger logger, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
logger.Debug("Sending Http Delete to {0}", url);
try
{
using (var msg = await HttpClient.DeleteAsync(url, cancellationToken).ConfigureAwait(false))
{
EnsureSuccessStatusCode(msg);
}
}
catch (HttpRequestException ex)
{
logger.ErrorException("Error getting response from " + url, ex);
throw new HttpException(ex.Message, ex);
}
catch (OperationCanceledException ex)
{
throw GetCancellationException(url, cancellationToken, ex, logger);
}
catch (Exception ex)
{
logger.ErrorException("Error requesting {0}", ex, url);
throw;
}
}
/// <summary>
/// Throws the cancellation exception.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="exception">The exception.</param>
/// <param name="logger">The logger.</param>
/// <returns>Exception.</returns>
private Exception GetCancellationException(string url, CancellationToken cancellationToken, OperationCanceledException exception, ILogger logger)
{
// If the HttpClient's timeout is reached, it will cancel the Task internally
if (!cancellationToken.IsCancellationRequested)
{
var msg = string.Format("Connection to {0} timed out", url);
logger.Error(msg);
// Throw an HttpException so that the caller doesn't think it was cancelled by user code
return new HttpException(msg, exception) { IsTimedOut = true };
}
return exception;
}
/// <summary>
/// Ensures the success status code.
/// </summary>
/// <param name="response">The response.</param>
/// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
private void EnsureSuccessStatusCode(HttpResponseMessage response)
{
if (!response.IsSuccessStatusCode)
{
throw new HttpException(response.ReasonPhrase) { StatusCode = response.StatusCode };
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
HttpClient.Dispose();
}
}
/// <summary>
/// Sets the authorization header that should be supplied on every request
/// </summary>
/// <param name="header">The header.</param>
/// <exception cref="System.NotImplementedException"></exception>
public void SetAuthorizationHeader(string header)
{
if (string.IsNullOrEmpty(header))
{
HttpClient.DefaultRequestHeaders.Remove("Authorization");
}
else
{
HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("MediaBrowser", header);
}
}
}
}
|