cc nasazeni
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
using System.Net;
|
||||
using IdokladSdk.Response;
|
||||
|
||||
namespace Idoklad.Client;
|
||||
|
||||
/// <summary>
|
||||
/// Helpers that unwrap the SDK's <see cref="ApiResult{TData}"/> envelope, returning the payload
|
||||
/// on success and translating failures into <see cref="IdokladApiException"/>.
|
||||
/// </summary>
|
||||
public static class ApiResultExtensions
|
||||
{
|
||||
public static TData Unwrap<TData>(this ApiResult<TData> result)
|
||||
{
|
||||
if (result is null)
|
||||
{
|
||||
throw new IdokladApiException(HttpStatusCode.BadGateway, default, "Empty response from iDoklad API.");
|
||||
}
|
||||
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
var status = result.StatusCode == 0 ? HttpStatusCode.BadGateway : result.StatusCode;
|
||||
throw new IdokladApiException(status, result.ErrorCode, result.Message);
|
||||
}
|
||||
|
||||
return result.Data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using IdokladSdk;
|
||||
using IdokladSdk.Builders;
|
||||
using Idoklad.Configuration;
|
||||
using Idoklad.Credentials;
|
||||
|
||||
namespace Idoklad.Client;
|
||||
|
||||
/// <summary>
|
||||
/// Builds <see cref="DokladApi"/> instances from resolved per-request credentials, using the
|
||||
/// official iDoklad .NET SDK (IdokladSdk) and an <see cref="IHttpClientFactory"/>-managed
|
||||
/// <see cref="HttpClient"/>. This is the iDoklad analogue of microsoft-365-service's graph_client.
|
||||
/// </summary>
|
||||
public sealed class DokladApiFactory
|
||||
{
|
||||
public const string HttpClientName = "IdokladApi";
|
||||
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly IdokladSettings _settings;
|
||||
|
||||
public DokladApiFactory(IHttpClientFactory httpClientFactory, IdokladSettings settings)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
public DokladApi Create(IdokladCredentials credentials)
|
||||
{
|
||||
var httpClient = _httpClientFactory.CreateClient(HttpClientName);
|
||||
|
||||
var builder = new DokladApiBuilder(_settings.AppName, _settings.AppVersion)
|
||||
.AddClientCredentialsAuthentication(credentials.ClientId, credentials.ClientSecret, credentials.ApplicationId)
|
||||
.AddHttpClient(httpClient)
|
||||
.AddApiContextOptions(options => options.Language = credentials.Language);
|
||||
|
||||
if (_settings.HasCustomUrls)
|
||||
{
|
||||
builder = builder.AddCustomApiUrls(_settings.ApiUrl, _settings.IdentityServerUrl);
|
||||
}
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using IdokladSdk;
|
||||
using Idoklad.Credentials;
|
||||
|
||||
namespace Idoklad.Client;
|
||||
|
||||
/// <summary>
|
||||
/// Scoped accessor that resolves the credentials for the current request and lazily builds a
|
||||
/// single <see cref="DokladApi"/> shared by all services handling that request.
|
||||
/// </summary>
|
||||
public sealed class IdokladApiAccessor
|
||||
{
|
||||
private readonly RequestCredentialsProvider _credentialsProvider;
|
||||
private readonly DokladApiFactory _factory;
|
||||
private DokladApi? _api;
|
||||
|
||||
public IdokladApiAccessor(RequestCredentialsProvider credentialsProvider, DokladApiFactory factory)
|
||||
{
|
||||
_credentialsProvider = credentialsProvider;
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
public DokladApi Api => _api ??= _factory.Create(_credentialsProvider.Resolve());
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Net;
|
||||
using IdokladSdk.Enums;
|
||||
|
||||
namespace Idoklad.Client;
|
||||
|
||||
/// <summary>
|
||||
/// Raised when an upstream iDoklad API call does not succeed. Carries the upstream HTTP status
|
||||
/// code, message and iDoklad error code so the failure can be surfaced to the caller.
|
||||
/// </summary>
|
||||
public sealed class IdokladApiException : Exception
|
||||
{
|
||||
public HttpStatusCode StatusCode { get; }
|
||||
public DokladErrorCode ErrorCode { get; }
|
||||
|
||||
public IdokladApiException(HttpStatusCode statusCode, DokladErrorCode errorCode, string? message)
|
||||
: base(string.IsNullOrWhiteSpace(message) ? "iDoklad API request failed." : message)
|
||||
{
|
||||
StatusCode = statusCode;
|
||||
ErrorCode = errorCode;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user