SMS služba: prostředník nad SMS-Služba.cz (API XML 3.0)
- JSON API /v1: odeslání SMS, doručenka, události, potvrzení událostí, kredit - vlastní API klíč pro každou aplikaci (X-Api-Key, API_KEYS) - po chybě přihlášení k bráně se volání zastaví, aby se účet nezablokoval - heslo maskované v logu, odeslání se nikdy neopakuje automaticky - /health, /docs a OpenAPI s prefixem z ROOT_PATH - .NET 10, šablona .NET 8 nahrazena projektem v src/, testy v tests/ - dokumentace: návrh, stav a návod pro napojení aplikací
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
using SmsSluzba.Api.Gateway;
|
||||
using SmsSluzba.Api.Problems;
|
||||
|
||||
namespace SmsSluzba.Api.Endpoints;
|
||||
|
||||
internal static class GatewayProblems
|
||||
{
|
||||
private const string UnknownStateDetail = "Stav není známý, brána mohla požadavek přijmout. Odeslání SMS neopakujte automaticky.";
|
||||
|
||||
public static ApiError ErrorFor(GatewayErrorKind kind) => kind switch
|
||||
{
|
||||
GatewayErrorKind.InvalidRecipient => ApiErrors.InvalidRecipient,
|
||||
GatewayErrorKind.MissingText => ApiErrors.MissingText,
|
||||
GatewayErrorKind.Rejected => ApiErrors.ProviderRejected,
|
||||
GatewayErrorKind.AuthFailed or GatewayErrorKind.AuthBlocked => ApiErrors.ProviderAuthBlocked,
|
||||
GatewayErrorKind.InsufficientCredit => ApiErrors.InsufficientCredit,
|
||||
GatewayErrorKind.Timeout => ApiErrors.ProviderTimeout,
|
||||
_ => ApiErrors.ProviderUnavailable,
|
||||
};
|
||||
|
||||
public static IResult ToResult(GatewayException ex)
|
||||
{
|
||||
var error = ErrorFor(ex.Kind);
|
||||
var detail = ex.ProviderMessage ?? (error.Status is 502 or 504 ? UnknownStateDetail : null);
|
||||
return ApiProblems.Result(error, detail, ex.ProviderCode);
|
||||
}
|
||||
|
||||
/// <summary>Runs a gateway call and turns a gateway failure into a problem response.</summary>
|
||||
public static async Task<IResult> RunAsync(Func<Task<IResult>> call)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await call();
|
||||
}
|
||||
catch (GatewayException ex)
|
||||
{
|
||||
// Already logged by SmsGatewayClient.
|
||||
return ToResult(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user