< Summary

Information
Class: NanoRoute.AwsLambda.ApiGatewayHttpApiV2Router
Assembly: NanoRoute.AwsLambda.dll
File(s): /home/runner/work/nanoroute/nanoroute/Src/NanoRoute.AwsLambda/Public/ApiGatewayHttpApiV2Router.cs
Line coverage
100%
Covered lines: 4
Uncovered lines: 0
Coverable lines: 4
Total lines: 87
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBlocks covered Blocks not covered
ApiGatewayHttpApiV2Router(...)30
CreateBuilder()50

File(s)

/home/runner/work/nanoroute/nanoroute/Src/NanoRoute.AwsLambda/Public/ApiGatewayHttpApiV2Router.cs

#LineLine coverage
 1/********************************************************************************
 2* ApiGatewayHttpApiV2Router.cs                                                  *
 3*                                                                               *
 4* Author: Denes Solti                                                           *
 5********************************************************************************/
 6using System;
 7using System.Collections.Generic;
 8using System.Net;
 9using System.Net.Http;
 10using System.Text.Json;
 11using System.Threading;
 12using System.Threading.Tasks;
 13
 14using Amazon.Lambda.APIGatewayEvents;
 15
 16namespace NanoRoute.AwsLambda
 17{
 18    using Json;
 19
 20    /// <summary>
 21    /// Routes API Gateway HTTP API and Lambda Function URL <see cref="APIGatewayHttpApiV2ProxyRequest"/> instances thro
 22    /// </summary>
 23    public sealed class ApiGatewayHttpApiV2Router : Router
 24    {
 125        private ApiGatewayHttpApiV2Router(RouterBuilder<ApiGatewayHttpApiV2Router, AwsLambdaRouterConfig> builder) : bas
 126        {
 127        }
 28
 29        /// <summary>
 30        /// Routes an API Gateway HTTP API or Lambda Function URL payload-format-2.0 request and returns the correspondi
 31        /// </summary>
 32        /// <param name="request">The API Gateway v2 request event.</param>
 33        /// <param name="services">The service provider exposed to handlers through <see cref="RequestContext.Services"/
 34        /// <param name="remainingTime">The time left before the Lambda invocation times out. Expired execution produces
 35        /// <returns>The API Gateway v2 proxy response generated by NanoRoute.</returns>
 36        public async Task<APIGatewayHttpApiV2ProxyResponse> Route(APIGatewayHttpApiV2ProxyRequest request, IServiceProvi
 37        {
 38            Ensure.NotNull(request, nameof(request));
 39            Ensure.NotNull(services, nameof(services));
 40
 41            TimeSpan cancellationDelay = remainingTime.Add(TimeSpan.FromSeconds(-1));
 42            if (cancellationDelay <= TimeSpan.Zero)
 43                return Timeout();
 44
 45            using CancellationTokenSource cts = new();
 46            cts.CancelAfter(cancellationDelay);
 47
 48            using HttpRequestMessage requestMessage = request.CreateRequestMessage();
 49
 50            try
 51            {
 52                using HttpResponseMessage responseMessage = await Handle(requestMessage, services, cts.Token);
 53
 54                return await responseMessage.CreateResponse();
 55            }
 56            catch (OperationCanceledException)
 57            {
 58                return Timeout();
 59            }
 60
 61            APIGatewayHttpApiV2ProxyResponse Timeout() => new()
 62            {
 63                StatusCode = (int) HttpStatusCode.GatewayTimeout,
 64                Body = JsonSerializer.Serialize
 65                (
 66                    new ErrorDetails
 67                    {
 68                        Status = HttpStatusCode.GatewayTimeout,
 69                        Title = "Gateway Timeout",
 70                        TraceId = request.RequestContext.RequestId
 71                    },
 72                    ErrorDetails.JsonTypeInfo
 73                ),
 74                Headers = new Dictionary<string, string>
 75                {
 76                    ["Content-Type"] = "application/json"
 77                }
 78            };
 79        }
 80
 81        /// <summary>
 82        /// Creates a strongly typed builder for configuring an <see cref="ApiGatewayHttpApiV2Router"/>.
 83        /// </summary>
 84        /// <returns>A builder that can register handlers, value parsers, and router configuration.</returns>
 185        public static RouterBuilder<ApiGatewayHttpApiV2Router, AwsLambdaRouterConfig> CreateBuilder() => new RouterBuild
 86    }
 87}