/
DanishTransportAuthorityScraper.cs
38 lines (34 loc) · 1.31 KB
/
DanishTransportAuthorityScraper.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
using System;
using System.Net;
using System.Threading.Tasks;
using MobileVehicleInspection.Contracts;
namespace MobileVehicleInspection.Api.Library
{
public class DanishTransportAuthorityScraper : IVehicleInspectionLookup
{
private readonly ScraperSettings _settings;
private readonly DanishTransportAuthorityResponseParser _parser;
public DanishTransportAuthorityScraper(ScraperSettings settings, DanishTransportAuthorityResponseParser parser)
{
_settings = settings;
_parser = parser;
}
public async Task<Vehicle> ByRegistration(RegistrationNumber registration)
{
var client = new WebClient();
var url = new Uri(String.Format(_settings.UrlTemplate, registration));
string result = null;
try
{
result = await client.DownloadStringTaskAsync(url);
}
catch (WebException wex)
{
throw new InvalidOperationException(
string.Format("Unable to retrieve data from {0}, {1}, HTTP {2}", url, wex.Status.ToString(),
wex.Response == null ? -1 : (int) ((HttpWebResponse) wex.Response).StatusCode), wex);
}
return _parser.Parse(result);
}
}
}