astral-api/Astral.ApiServer/Controllers/BaseApiController.cs

98 lines
3 KiB
C#

// <copyright file="BaseApiController.cs" company="alveus.dev">
// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License.
// </copyright>
using System.Net;
using System.Net.Sockets;
using Astral.ApiServer.Models.Common;
using Astral.Core.Constants;
using Microsoft.AspNetCore.Mvc;
namespace Astral.ApiServer.Controllers;
/// <summary>
/// Base API controller with common methods.
/// </summary>
[Produces("application/json")]
[Consumes("application/x-www-form-urlencoded")]
public class BaseApiController : ControllerBase
{
/// <summary>
/// Return a failure status with no data.
/// </summary>
/// <returns>Instance of <see cref="IActionResult"/>.</returns>
protected IActionResult FailureResult()
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return new JsonResult(new ResultModel
{
Success = false,
});
}
/// <summary>
/// Return a failure result with additional information.
/// </summary>
/// <param name="errorCode">Api error code.</param>
/// <param name="message">Api error message.</param>
/// <returns>Instance of <see cref="IActionResult"/>.</returns>
protected IActionResult FailureResult(string errorCode, string message)
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return new JsonResult(new ErrorResultModel
{
Error = errorCode,
Message = message,
});
}
/// <summary>
/// Returns a failure result indicating the body is missing from the request.
/// </summary>
/// <returns>Instance of <see cref="IActionResult"/>.</returns>
protected IActionResult MissingBodyResult()
{
return FailureResult(CoreErrorCodes.NoDataProvided, "Missing request body");
}
/// <summary>
/// Return a success status with no data.
/// </summary>
/// <returns>Instance of <see cref="IActionResult"/>.</returns>
protected IActionResult SuccessResult()
{
Response.StatusCode = (int)HttpStatusCode.OK;
return new JsonResult(new ResultModel
{
Success = true,
});
}
/// <summary>
/// Fetch IP address of requesting agent.
/// </summary>
/// <returns>Request origin's IP address.</returns>
protected IPAddress ClientIpAddress()
{
IPAddress remoteIpAddress = null;
if (Request.Headers.TryGetValue("X-Forwarded-For", out var value))
{
foreach (var ip in value)
{
if (IPAddress.TryParse(ip, out var address) &&
(address.AddressFamily is AddressFamily.InterNetwork
or AddressFamily.InterNetworkV6))
{
remoteIpAddress = address;
break;
}
}
}
else
{
remoteIpAddress = HttpContext.Connection.RemoteIpAddress?.MapToIPv4();
}
return remoteIpAddress;
}
}