using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Web.Http; using System.Web.Http.Description; using FHT.Utilities; using Newtonsoft.Json; using Swashbuckle.Swagger; using FHT.Agency.UI.InputModels.Feature.FeatureDefinition; using FHT.Agency.UI.InputModels.Order.FeatureUsages; using FHT.Agency.UI.ViewModels.Feature; using FHT.Management.Interfaces.InputModels; using FHT.Core.UI.ViewModels; using FHT.Statistics.Services.ImplementedInterfaces.InputModels; using FHT.SwaggerConfig; using FHT.DDD; using System.Web.Http.Cors; namespace FHT.Agency.Web.ApiControllers { [RoutePrefix("api/definitions")] [EnableCors(origins: "*", headers: "*", methods: "*")] public class DefinitionsApiController : ApiController { private SwaggerDocument _swaggerDoc; public SwaggerDocument SwaggerDoc { get { if (_swaggerDoc == null) { using (var httpClient = new HttpClient()) { var baseUri = new Uri(CurrentHost()); var swaggerApiPath = new Uri(baseUri, "/swagger/docs/v1"); var settings = new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore }; string res = httpClient.GetAsync(swaggerApiPath).Result.Content.ReadAsStringAsync().Result; _swaggerDoc = JsonConvert.DeserializeObject(res, settings); } } return _swaggerDoc; } } private string CurrentHost() { return Request.RequestUri.Scheme + Uri.SchemeDelimiter + Request.RequestUri.Host + (Request.RequestUri.IsDefaultPort ? "" : ":" + Request.RequestUri.Port); } [HttpGet] [Route("enums")] [ResponseType(typeof(List))] public IHttpActionResult GetEnumTypes() { List allEnumTypes = SwaggerEnumHelper.GetAllEnumTypes(SwaggerDoc); return Ok(allEnumTypes.Select(e => e.ToEnumModel()).ToList()); } /// /// 暴露一堆的Model,不要调用 /// /// [HttpGet] [Route("misc")] [ResponseType(typeof(ExposeDefinitionsViewModel))] public IHttpActionResult ExposeDefinitions() { return BadRequest("You should never call me as this endpoint is for swagger codegen only"); } } public class ExposeDefinitionsViewModel { public ExposedStatInputModelDefinitions ExposedStatInputModelDefinitions { get; } public ImageSizeViewModelOfSwagger ImageSizeViewModelOfSwagger { get; } public AllSubMenuNavLinkOfSwagger AllSubMenuNavLinkOfSwagger { get; } public AllSubFeatureUsageInputModelForSwagger AllSubFeatureUsageInputModelForSwagger { get; } public AllSubFeatureDefinitionInputModelOfSwagger AllSubFeatureDefinitionInputModelOfSwagger { get; } public AllSubFeatureDefinitionViewModelOfSwagger AllSubFeatureDefinitionViewModelOfSwagger { get; } public ExceptionViewModel ExceptionViewModel { get; set; } } }