forked from mtusi2026/mtusi2026lab3
Реализовать лабораторную работу 3
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using Messenger.Api.Contracts;
|
||||
|
||||
namespace Messenger.Api.Services;
|
||||
|
||||
public static class ApiSupport
|
||||
{
|
||||
public static async Task<(T? Value, IResult? Error)> ReadJsonAsync<T>(HttpContext context)
|
||||
{
|
||||
if (!context.Request.HasJsonContentType())
|
||||
return (default, ApiError(context, 400, "invalid_request", "Требуется application/json"));
|
||||
|
||||
try
|
||||
{
|
||||
var value = await context.Request.ReadFromJsonAsync<T>();
|
||||
return value is null
|
||||
? (default, ApiError(context, 400, "invalid_request", "Тело запроса обязательно"))
|
||||
: (value, null);
|
||||
}
|
||||
catch (Exception exception) when (exception is JsonException or NotSupportedException or BadHttpRequestException)
|
||||
{
|
||||
return (default, ApiError(context, 400, "invalid_request", "Некорректный JSON"));
|
||||
}
|
||||
}
|
||||
|
||||
public static IResult Unauthorized(HttpContext context, bool basic = false)
|
||||
{
|
||||
context.Response.Headers["WWW-Authenticate"] = basic
|
||||
? "Basic realm=\"messenger\", charset=\"UTF-8\""
|
||||
: "Bearer realm=\"messenger\"";
|
||||
return ApiError(context, 401, "unauthorized", "Требуется действительная сессия");
|
||||
}
|
||||
|
||||
public static (ChatCursor? Cursor, DateTime? Time, IResult? Error) ParseCursor(
|
||||
HttpContext context, string scope)
|
||||
{
|
||||
if (!context.Request.Query.TryGetValue("cursor", out var value))
|
||||
return (null, null, null);
|
||||
|
||||
var cursor = CursorService.DecodeChatCursor(value.ToString());
|
||||
if (cursor is null || cursor.Scope != scope || cursor.Id == Guid.Empty ||
|
||||
!DateTime.TryParse(cursor.CreatedAt, CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.RoundtripKind, out var time) || time.Kind != DateTimeKind.Utc)
|
||||
return (null, null, ApiError(context, 400, "invalid_request", "Некорректный cursor"));
|
||||
|
||||
return (cursor, time, null);
|
||||
}
|
||||
|
||||
public static (int Limit, IResult? Error) ParseLimit(HttpContext context)
|
||||
{
|
||||
const int defaultLimit = 50;
|
||||
|
||||
if (!context.Request.Query.TryGetValue("limit", out var value))
|
||||
return (defaultLimit, null);
|
||||
|
||||
if (!int.TryParse(value.ToString(), out var limit) ||
|
||||
limit < 1 ||
|
||||
limit > 100)
|
||||
{
|
||||
return (
|
||||
0,
|
||||
ApiError(context, 400, "invalid_request", "limit должен быть от 1 до 100")
|
||||
);
|
||||
}
|
||||
|
||||
return (limit, null);
|
||||
}
|
||||
|
||||
public static IResult ApiError(
|
||||
HttpContext context,
|
||||
int status,
|
||||
string code,
|
||||
string message)
|
||||
{
|
||||
var requestId =
|
||||
context.Items["RequestId"]?.ToString()
|
||||
?? Guid.NewGuid().ToString();
|
||||
|
||||
return Results.Json(
|
||||
new
|
||||
{
|
||||
error = new
|
||||
{
|
||||
code,
|
||||
message,
|
||||
request_id = requestId
|
||||
}
|
||||
},
|
||||
statusCode: status
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user