-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFetchingContext.cs
34 lines (30 loc) · 1.09 KB
/
FetchingContext.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
using System.Net.Http;
using System.Threading.Tasks;
namespace PickAll
{
/// <summary>The context in which the a document without HTML DOM is fetched.</summary>
public sealed class FetchingContext : IFetchingContext
{
private readonly HttpClient _client;
#if DEBUG
public FetchingContext(HttpClient httpClient) => _client = httpClient;
#else
internal FetchingContext(HttpClient httpClient) => _client = httpClient;
#endif
public async Task<IFetchedDocument> FetchAsync(string address)
{
Guard.AgainstNull(nameof(address), address);
Guard.AgainstEmptyWhiteSpace(nameof(address), address);
try {
var response = await _client.GetAsync(address);
if (!response.IsSuccessStatusCode) {
return FetchedDocument.Empty;
}
return new FetchedDocument(await response.Content.ReadAsByteArrayAsync());
}
catch (HttpRequestException) {
return FetchedDocument.Empty;
}
}
}
}