Huge refactoring
Added person info provider Added person image provider Added trailers fetching
This commit is contained in:
1 parent
35ce5536be
commit
fce88168e8
43 files changed
+4381
-540
No files matched your search
@@ -0,0 +1,40 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace KinopoiskUnofficialInfo.ApiClient
|
||||
{
|
||||
public class DeclarationPatcherContractResolver : DefaultContractResolver
|
||||
{
|
||||
protected override IList<JsonProperty> CreateProperties(System.Type type, MemberSerialization memberSerialization)
|
||||
{
|
||||
var list = base.CreateProperties(type, memberSerialization);
|
||||
foreach (var property in list)
|
||||
{
|
||||
if (property.PropertyType == typeof(string))
|
||||
{
|
||||
property.Required = Required.Default;
|
||||
}
|
||||
|
||||
if (property.PropertyType == typeof(PersonResponse_filmsProfessionKey))
|
||||
{
|
||||
#pragma warning disable CS0618
|
||||
property.MemberConverter = null;
|
||||
#pragma warning restore CS0618
|
||||
property.Converter = new JsonProfessionKeyConverter();
|
||||
}
|
||||
|
||||
if (type == typeof(VideoResponse_trailers))
|
||||
{
|
||||
if (property.PropertyName == "size")
|
||||
{
|
||||
property.Required = Required.Default;
|
||||
property.DefaultValue = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,15 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KinopoiskUnofficialInfo.ApiClient
|
||||
{
|
||||
public interface IKinopoiskApiClient
|
||||
{
|
||||
Task<PersonResponse> GetPerson(int personId, CancellationToken? cancellationToken = null);
|
||||
Task<Film> GetSingleFilm(int filmId, CancellationToken? cancellationToken = null);
|
||||
Task<ICollection<StaffResponse>> GetStaff(int filmId, CancellationToken? cancellationToken = null);
|
||||
Task<VideoResponse> GetTrailers(int filmId, CancellationToken? cancellationToken = null);
|
||||
Task<FilmSearchResponse> SearchByKeyword(string keyword, int page = 1, CancellationToken? cancellationToken = null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace KinopoiskUnofficialInfo.ApiClient
|
||||
{
|
||||
public class JsonProfessionKeyConverter : StringEnumConverter
|
||||
{
|
||||
public override object ReadJson(JsonReader reader, System.Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
try
|
||||
{
|
||||
return base.ReadJson(reader, objectType, existingValue, serializer);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return PersonResponse_filmsProfessionKey.UNKNOWN;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace KinopoiskUnofficialInfo.ApiClient
|
||||
{
|
||||
internal static class JsonTransformator
|
||||
{
|
||||
public static JsonSerializerSettings TransformSettings(
|
||||
JsonSerializerSettings settings)
|
||||
{
|
||||
// Опубликованный openapi.json малость кривоват, многое помечено required, но возвращается null
|
||||
// Либа генерации прокси - тоже малость кривовата, т.к. для string считает явно указанный null
|
||||
// недопустимым, независимо от required для данного поля.
|
||||
// С задекларированным составом перечислений тоже беда
|
||||
// В общем, приходится патчить контракты на лету.
|
||||
settings.ContractResolver = new DeclarationPatcherContractResolver();
|
||||
|
||||
return settings;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
<PackageReference Include="NSwag.MSBuild" Version="13.11.1">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
@@ -22,10 +24,10 @@
|
||||
</OpenApiReference>
|
||||
</ItemGroup> -->
|
||||
|
||||
<Target Name="GenerateApiClientSourceCode" BeforeTargets="CoreCompile;PrepareResource">
|
||||
<Exec Command="$(NSwagExe_Net50) openapi2csclient /runtime:Net50 /input:https://kinopoiskapiunofficial.tech/documentation/api/openapi.json /namespace:KinopoiskUnofficialInfo /output:$(IntermediateOutputPath)\KinopoiskApiClient.generated.cs" />
|
||||
<Target Name="GenerateApiClientSourceCode">
|
||||
<Exec Command="$(NSwagExe_Net50) openapi2csclient /runtime:Net50 /input:https://kinopoiskapiunofficial.tech/documentation/api/openapi.json /namespace:KinopoiskUnofficialInfo.ApiClient /JsonSerializerSettingsTransformationMethod:KinopoiskUnofficialInfo.ApiClient.JsonTransformator.TransformSettings /output:GeneratedClient.generated.cs" />
|
||||
<ItemGroup>
|
||||
<Compile Include="$(IntermediateOutputPath)\KinopoiskApiClient.generated.cs" />
|
||||
<Compile Include="GeneratedClient.generated.cs" />
|
||||
</ItemGroup>
|
||||
</Target>
|
||||
</Project>
|
||||
Reference in new issue
Block a user