コード例 #1
0
        public ApiDocument GetApi()
        {
            var actionGroups = _apiDescriptionsProvider.ApiDescriptionGroups.Items;


            var dapi = new ApiDocument
            {
                ApiName     = _options.Value.ApiOptions.ApiName,
                Version     = _options.Value.ApiOptions.APiVersion,
                Copyright   = _options.Value.ApiOptions.Copyright,
                NetworkDocs = _options.Value.ApiOptions.NetworkDocs
            };

            foreach (var actionGroup in actionGroups)
            {
                if (actionGroup == null)
                {
                    continue;
                }
                var apiGroup = new ApiGroup {
                    GroupName = actionGroup.GroupName
                };

                foreach (var apiDescription in actionGroup.Items)
                {
                    var dapiInfo      = new DApiInfo();
                    var routeTemplate = apiDescription.RelativePath;

                    //Fix issue
                    if (apiGroup.IsExistApiInfo(apiGroup.DApiInfo, routeTemplate))
                    {
                        continue;
                    }

                    var requestParam  = apiDescription.ParameterDescriptions.FirstOrDefault();
                    var responseParam = apiDescription.SupportedResponseTypes.FirstOrDefault();
                    var requestClass  = requestParam == null
                        ? null
                        : (requestParam.Type == typeof(void) ? null : Activator.CreateInstance(requestParam.Type));
                    var responseClass = responseParam == null
                        ? null
                        : (responseParam?.Type == typeof(void) ? null : Activator.CreateInstance(responseParam?.Type));
                    var methinfo = (apiDescription.ActionDescriptor as ControllerActionDescriptor)?.MethodInfo;

                    dapiInfo.Url           = routeTemplate.ToLower();
                    dapiInfo.ApiName       = routeTemplate.Split('/').Last().ToLower();
                    dapiInfo.ApiDesc       = _xmlFilter.GetFuncDec(methinfo);
                    dapiInfo.RequestProto  = GetProtoFile(requestClass);
                    dapiInfo.ResponseProto = GetProtoFile(responseClass);
                    dapiInfo.Request       = GetParamInfo(requestParam?.Type);
                    dapiInfo.Response      = GetParamInfo(responseParam?.Type);

                    apiGroup.DApiInfo.Add(dapiInfo);
                }
                dapi.ApiGroups.Add(apiGroup);
            }

            return(dapi);
        }
コード例 #2
0
ファイル: ApiMiddleware.cs プロジェクト: EricBlack/CoindaqAPI
 private void RespondWithApiJson(HttpResponse response, ApiDocument swagger)
 {
     response.StatusCode  = 200;
     response.ContentType = "application/json";
     using (var writer = new StreamWriter(response.Body, Encoding.GetEncoding("utf-8")))
     {
         _apiSerializer.Serialize(writer, swagger);
     }
 }