ApiAipController.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using AipGateway.AIP;
  2. using AipGateway.API.Application;
  3. using AipGateway.API.Application.Interfaces.Services;
  4. using AipGateway.API.Domain.Models.Response;
  5. using AipGateway.API.Services;
  6. using AipGateway.API.Services.Interfaces;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Swashbuckle.AspNetCore.Annotations;
  9. using System.ComponentModel.DataAnnotations;
  10. namespace AipGateway.API.Controllers
  11. {
  12. [ApiController]
  13. [Route("/api/v1/aip")]
  14. [Produces("application/json")]
  15. public class ApiAipController : BaseController
  16. {
  17. private readonly ILogger<ApiAipController> _log;
  18. private readonly IApiAipService _service;
  19. private readonly IApiAuthService _authService;
  20. public ApiAipController(ILogger<ApiAipController> log, IApiAuthService authService, IApiAipService apiAipService)
  21. {
  22. _log = log;
  23. _authService = authService;
  24. _service = apiAipService;
  25. }
  26. [HttpPost("download")]
  27. [SwaggerResponse(200, type: typeof(ApiResponseModel<GeneralResponse>))]
  28. public async Task<IResult> DownloadAipInfo([Required] string apiKey)
  29. {
  30. return await CreateResponseAsync(async () =>
  31. {
  32. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_AIP_DOWNLOAD, apiKey, null);
  33. int authError = _authService.CheckApiKeyValidation(HttpContext, apiKey, GlobalConstants.API_AIP_DOWNLOAD);
  34. if (authError != 0)
  35. {
  36. throw ResponseApiKeyValidationError(HttpContext, authError);
  37. }
  38. var response = await _service.DownloadAipInfo();
  39. var result = Results.Ok(ResponseSuccess(HttpContext, response));
  40. HttpContext.Items[GlobalConstants.API_RESULT] = result;
  41. return result;
  42. });
  43. }
  44. [HttpGet("labels")]
  45. [SwaggerResponse(200, type: typeof(ApiResponseModel<List<AipLabel>>))]
  46. public async Task<IResult> GetAipLabels([Required] string apiKey)
  47. {
  48. return await CreateResponseAsync(async () =>
  49. {
  50. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_AIP_LABELS, apiKey, null);
  51. int authError = _authService.CheckApiKeyValidation(HttpContext, apiKey, GlobalConstants.API_AIP_LABELS);
  52. if (authError != 0)
  53. {
  54. throw ResponseApiKeyValidationError(HttpContext, authError);
  55. }
  56. var response = await _service.GetLabels();
  57. var result = Results.Ok(new ApiResponseModel<List<AipLabel>>()
  58. {
  59. success = true,
  60. errorCode = 0,
  61. errorMessage = GlobalConstants.API_RESULT_SUCCESS,
  62. result = response,
  63. });
  64. HttpContext.Items[GlobalConstants.API_RESULT_CODE] = 0;
  65. HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS;
  66. HttpContext.Items[GlobalConstants.API_RESULT] = response;
  67. return result;
  68. });
  69. }
  70. [HttpGet("policies")]
  71. [SwaggerResponse(200, type: typeof(ApiResponseModel<List<AipLabel>>))]
  72. public async Task<IResult> GetAipPolicies([Required] string apiKey)
  73. {
  74. return await CreateResponseAsync(async () =>
  75. {
  76. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_AIP_POLICIES, apiKey, null);
  77. int authError = _authService.CheckApiKeyValidation(HttpContext, apiKey, GlobalConstants.API_AIP_POLICIES);
  78. if (authError != 0)
  79. {
  80. throw ResponseApiKeyValidationError(HttpContext, authError);
  81. }
  82. var response = await _service.GetPolicies();
  83. var result = Results.Ok(new ApiResponseModel<List<AipLabel>>()
  84. {
  85. success = true,
  86. errorCode = 0,
  87. errorMessage = GlobalConstants.API_RESULT_SUCCESS,
  88. result = response,
  89. });
  90. HttpContext.Items[GlobalConstants.API_RESULT_CODE] = 0;
  91. HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS;
  92. HttpContext.Items[GlobalConstants.API_RESULT] = result;
  93. return result;
  94. });
  95. }
  96. [HttpGet("protections")]
  97. [SwaggerResponse(200, type: typeof(ApiResponseModel<List<AipTemplate>>))]
  98. public async Task<IResult> GetAipProtections([Required] string apiKey)
  99. {
  100. return await CreateResponseAsync(async () =>
  101. {
  102. GlobalConstants.SetAuthorization(HttpContext, GlobalConstants.API_AIP_PROTECTIONS, apiKey, null);
  103. int authError = _authService.CheckApiKeyValidation(HttpContext, apiKey, GlobalConstants.API_AIP_PROTECTIONS);
  104. if (authError != 0)
  105. {
  106. throw ResponseApiKeyValidationError(HttpContext, authError);
  107. }
  108. var response = await _service.GetProtections();
  109. var result = Results.Ok(new ApiResponseModel<List<AipTemplate>>()
  110. {
  111. success = true,
  112. errorCode = 0,
  113. errorMessage = GlobalConstants.API_RESULT_SUCCESS,
  114. result = response,
  115. });
  116. HttpContext.Items[GlobalConstants.API_RESULT_CODE] = 0;
  117. HttpContext.Items[GlobalConstants.API_RESULT_MESSAGE] = GlobalConstants.API_RESULT_SUCCESS;
  118. HttpContext.Items[GlobalConstants.API_RESULT] = result;
  119. return result;
  120. });
  121. }
  122. }
  123. }