88using System . Text . Json . Serialization ;
99using Amazon . Lambda . APIGatewayEvents ;
1010using Amazon . Lambda . ApplicationLoadBalancerEvents ;
11+ using Amazon . Lambda . AspNetCoreServer . Internal ;
1112using Amazon . Lambda . Core ;
1213using Amazon . Lambda . RuntimeSupport ;
1314using Amazon . Lambda . RuntimeSupport . Helpers ;
@@ -101,8 +102,6 @@ private class HelperLambdaContext : ILambdaContext, ICognitoIdentity, IClientCon
101102
102103 private static class SnapstartHelperLambdaRequests
103104 {
104- private static readonly Uri _baseUri = new Uri ( "http://localhost" ) ;
105-
106105 public static async Task ExecuteSnapstartInitRequests ( string jsonRequest , int times , HandlerWrapper handlerWrapper )
107106 {
108107 var dummyRequest = new InvocationRequest (
@@ -124,108 +123,20 @@ public static async Task ExecuteSnapstartInitRequests(string jsonRequest, int ti
124123
125124 public static async Task < string > SerializeToJson ( HttpRequestMessage request , LambdaEventSource lambdaType )
126125 {
127- if ( null == request . RequestUri )
128- {
129- throw new ArgumentException ( $ "{ nameof ( HttpRequestMessage . RequestUri ) } must be set.", nameof ( request ) ) ;
130- }
131-
132- if ( request . RequestUri . IsAbsoluteUri )
133- {
134- throw new ArgumentException ( $ "{ nameof ( HttpRequestMessage . RequestUri ) } must be relative.", nameof ( request ) ) ;
135- }
136-
137- // make request absolut (relative to localhost) otherwise parsing the query will not work
138- request . RequestUri = new Uri ( _baseUri , request . RequestUri ) ;
139-
140- var duckRequest = new
141- {
142- Body = await ReadContent ( request ) ,
143- Headers = request . Headers
144- . ToDictionary (
145- kvp => kvp . Key ,
146- kvp => kvp . Value . FirstOrDefault ( ) ,
147- StringComparer . OrdinalIgnoreCase ) ,
148- HttpMethod = request . Method . ToString ( ) ,
149- Path = "/" + _baseUri . MakeRelativeUri ( request . RequestUri ) ,
150- RawQuery = request . RequestUri ? . Query ,
151- Query = QueryHelpers . ParseNullableQuery ( request . RequestUri ? . Query )
152- } ;
153-
154- string translatedRequestJson = lambdaType switch
126+ var result = lambdaType switch
155127 {
156128 LambdaEventSource . ApplicationLoadBalancer =>
157- JsonSerializer . Serialize (
158- new ApplicationLoadBalancerRequest
159- {
160- Body = duckRequest . Body ,
161- Headers = duckRequest . Headers ,
162- Path = duckRequest . Path ,
163- HttpMethod = duckRequest . HttpMethod ,
164- QueryStringParameters = duckRequest . Query ? . ToDictionary ( k => k . Key , v => v . Value . ToString ( ) )
165- } ,
166- LambdaRequestTypeClasses . Default . ApplicationLoadBalancerRequest ) ,
129+ await HttpRequestMessageSerializer . SerializeToJson < ApplicationLoadBalancerRequest > ( request ) ,
167130 LambdaEventSource . HttpApi =>
168- JsonSerializer . Serialize (
169- new APIGatewayHttpApiV2ProxyRequest
170- {
171- Body = duckRequest . Body ,
172- Headers = duckRequest . Headers ,
173- RawPath = duckRequest . Path ,
174- RequestContext = new APIGatewayHttpApiV2ProxyRequest . ProxyRequestContext
175- {
176- Http = new APIGatewayHttpApiV2ProxyRequest . HttpDescription
177- {
178- Method = duckRequest . HttpMethod ,
179- Path = duckRequest . Path
180- }
181- } ,
182- QueryStringParameters = duckRequest . Query ? . ToDictionary ( k => k . Key , v => v . Value . ToString ( ) ) ,
183- RawQueryString = duckRequest . RawQuery
184- } ,
185- LambdaRequestTypeClasses . Default . APIGatewayHttpApiV2ProxyRequest ) ,
131+ await HttpRequestMessageSerializer . SerializeToJson < APIGatewayHttpApiV2ProxyRequest > ( request ) ,
186132 LambdaEventSource . RestApi =>
187- JsonSerializer . Serialize (
188- new APIGatewayProxyRequest
189- {
190- Body = duckRequest . Body ,
191- Headers = duckRequest . Headers ,
192- Path = duckRequest . Path ,
193- HttpMethod = duckRequest . HttpMethod ,
194- RequestContext = new APIGatewayProxyRequest . ProxyRequestContext
195- {
196- HttpMethod = duckRequest . HttpMethod
197- } ,
198- QueryStringParameters = duckRequest . Query ? . ToDictionary ( k => k . Key , v => v . Value . ToString ( ) )
199- } ,
200- LambdaRequestTypeClasses . Default . APIGatewayProxyRequest ) ,
133+ await HttpRequestMessageSerializer . SerializeToJson < APIGatewayProxyRequest > ( request ) ,
201134 _ => throw new NotImplementedException (
202135 $ "Unknown { nameof ( LambdaEventSource ) } : { Enum . GetName ( lambdaType ) } ")
203136 } ;
204137
205- return translatedRequestJson ;
206- }
207-
208- private static async Task < string > ReadContent ( HttpRequestMessage r )
209- {
210- if ( r . Content == null )
211- return string . Empty ;
212-
213- return await r . Content . ReadAsStringAsync ( ) ;
138+ return result ;
214139 }
215140 }
216141}
217-
218-
219- [ JsonSourceGenerationOptions ( WriteIndented = true ) ]
220- [ JsonSerializable ( typeof ( ApplicationLoadBalancerRequest ) ) ]
221- [ JsonSerializable ( typeof ( APIGatewayHttpApiV2ProxyRequest ) ) ]
222- [ JsonSerializable ( typeof ( APIGatewayProxyRequest ) ) ]
223- [ JsonSerializable ( typeof ( APIGatewayProxyRequest . ClientCertValidity ) ) ]
224- [ JsonSerializable ( typeof ( APIGatewayProxyRequest . ProxyRequestClientCert ) ) ]
225- [ JsonSerializable ( typeof ( APIGatewayProxyRequest . ProxyRequestContext ) ) ]
226- [ JsonSerializable ( typeof ( APIGatewayProxyRequest . RequestIdentity ) ) ]
227-
228- internal partial class LambdaRequestTypeClasses : JsonSerializerContext
229- {
230- }
231142#endif
0 commit comments