예제 #1
0
        public OpenApiDocument GetSwagger(string documentName, string?host = null, string?basePath = null)
        {
            if (this.cacheDocument != null)
            {
                return(this.cacheDocument);
            }

            var schemaRepository = new SchemaRepository();
            var methodProvider   =
                this.scopeFactory.CreateScope().ServiceProvider.GetRequiredService <IRpcMethodProvider>();
            RpcRouteMetaData metaData = methodProvider.Get();
            OpenApiPaths     paths    = this.GetOpenApiPaths(metaData, schemaRepository);

            this.cacheDocument = new OpenApiDocument()
            {
                Info = new OpenApiInfo()
                {
                    Title   = Assembly.GetEntryAssembly().GetName().Name,
                    Version = "v1"
                },
                Servers = this.swagerOptions.Endpoints.Select(x => new OpenApiServer()
                {
                    Url = x
                }).ToList(),
                Components = new OpenApiComponents()
                {
                    Schemas = schemaRepository.Schemas
                },
                Paths = paths
            };

            return(this.cacheDocument);
        }
예제 #2
0
        private List <UniqueMethod> GetUniqueKeyMethodPairs(RpcRouteMetaData metaData)
        {
            List <UniqueMethod> methodList = this.Convert(metaData.BaseRoute, path: null).ToList();

            foreach ((RpcPath path, IReadOnlyList <IRpcMethodInfo> pathRoutes) in metaData.PathRoutes)
            {
                methodList.AddRange(this.Convert(pathRoutes, path));
            }

            return(methodList);
        }
예제 #3
0
        /// <summary>
        /// Extension method to use the JsonRpc router in the Asp.Net pipeline
        /// </summary>
        /// <param name="app"><see cref="IApplicationBuilder"/> that is supplied by Asp.Net</param>
        /// <param name="builder">(Optional) Action to configure the endpoints. Will default to include all controllers that derive from `RpcController`</param>
        /// <returns><see cref="IApplicationBuilder"/> that includes the Basic auth middleware</returns>
        public static IApplicationBuilder UseJsonRpc(this IApplicationBuilder app, Action <RpcEndpointBuilder>?builder = null)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            var options = new RpcEndpointBuilder();

            builder = builder ?? BuildFromBaseController <RpcController>;
            builder.Invoke(options);
            RpcRouteMetaData data = options.Resolve();

            return(app.UseJsonRpc(data));
        }
예제 #4
0
        /// <summary>
        /// Extension method to use the JsonRpc router in the Asp.Net pipeline
        /// </summary>
        /// <param name="app"><see cref="IApplicationBuilder"/> that is supplied by Asp.Net</param>
        /// <param name="methodProvider">All the available methods to call</param>
        /// <returns><see cref="IApplicationBuilder"/> that includes the Basic auth middleware</returns>
        internal static IApplicationBuilder UseJsonRpc(this IApplicationBuilder app, RpcRouteMetaData data)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (app.ApplicationServices.GetService <RpcServicesMarker>() == null)
            {
                throw new InvalidOperationException("AddJsonRpc() needs to be called in the ConfigureServices method.");
            }
            var router = new RpcHttpRouter();

            app.ApplicationServices.GetRequiredService <StaticRpcMethodDataAccessor>().Value = data;
            return(app.UseRouter(router));
        }
예제 #5
0
        private OpenApiPaths GetOpenApiPaths(RpcRouteMetaData metaData, SchemaRepository schemaRepository)
        {
            OpenApiPaths paths = new OpenApiPaths();

            List <UniqueMethod> uniqueMethods = this.GetUniqueKeyMethodPairs(metaData);

            foreach (UniqueMethod method in uniqueMethods)
            {
                string           operationKey = method.UniqueUrl.Replace("/", "_").Replace("#", "|");
                OpenApiOperation operation    = this.GetOpenApiOperation(operationKey, method.Info, schemaRepository);

                var pathItem = new OpenApiPathItem()
                {
                    Operations = new Dictionary <OperationType, OpenApiOperation>()
                    {
                        [OperationType.Post] = operation
                    }
                };
                paths.Add(method.UniqueUrl, pathItem);
            }

            return(paths);
        }