2018-04-30 23:44:19 +02:00
|
|
|
|
using System.Net.Http;
|
2018-04-28 01:01:48 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using WikipediaApi.Page;
|
|
|
|
|
|
|
|
|
|
namespace WikipediaApi
|
|
|
|
|
{
|
|
|
|
|
public class WikipediaClient : IWikipediaClient
|
|
|
|
|
{
|
|
|
|
|
private readonly HttpClient _httpClient;
|
2018-04-28 17:38:45 +02:00
|
|
|
|
public WikipediaClient()
|
2018-04-28 01:01:48 +02:00
|
|
|
|
{
|
2018-04-28 17:38:45 +02:00
|
|
|
|
_httpClient = new HttpClient();
|
2018-04-28 01:01:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-28 17:38:45 +02:00
|
|
|
|
public async Task<PagePreview> GetPreview(string pageName, string language = "en")
|
2018-04-28 01:01:48 +02:00
|
|
|
|
{
|
2018-04-28 17:38:45 +02:00
|
|
|
|
var response = await _httpClient.GetAsync($"https://{language}.wikipedia.org/api/rest_v1/page/summary/{pageName}");
|
2018-04-28 01:01:48 +02:00
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var stringResponse = await response.Content.ReadAsStringAsync();
|
|
|
|
|
return JsonConvert.DeserializeObject<PagePreview>(stringResponse);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|