2018-04-30 23:44:19 +02:00
|
|
|
|
using System.Net.Http;
|
2018-04-28 01:01:48 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2020-08-08 21:09:27 +02:00
|
|
|
|
using Geekbot.net.Lib.WikipediaClient.Page;
|
2018-04-28 01:01:48 +02:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
2020-08-08 21:09:27 +02:00
|
|
|
|
namespace Geekbot.net.Lib.WikipediaClient
|
2018-04-28 01:01:48 +02:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|