2016-10-06 2 views
0

에 의해 API 년대를 분할 AutoRest을 얻는 방법 :지금 내가 사용하고 컨트롤러

AutoRest\AutoRest.exe -Input %jsonUrl% -Namespace %projectName%ClientAutoGen -OutputDirectory %projectName%Client 

ASP.NET CoreRest Client를 생성하려면.

두통은 AutoRestAPIcontrollers에서의 모든 단일 file/class을 생성한다는 것이다. 나는 각 controller을 자신의 file/class으로 나눌 수있는 pre-ASP.NET Coreauto-generators을 사용했습니다. AutoRest에서이 동작을 강제하는 방법이 있습니까?

답변

0

GitHub의 AutoRest 팀 (여기 : https://github.com/Azure/autorest/issues/1497)이 도움이되는 답변에 따르면 답변은을 OperationId에서 사용하여 분할을 발생시키는 것입니다.

public class SwashbuckleOperationFilter : IOperationFilter 
{ 
    public void Apply(Operation operation, OperationFilterContext context) 
    { 
     try 
     { 
      var pathSegments = context.ApiDescription.RelativePath.Split(new[] { '/' }).ToList(); 

      var version = string.Empty; 
      var controller = string.Empty; 
      if (pathSegments.Count > 1) 
      { 
       version = pathSegments[0]; 
       controller = pathSegments[1] + "_"; 

       pathSegments = pathSegments.Skip(2).Where(x => !x.Contains("{")).ToList(); 
      } 

      string httpMethod = FirstCharToUpper(context.ApiDescription.HttpMethod); 
      var routeName = context.ApiDescription.ActionDescriptor?.AttributeRouteInfo?.Name ?? string.Empty; 

      operation.OperationId = $"{version}{controller}{httpMethod}{string.Join("", pathSegments)}{routeName}"; 
     } 
     catch (Exception ex) 
     { 
      throw new Exception("Are you missing the [Route(\"v1/[controller]/\")] on your Controller?", ex); 
     } 
    } 

    private string FirstCharToUpper(string input) 
    { 
     if (String.IsNullOrEmpty(input)) 
      return string.Empty; 

     input = input.Trim().ToLower(); 

     return input.First().ToString().ToUpper() + new string(input.Skip(1).ToArray()); 
    } 
} 

StartUp에서 다음과 같이 사용합니다 : service method를 생성 이런 종류의 속으로

[Route("v1/[controller]/")] 
public class ThingController : Controller 
{ 
    [HttpGet("ById/{id}")] 
    [Produces(typeof(ThingDTO))] 
    public async Task<IActionResult> GetThing([FromRoute] long id) 
    { 
     // Your implementation 
    } 
} 

:

services.AddSwaggerGen(options => 
{ 
    options.OperationFilter<SwashbuckleOperationFilter>(); 
    //... 
} 

이 같은 API Controller Method을 켜려면이는 Filter의 내 버전입니다 :

public class ThingClient : IThingClient 
{ 
    private readonly AppSettings appSettings; 
    private readonly IMapper mapper; 
    private IV1Thing api; 

    public ThingClient(IOptions<AppSettings> appSettingsOptions, IMapper mapper) 
    { 
     appSettings = appSettingsOptions.Value; 
     this.mapper = mapper; 
    } 

    private IV1Thing service => api ?? 
       (api = new V1Thing(new ThingService(new Uri(appSettings.URLs.ThingService)))); 

    public async Task<IThing> GetByIdAsync(long thingId) 
    { 
     return mapper.Map<IThing>(await service.GetByIdAsync(thingId)); 
    } 
} 
관련 문제