Huge refactoring

Added person info provider
Added person image provider
Added trailers fetching
This commit is contained in:
PingWin committed 2021-05-22 22:21:08 +00:00
1 parent 35ce5536be
commit fce88168e8
43 files changed
+4381 -540

No files matched your search

@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace KinopoiskUnofficialInfo.ApiClient
{
public class KinopoiskApiClient : IKinopoiskApiClient
{
private readonly string _apiToken;
private readonly ILogger<KinopoiskApiClient> _logger;
private readonly IHttpClientFactory _httpClientFactory;
private readonly Client _apiClient;
public KinopoiskApiClient(string apiToken, ILogger<KinopoiskApiClient> logger, IHttpClientFactory httpClientFactory)
{
if (string.IsNullOrEmpty(apiToken))
{
throw new System.ArgumentException($"'{nameof(apiToken)}' cannot be null or empty.", nameof(apiToken));
}
_apiToken = apiToken;
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
_httpClientFactory = httpClientFactory ?? throw new System.ArgumentNullException(nameof(httpClientFactory));
var httpClient = _httpClientFactory.CreateClient();
httpClient.DefaultRequestHeaders.Add("X-API-KEY", _apiToken);
_apiClient = new Client(httpClient);
}
private async Task<T> Invoke<T>(Func<CancellationToken, Task<T>> method, CancellationToken? ct)
{
try
{
return await method.Invoke(ct ?? CancellationToken.None);
}
catch (ApiException e)
{
_logger.LogError($"Received non-success result status code {e.StatusCode} from Kinopoisk API, response content is:\n{e.Response}");
throw;
}
}
public Task<Film> GetSingleFilm(int filmId, CancellationToken? cancellationToken = null)
=> Invoke((ct) => _apiClient.FilmsAsync(filmId, new[] { Anonymous.BUDGET, Anonymous.REVIEW, Anonymous.RATING, Anonymous.POSTERS }, ct), cancellationToken);
public Task<ICollection<StaffResponse>> GetStaff(int filmId, CancellationToken? cancellationToken = null)
=> Invoke((ct) => _apiClient.StaffAllAsync(filmId, ct), cancellationToken);
public Task<FilmSearchResponse> SearchByKeyword(string keyword, int page = 1, CancellationToken? cancellationToken = null)
=> Invoke((ct) => _apiClient.SearchByKeywordAsync(keyword, null, ct), cancellationToken);
public Task<PersonResponse> GetPerson(int personId, CancellationToken? cancellationToken = null)
=> Invoke((ct) => _apiClient.StaffAsync(personId, ct), cancellationToken);
public Task<VideoResponse> GetTrailers(int filmId, CancellationToken? cancellationToken = null)
=> Invoke((ct) => _apiClient.VideosAsync(filmId, ct), cancellationToken);
}
}