protected void GenerateControllerInternal(ServiceGenInfo srvInfo, CsFileWriter writer) { var srv = srvInfo.Service; writer.Usings.Append("using System.Collections.Generic;").AppendLine(); writer.Usings.Append("using System.Threading.Tasks;").AppendLine(); writer.Usings.Append("using Microsoft.AspNetCore.Http;").AppendLine(); writer.Usings.Append("using Microsoft.AspNetCore.Mvc;").AppendLine(); writer.Usings.Append("using Cybtans.AspNetCore;").AppendLine(); var clsWriter = writer.Class; if (srv.Option.RequiredAuthorization || srv.Option.AllowAnonymous || srv.Rpcs.Any(x => x.Option.RequiredAuthorization || x.Option.AllowAnonymous)) { writer.Usings.Append("using Microsoft.AspNetCore.Authorization;").AppendLine(); } if (srvInfo.Service.Option.Description != null) { clsWriter.Append("/// <summary>").AppendLine(); clsWriter.Append("/// ").Append(srvInfo.Service.Option.Description).AppendLine(); clsWriter.Append("/// </summary>").AppendLine(); clsWriter.Append($"[System.ComponentModel.Description(\"{srvInfo.Service.Option.Description}\")]").AppendLine(); } AddAutorizationAttribute(srv.Option, clsWriter); clsWriter.Append($"[Route(\"{srv.Option.Prefix}\")]").AppendLine(); clsWriter.Append("[ApiController]").AppendLine(); clsWriter.Append($"public partial class {srvInfo.Name}Controller : ControllerBase").AppendLine(); clsWriter.Append("{").AppendLine(); clsWriter.Append('\t', 1); var bodyWriter = clsWriter.Block("BODY"); bodyWriter.Append($"private readonly I{srvInfo.Name} _service;").AppendLine().AppendLine(); #region Constructor bodyWriter.Append($"public {srvInfo.Name}Controller(I{srvInfo.Name} service)").AppendLine(); bodyWriter.Append("{").AppendLine(); bodyWriter.Append('\t', 1).Append("_service = service;").AppendLine(); bodyWriter.Append("}").AppendLine(); #endregion foreach (var rpc in srv.Rpcs) { var options = rpc.Option; var request = rpc.RequestType; var response = rpc.ResponseType; var rpcName = _serviceGenerator.GetRpcName(rpc); string template = options.Template != null ? $"(\"{options.Template}\")" : ""; bodyWriter.AppendLine(); if (rpc.Option.Description != null) { bodyWriter.Append("/// <summary>").AppendLine(); bodyWriter.Append("/// ").Append(rpc.Option.Description).AppendLine(); bodyWriter.Append("/// </summary>").AppendLine(); bodyWriter.Append($"[System.ComponentModel.Description(\"{rpc.Option.Description}\")]").AppendLine(); } AddAutorizationAttribute(options, bodyWriter); AddRequestMethod(bodyWriter, options, template); bodyWriter.AppendLine(); if (request.HasStreams()) { bodyWriter.Append("[DisableFormValueModelBinding]").AppendLine(); } bodyWriter.Append($"public {response.GetControllerReturnTypeName()} {rpcName}").Append("("); var parametersWriter = bodyWriter.Block($"PARAMS_{rpc.Name}"); bodyWriter.Append($"{GetRequestBinding(options.Method, request)}{request.GetRequestTypeName("__request")})").AppendLine() .Append("{").AppendLine() .Append('\t', 1); var methodWriter = bodyWriter.Block($"METHODBODY_{rpc.Name}"); bodyWriter.AppendLine().Append("}").AppendLine(); if (options.Template != null) { var path = request is MessageDeclaration?_typeGenerator.GetMessageInfo(request).GetPathBinding(options.Template) : null; if (path != null) { foreach (var field in path) { parametersWriter.Append($"{field.Type} {field.Field.Name}, "); methodWriter.Append($"__request.{field.Name} = {field.Field.Name};").AppendLine(); } } } if (response.HasStreams()) { methodWriter.Append($"var result = await _service.{rpcName}({(request != PrimitiveType.Void ? "__request" : "")});").AppendLine(); var result = "result"; var contentType = $"\"{options.StreamOptions?.ContentType ?? "application/octet-stream"}\""; var fileName = options.StreamOptions?.Name; fileName = fileName != null ? $"\"{fileName}\"" : "Guid.NewGuid().ToString()"; if (response is MessageDeclaration responseMsg) { var name = responseMsg.Fields.FirstOrDefault(x => x.FieldType == PrimitiveType.String && x.Name.EndsWith("Name")); var type = responseMsg.Fields.FirstOrDefault(x => x.FieldType == PrimitiveType.String && x.Name.EndsWith("Type")); if (name != null) { fileName = $"result.{name.GetFieldName()}"; } if (type != null) { contentType = $"result.{type.GetFieldName()}"; } methodWriter.AppendTemplate(streamReturnTemplate, new Dictionary <string, object>()) .AppendLine(); var stream = responseMsg.Fields.FirstOrDefault(x => x.FieldType == PrimitiveType.Stream); if (stream != null) { result = $"result.{stream.GetFieldName()}"; } } methodWriter.Append($"return new FileStreamResult({result}, {contentType}) {{ FileDownloadName = {fileName} }};"); } else { methodWriter.Append($"return _service.{rpcName}({(request != PrimitiveType.Void ? "__request" : "")});"); } } clsWriter.Append("}").AppendLine(); writer.Save($"{srvInfo.Name}Controller"); }
private void GenerateClient(ServiceGenInfo info) { var writer = CreateWriter(Namespace); writer.Usings.Append($"using Refit;").AppendLine(); writer.Usings.Append($"using Cybtans.Refit;").AppendLine(); writer.Usings.Append($"using System.Net.Http;").AppendLine(); writer.Usings.Append($"using System.Threading.Tasks;").AppendLine(); writer.Usings.Append($"using {_typeGenerator.Namespace};").AppendLine(); var clsWriter = writer.Class; if (info.Service.Option.Description != null) { clsWriter.Append("/// <summary>").AppendLine(); clsWriter.Append("/// ").Append(info.Service.Option.Description).AppendLine(); clsWriter.Append("/// </summary>").AppendLine(); } clsWriter.Append("[ApiClient]").AppendLine(); clsWriter.Append($"public interface I{info.Name}").AppendLine(); clsWriter.Append("{").AppendLine(); clsWriter.Append('\t', 1); var bodyWriter = clsWriter.Block("BODY"); var srv = info.Service; foreach (var rpc in srv.Rpcs) { var options = rpc.Option; var request = rpc.RequestType; var response = rpc.ResponseType; var rpcName = _serviceGenerator.GetRpcName(rpc); string url = $"/{srv.Option.Prefix}"; List <MessageFieldInfo>?path = null; if (options.Template != null) { var template = options.Template; path = request is MessageDeclaration?_typeGenerator.GetMessageInfo(request).GetPathBinding(template) : null; if (path != null) { foreach (var field in path) { template = template.Replace($"{{{field.Field.Name}}}", $"{{request.{field.Name}}}"); } } url = $"/{srv.Option.Prefix}/{ template}"; } bodyWriter.AppendLine(); if (rpc.Option.Description != null) { bodyWriter.Append("/// <summary>").AppendLine(); bodyWriter.Append("/// ").Append(rpc.Option.Description).AppendLine(); bodyWriter.Append("/// </summary>").AppendLine(); } if (srv.Option.RequiredAuthorization || options.RequiredAuthorization) { bodyWriter.Append("[Headers(\"Authorization: Bearer\")]").AppendLine(); } string optional = ""; switch (options.Method) { case "GET": bodyWriter.Append($"[Get(\"{url}\")]"); if ((path == null || path.Count == 0) && request != PrimitiveType.Void) { optional = " = null"; } break; case "POST": bodyWriter.Append($"[Post(\"{url}\")]"); break; case "PUT": bodyWriter.Append($"[Put(\"{url}\")]"); break; case "DELETE": bodyWriter.Append($"[Delete(\"{url}\")]"); break; } bodyWriter.AppendLine(); bodyWriter.Append($"{response.GetReturnTypeName()} {rpcName}({GetRequestBinding(options.Method)}{request.GetRequestTypeName("request")}{optional});").AppendLine(); } clsWriter.AppendLine().Append("}").AppendLine(); writer.Save($"I{info.Name}"); }