-
Notifications
You must be signed in to change notification settings - Fork 1
/
HttpResolver.cs
286 lines (251 loc) · 10.3 KB
/
HttpResolver.cs
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace JollyQuotes
{
/// <summary>
/// <see cref="IResourceResolver"/> that uses a <see cref="HttpClient"/> to access the requested resources.
/// </summary>
public class HttpResolver : IStreamResolver, IDisposable
{
private bool _disposed;
/// <summary>
/// <see cref="HttpClient"/> that is used to access the requested resources.
/// </summary>
public HttpClient BaseClient { get; }
/// <summary>
/// Determines whether to dispose the <see cref="BaseClient"/> when <see cref="Dispose()"/> is called.
/// </summary>
/// <remarks>The default value is <see langword="false"/>.</remarks>
public bool DisposeClient { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="HttpResolver"/> class with an underlaying <paramref name="client"/> specified.
/// </summary>
/// <param name="client"><see cref="HttpClient"/> that is used to access the requested resources.</param>
/// <exception cref="ArgumentNullException"><paramref name="client"/> is <see langword="null"/>.</exception>
public HttpResolver(HttpClient client) : this(client, false)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpResolver"/> class with an underlaying <paramref name="client"/> specified.
/// </summary>
/// <param name="client"><see cref="HttpClient"/> that is used to access the requested resources.</param>
/// <param name="dispose">Determines whether to dispose the <paramref name="client"/> when <see cref="Dispose()"/> is called.</param>
/// <exception cref="ArgumentNullException"><paramref name="client"/> is <see langword="null"/>.</exception>
public HttpResolver(HttpClient client, bool dispose)
{
if (client is null)
{
throw Error.Null(nameof(client));
}
BaseClient = client;
DisposeClient = dispose;
}
/// <summary>
/// Returns a new instance of <see cref="HttpResolver"/> with default options applied.
/// </summary>
public static HttpResolver CreateDefault()
{
HttpClient client = Internals.CreateDefaultClient();
return new HttpResolver(client);
}
/// <summary>
/// Releases the unmanaged resources used by the <see cref="HttpResolver"/> and optionally disposes of the managed resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Downloads json data from the specified <paramref name="source"/> and deserializes it into a new <typeparamref name="T"/> object.
/// </summary>
/// <param name="source">Link to the json data.</param>
/// <exception cref="ArgumentException"><paramref name="source"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="HttpRequestException">
/// The HTTP response is unsuccessful. -or-
/// Object could not be deserialized from response.
/// </exception>
public virtual T Resolve<T>(string source)
{
return ResolveAsync<T>(source).Result;
}
/// <summary>
/// Asynchronously downloads json data from the specified <paramref name="source"/> and deserializes it into a new <typeparamref name="T"/> object.
/// </summary>
/// <param name="source">Link to the json data.</param>
/// <exception cref="ArgumentException"><paramref name="source"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="HttpRequestException">
/// The HTTP response is unsuccessful. -or-
/// Object could not be deserialized from response.
/// </exception>
public virtual async Task<T> ResolveAsync<T>(string source)
{
if (string.IsNullOrWhiteSpace(source))
{
throw Error.NullOrEmpty(nameof(source));
}
HttpResponseMessage response = await GetResponse(source).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
T? t = JsonConvert.DeserializeObject<T>(json, Quote.JsonSettings);
if (t is null)
{
throw new HttpRequestException($"Object could not be deserialized from source '{source}'");
}
return t;
}
/// <summary>
/// Downloads a <see cref="Stream"/> of data from the specified <paramref name="source"/>.
/// </summary>
/// <param name="source">Source of data to download.</param>
/// <exception cref="ArgumentException"><paramref name="source"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="HttpRequestException">The HTTP response is unsuccessful.</exception>
public Stream ResolveStream(string source)
{
return ResolveStreamAsync(source).Result;
}
/// <summary>
/// Asynchronously downloads a <see cref="Stream"/> of data from the specified <paramref name="source"/>.
/// </summary>
/// <param name="source">Source of data to download.</param>
/// <exception cref="ArgumentException"><paramref name="source"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="HttpRequestException">The HTTP response is unsuccessful.</exception>
public virtual async Task<Stream> ResolveStreamAsync(string source)
{
if (string.IsNullOrWhiteSpace(source))
{
throw Error.NullOrEmpty(nameof(source));
}
HttpResponseMessage response = await GetResponse(source).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
}
/// <summary>
/// Attempts to download json data from the specified <paramref name="source"/> and deserializes it into a new <typeparamref name="T"/> object.
/// </summary>
/// <param name="source">Link to the json data.</param>
/// <exception cref="ArgumentException"><paramref name="source"/> is <see langword="null"/> or empty.</exception>
public virtual HttpResolverResponse<T> TryResolve<T>(string source)
{
return TryResolveAsync<T>(source).Result;
}
/// <summary>
/// Attempts to asynchronously download json data from the specified <paramref name="source"/> and deserializes it into a new <typeparamref name="T"/> object.
/// </summary>
/// <param name="source">Link to the json data.</param>
/// <exception cref="ArgumentException"><paramref name="source"/> is <see langword="null"/> or empty.</exception>
/// <exception cref="HttpRequestException">
/// The HTTP response is unsuccessful. -or-
/// Object could not be deserialized from response.
/// </exception>
public virtual Task<HttpResolverResponse<T>> TryResolveAsync<T>(string source)
{
if (string.IsNullOrWhiteSpace(source))
{
throw Error.NullOrEmpty(nameof(source));
}
return TryResolve(source, response => response.Content.ReadAsStringAsync().ContinueWith(t =>
{
string json = t.Result;
T? result = JsonConvert.DeserializeObject<T>(json, Quote.JsonSettings);
return new HttpResolverResponse<T>(response, json, result);
}));
}
/// <summary>
/// Attempts to download a <see cref="Stream"/> of data from the specified <paramref name="source"/>.
/// </summary>
/// <param name="source">Source of data to download.</param>
/// <exception cref="ArgumentException"><paramref name="source"/> is <see langword="null"/> or empty.</exception>
/// <returns><see langword="true"/> if the data was successfully downloaded, <see langword="false"/> otherwise.</returns>
public virtual HttpResolverResponse<Stream> TryResolveStream(string source)
{
return TryResolveStreamAsync(source).Result;
}
/// <summary>
/// Attempts to asynchronously download a <see cref="Stream"/> of data from the specified <paramref name="source"/>.
/// </summary>
/// <param name="source">Source of data to download.</param>
/// <exception cref="ArgumentException"><paramref name="source"/> is <see langword="null"/> or empty.</exception>
public virtual Task<HttpResolverResponse<Stream>> TryResolveStreamAsync(string source)
{
if (string.IsNullOrWhiteSpace(source))
{
throw Error.NullOrEmpty(nameof(source));
}
return TryResolve(source, response => response.Content.ReadAsStreamAsync().ContinueWith(t =>
{
Stream stream = t.Result;
return new HttpResolverResponse<Stream>(response, result: stream);
}));
}
ResolverResponse<T> IResourceResolver.TryResolve<T>(string source)
{
return ((IResourceResolver)this).TryResolveAsync<T>(source).Result;
}
async Task<ResolverResponse<T>> IResourceResolver.TryResolveAsync<T>(string source)
{
HttpResolverResponse<T> response = await TryResolveAsync<T>(source).ConfigureAwait(false);
return response;
}
ResolverResponse<Stream> IStreamResolver.TryResolveStream(string source)
{
return ((IStreamResolver)this).TryResolveStreamAsync(source).Result;
}
async Task<ResolverResponse<Stream>> IStreamResolver.TryResolveStreamAsync(string source)
{
HttpResolverResponse<Stream> response = await TryResolveStreamAsync(source).ConfigureAwait(false);
return response;
}
/// <summary>
/// Releases the unmanaged resources used by the <see cref="HttpResolver"/> and optionally disposes of the managed resources.
/// </summary>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources;<see langword="false"/> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing && DisposeClient)
{
BaseClient.Dispose();
}
_disposed = true;
}
}
/// <summary>
/// Provides default mechanism for accessing web data.
/// </summary>
/// <typeparam name="T">Type of data to access.</typeparam>
/// <param name="source">Source of the data.</param>
/// <param name="onSuccess">Function performed when a <see cref="HttpResponseMessage"/> is successful.</param>
protected async Task<HttpResolverResponse<T>> TryResolve<T>(string source, Func<HttpResponseMessage, Task<HttpResolverResponse<T>>> onSuccess)
{
try
{
HttpResponseMessage response = await GetResponse(source).ConfigureAwait(false);
try
{
if (response.IsSuccessStatusCode)
{
return await onSuccess(response).ConfigureAwait(false);
}
return new HttpResolverResponse<T>(response);
}
catch (Exception e)
{
return new HttpResolverResponse<T>(response, e);
}
}
catch (Exception e)
{
return new HttpResolverResponse<T>(e);
}
}
private async Task<HttpResponseMessage> GetResponse(string source)
{
return await BaseClient.GetAsync(source).ConfigureAwait(false);
}
}
}