public static IEnumerable <IApiResponseMetadataProvider> GetApiResponses(this EndpointDeclaration info)
        {
            if (info.HandlerDeclaration.ReturnType is not null)
            {
                if (info.HandlerDeclaration.ReturnType == typeof(void))
                {
                    yield return(new EndpointResponseMetaData(201, typeof(void)));
                }
                else if (info.HandlerDeclaration.ReturnType == typeof(string))
                {
                    yield return(new PlainTextEndpointResponseMetaData(200));
                }
                else
                {
                    yield return(new JsonEndpointResponseMetaData(200, info.HandlerDeclaration.ReturnType));
                }
            }

            foreach (var produces in info.Type.GetCustomAttributes <ProducesResponseTypeAttribute>())
            {
                yield return(produces.Type == typeof(void)
                    ? new EndpointResponseMetaData(produces.StatusCode, produces.Type)
                    : new JsonEndpointResponseMetaData(produces.StatusCode, produces.Type));
            }
        }
        private static RequestDelegate BuildDelegate(EndpointDeclaration endpointInfo)
        {
            async Task Request(HttpContext context)
            {
                try
                {
                    var endpointHandler = endpointInfo.HandlerDeclaration.Factory(context);
                    await endpointHandler.HandleRequest().ConfigureAwait(false);
                }
                catch (EndpointNotFoundException)
                {
                    context.Response.StatusCode = 404;
                    await context.Response.WriteAsync("Not found").ConfigureAwait(false);
                }
                catch (MalformedRequestException e)
                {
                    var handler = context.RequestServices.GetRequiredService <IMalformedRequestExceptionHandler>();
                    await handler.HandleMalformedRequest(e, context).ConfigureAwait(false);
                }
                catch (EndpointStatusCodeResponseException e)
                {
                    context.Response.StatusCode = e.StatusCode;
                    await context.Response.WriteAsync(e.Message).ConfigureAwait(false);
                }
            }

            return(Request);
        }
        private static void MapAuthMeta(this EndpointDeclaration info, TypeInfo declaredEndpoint)
        {
            var attributesToMap = declaredEndpoint.GetCustomAttributes().Where(t => t is IAuthorizeData || t is IAllowAnonymous);

            foreach (var attribute in attributesToMap)
            {
                info.Meta.Add(attribute);
            }
        }
        public static IEndpointRequestBodyMetadataProvider?GetBodyParameterOrDefault(this EndpointDeclaration info)
        {
            var bodyParameter = info.HandlerDeclaration.GetDetails().FirstOrDefault(r => r.Source == EndpointParameterSource.Body);

            if (bodyParameter is not null)
            {
                return(new JsonEndpointRequestBodyMetaData(bodyParameter.ParameterType));
            }
            return(null);
        }
        private IEnumerable <ApiDescription> GetApiDescriptions(EndpointDeclaration endpoint, ActionDescriptor actionDescriptor)
        {
            var httpMethodMetadata = endpoint.GetMetadata <HttpMethodMetadata>();

            if (httpMethodMetadata is null)
            {
                return(Enumerable.Empty <ApiDescription>());
            }
            return(httpMethodMetadata.HttpMethods.Select(s => GetApiDescriptions(endpoint, s, actionDescriptor)));
        }
        private static void AddMeta(EndpointDeclaration info, EndpointDeclarationFactoryOptions options, TypeInfo declaredEndpoint, object[] meta)
        {
            var allMeta = options.EndpointMetaDeclarations
                          .SelectMany(declaration => declaration.GetMetaDataFromDeclaredEndpoint(declaredEndpoint))
                          .Concat(meta);

            foreach (var item in allMeta)
            {
                info.Meta.Add(item);
            }
        }
        private static EndpointDeclaration BuildInfoWithRoute(TypeInfo endpoint, EndpointDeclarationFactoryOptions options, IEnumerable <EndpointRouteValueMetadata> routeValueMetaData)
        {
            var controllerName    = GetControllerValue(endpoint);
            var endpointValue     = GetEndpointValue(endpoint);
            var routeValues       = BuildRouteParameters(controllerName, endpointValue);
            var routeInfo         = GetRouteInfo(endpoint, routeValues.Concat(routeValueMetaData).ToArray(), endpointValue.Verb, options);
            var declaredRouteInfo = DeclaredRouteInfoFactory.GetFromTemplate(routeInfo.Template);
            var info = new EndpointDeclaration(endpoint.AsType(), EndpointRequestHandlerFactoryBuilder.BuildFactoryForEndpoint(endpoint, declaredRouteInfo, options), RoutePatternFactory.Parse(routeInfo.Template), routeInfo.Name, routeInfo.Order ?? 0);

            info.Meta.Add(new HttpMethodMetadata(routeInfo.HttpMethods));
            foreach (var routeValue in routeValues)
            {
                info.Meta.Add(routeValue);
            }
            return(info);
        }
        private ApiDescription GetApiDescriptions(EndpointDeclaration endpoint, string httpMethod, ActionDescriptor actionDescriptor)
        {
            var apiDescription = new ApiDescription
            {
                GroupName        = groupName,
                HttpMethod       = httpMethod,
                RelativePath     = BuildRoute(endpoint.Pattern),
                ActionDescriptor = actionDescriptor,
            };

            foreach (var response in GetResponseTypes(endpoint))
            {
                apiDescription.SupportedResponseTypes.Add(response);
            }

            ApplyRequestParameters(apiDescription, endpoint);
            return(apiDescription);
        }
 private static IDictionary <string, string?> GetRouteValues(EndpointDeclaration endpoint)
 {
     return(endpoint.GetAllMetadata <IEndpointRouteValueMetadataProvider>().ToDictionary <IEndpointRouteValueMetadataProvider, string, string?>(k => k.Key, v => v.Value));
 }
Exemple #10
0
 public static T?GetMetadata <T>(this EndpointDeclaration source)
 {
     return(source.Meta.OfType <T>().FirstOrDefault());
 }
Exemple #11
0
 public static IEnumerable <T> GetAllMetadata <T>(this EndpointDeclaration source)
 {
     return(source.Meta.OfType <T>());
 }