Commit 46e75796 authored by Alessio Parma's avatar Alessio Parma 🐔

rapid api client

parent 81322d15
......@@ -46,8 +46,8 @@ public sealed class ConcreteThumbnailerClient : IThumbnailerClient
{
private const string ApiKeyHeaderName = "X-Api-Key";
private const string OptimizationModeParamName = "mode";
private static readonly HashSet<int> s_retriableStatusCodes = new HashSet<int> { 424 };
private const string RapidApiKeyHeaderName = "X-RapidAPI-Key";
private static readonly HashSet<int> s_retriableStatusCodes = new HashSet<int> { 424, 429 };
private readonly IOptions<ThumbnailerClientConfiguration> _clientConfiguration;
private readonly AsyncRetryPolicy _retryPolicy;
......@@ -75,6 +75,8 @@ public sealed class ConcreteThumbnailerClient : IThumbnailerClient
.WaitAndRetryAsync(clientConfiguration.Value.RetryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
}
private bool IsRapidApi => _clientConfiguration.Value.BaseUri.Host.EndsWith(".rapidapi.com", StringComparison.OrdinalIgnoreCase);
/// <inheritdoc/>
public async Task<byte[]> GetThumbnailAsync(
Uri fileUri,
......@@ -84,10 +86,12 @@ public sealed class ConcreteThumbnailerClient : IThumbnailerClient
{
_validator.ValidateFileUri(fileUri);
var isRapidApi = IsRapidApi;
return await EvaluateResponseAsync(OperationType.ThumbnailGeneration, async () => await Url
.Combine(_clientConfiguration.Value.BaseUri.AbsoluteUri, "api/v1/thumbnail")
.Combine(_clientConfiguration.Value.BaseUri.AbsoluteUri, $"{(isRapidApi ? string.Empty : "api/")}v1/thumbnail")
.WithTimeout(_clientConfiguration.Value.Timeout)
.WithHeader(ApiKeyHeaderName, _clientConfiguration.Value.ApiKey)
.WithHeader(isRapidApi ? RapidApiKeyHeaderName : ApiKeyHeaderName, _clientConfiguration.Value.ApiKey)
.SetQueryParam(nameof(fileUri), fileUri.AbsoluteUri)
.SetQueryParam(nameof(widthPx), widthPx.ToString(CultureInfo.InvariantCulture))
.SetQueryParam(nameof(heightPx), heightPx.ToString(CultureInfo.InvariantCulture))
......@@ -110,11 +114,12 @@ public sealed class ConcreteThumbnailerClient : IThumbnailerClient
_validator.ValidateContentTypeForThumbnailGeneration(contentType, @throw: true);
fileName = AdjustFileName(contentType, fileName);
var isRapidApi = IsRapidApi;
return await EvaluateResponseAsync(OperationType.ThumbnailGeneration, async () => await Url
.Combine(_clientConfiguration.Value.BaseUri.AbsoluteUri, "api/v1/thumbnail")
.Combine(_clientConfiguration.Value.BaseUri.AbsoluteUri, $"{(isRapidApi ? string.Empty : "api/")}v1/thumbnail")
.WithTimeout(_clientConfiguration.Value.Timeout)
.WithHeader(ApiKeyHeaderName, _clientConfiguration.Value.ApiKey)
.WithHeader(isRapidApi ? RapidApiKeyHeaderName : ApiKeyHeaderName, _clientConfiguration.Value.ApiKey)
.SetQueryParam(nameof(widthPx), widthPx.ToString(CultureInfo.InvariantCulture))
.SetQueryParam(nameof(heightPx), heightPx.ToString(CultureInfo.InvariantCulture))
.SetQueryParam(nameof(shavePx), shavePx.ToString(CultureInfo.InvariantCulture))
......@@ -145,10 +150,12 @@ public Task<bool> IsThumbnailGenerationSupportedAsync(string contentType)
{
_validator.ValidateFileUri(fileUri);
var isRapidApi = IsRapidApi;
return await EvaluateResponseAsync(OperationType.ImageOptimization, async () => await Url
.Combine(_clientConfiguration.Value.BaseUri.AbsoluteUri, "api/v1/optimize")
.Combine(_clientConfiguration.Value.BaseUri.AbsoluteUri, $"{(isRapidApi ? string.Empty : "api/")}v1/optimize")
.WithTimeout(_clientConfiguration.Value.Timeout)
.WithHeader(ApiKeyHeaderName, _clientConfiguration.Value.ApiKey)
.WithHeader(isRapidApi ? RapidApiKeyHeaderName : ApiKeyHeaderName, _clientConfiguration.Value.ApiKey)
.SetQueryParam(nameof(fileUri), fileUri.AbsoluteUri)
.SetQueryParam(OptimizationModeParamName, mode.ToString())
.GetBytesAsync(cancellationToken)
......@@ -167,11 +174,12 @@ public Task<bool> IsThumbnailGenerationSupportedAsync(string contentType)
_validator.ValidateContentTypeForImageOptimization(contentType, @throw: true);
fileName = AdjustFileName(contentType, fileName);
var isRapidApi = IsRapidApi;
return await EvaluateResponseAsync(OperationType.ImageOptimization, async () => await Url
.Combine(_clientConfiguration.Value.BaseUri.AbsoluteUri, "api/v1/optimize")
.Combine(_clientConfiguration.Value.BaseUri.AbsoluteUri, $"{(isRapidApi ? string.Empty : "api/")}v1/optimize")
.WithTimeout(_clientConfiguration.Value.Timeout)
.WithHeader(ApiKeyHeaderName, _clientConfiguration.Value.ApiKey)
.WithHeader(isRapidApi ? RapidApiKeyHeaderName : ApiKeyHeaderName, _clientConfiguration.Value.ApiKey)
.SetQueryParam(OptimizationModeParamName, mode.ToString())
.PostMultipartAsync(mp => mp.AddFile("file", new MemoryStream(contents), fileName, contentType), cancellationToken)
.ReceiveBytes()
......
......@@ -22,10 +22,10 @@
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" PrivateAssets="All" />
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.76" PrivateAssets="All" />
<PackageReference Include="NWebsec.AspNetCore.Middleware" Version="3.0.0" />
<PackageReference Include="PommaLabs.KVLite.AspNetCore" Version="10.0.1" />
<PackageReference Include="PommaLabs.KVLite.Memory" Version="10.0.1" />
<PackageReference Include="PommaLabs.KVLite.PostgreSql" Version="10.0.1" />
<PackageReference Include="PommaLabs.KVLite.SqlServer" Version="10.0.1" />
<PackageReference Include="PommaLabs.KVLite.AspNetCore" Version="10.0.2" />
<PackageReference Include="PommaLabs.KVLite.Memory" Version="10.0.2" />
<PackageReference Include="PommaLabs.KVLite.PostgreSql" Version="10.0.2" />
<PackageReference Include="PommaLabs.KVLite.SqlServer" Version="10.0.2" />
<PackageReference Include="reCAPTCHA.AspNetCore" Version="3.0.3" />
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
<PackageReference Include="Serilog.Filters.Expressions" Version="2.1.0" />
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment