Skip to content

Commit

Permalink
Use extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
gcbeattyAWS committed Dec 9, 2024
1 parent 57bfdc2 commit 2e94f8b
Show file tree
Hide file tree
Showing 14 changed files with 387 additions and 577 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Amazon.Lambda.TestTool
{
public class ApiGatewayRouteConfig
{
public string LambdaResourceName { get; set; }
public string Endpoint { get; set; }
public string HttpMethod { get; set; }
public string Path { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using Amazon.Lambda.APIGatewayEvents;
using System.Text;
using System.Web;
using static Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyRequest;

namespace Amazon.Lambda.TestTool
{
/// <summary>
/// Provides extension methods to translate an <see cref="HttpContext"/> to different types of API Gateway requests.
/// </summary>
public static class HttpContextExtensions
{
private static IHttpRequestUtility _httpRequestUtility = new HttpRequestUtility();
private static IRouteConfigurationParser _routeConfigurationParser;

public static void SetHttpRequestUtility(IHttpRequestUtility httpRequestUtility)
{
_httpRequestUtility = httpRequestUtility ?? throw new ArgumentNullException(nameof(httpRequestUtility));
}

public static void SetRouteConfigurationParser(IRouteConfigurationParser routeConfigurationParser)
{
_routeConfigurationParser = routeConfigurationParser ?? throw new ArgumentNullException(nameof(routeConfigurationParser));
}

/// <summary>
/// Translates an <see cref="HttpContext"/> to an <see cref="APIGatewayHttpApiV2ProxyRequest"/>.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> to be translated.</param>
/// <returns>An <see cref="APIGatewayHttpApiV2ProxyRequest"/> object representing the translated request.</returns>
public static APIGatewayHttpApiV2ProxyRequest ToApiGatewayHttpV2Request(
this HttpContext context)
{
var request = context.Request;

var matchedConfig = _routeConfigurationParser.GetRouteConfig(request.Method, request.Path);
var pathParameters = _routeConfigurationParser.ExtractPathParameters(matchedConfig, request.Path);

var (headers, _) = _httpRequestUtility.ExtractHeaders(request.Headers);
var (queryStringParameters, _) = _httpRequestUtility.ExtractQueryStringParameters(request.Query);

var httpApiV2ProxyRequest = new APIGatewayHttpApiV2ProxyRequest
{
RouteKey = $"{request.Method} {matchedConfig.Path}",
RawPath = request.Path,
RawQueryString = request.QueryString.Value,
Cookies = request.Cookies.Select(c => $"{c.Key}={c.Value}").ToArray(),
Headers = headers,
QueryStringParameters = queryStringParameters,
PathParameters = pathParameters ?? new Dictionary<string, string>(),
Body = _httpRequestUtility.ReadRequestBody(request),
IsBase64Encoded = false,
RequestContext = new ProxyRequestContext
{
Http = new HttpDescription
{
Method = request.Method,
Path = request.Path,
Protocol = request.Protocol
},
RouteKey = $"{request.Method} {matchedConfig.Path}"
},
Version = "2.0"
};

if (_httpRequestUtility.IsBinaryContent(request.ContentType))
{
httpApiV2ProxyRequest.Body = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(httpApiV2ProxyRequest.Body));
httpApiV2ProxyRequest.IsBase64Encoded = true;
}

return httpApiV2ProxyRequest;
}

/// <summary>
/// Translates an <see cref="HttpContext"/> to an <see cref="APIGatewayProxyRequest"/>.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> to be translated.</param>
/// <returns>An <see cref="APIGatewayProxyRequest"/> object representing the translated request.</returns>
public static APIGatewayProxyRequest ToApiGatewayRequest(
this HttpContext context)
{
var request = context.Request;

var matchedConfig = _routeConfigurationParser.GetRouteConfig(request.Method, request.Path);
var pathParameters = _routeConfigurationParser.ExtractPathParameters(matchedConfig, request.Path);

var (headers, multiValueHeaders) = _httpRequestUtility.ExtractHeaders(request.Headers);
var (queryStringParameters, multiValueQueryStringParameters) = _httpRequestUtility.ExtractQueryStringParameters(request.Query);

var proxyRequest = new APIGatewayProxyRequest
{
Resource = matchedConfig.Path,
Path = HttpUtility.UrlEncode(request.Path),
HttpMethod = request.Method,
Headers = headers,
MultiValueHeaders = multiValueHeaders,
QueryStringParameters = queryStringParameters,
MultiValueQueryStringParameters = multiValueQueryStringParameters,
PathParameters = pathParameters ?? new Dictionary<string, string>(),
Body = _httpRequestUtility.ReadRequestBody(request),
IsBase64Encoded = false
};

if (_httpRequestUtility.IsBinaryContent(request.ContentType))
{
proxyRequest.Body = Convert.ToBase64String(Encoding.UTF8.GetBytes(proxyRequest.Body));
proxyRequest.IsBase64Encoded = true;
}

return proxyRequest;
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Amazon.Lambda.TestTool
{
public interface IRouteConfigurationParser
{
ApiGatewayRouteConfig GetRouteConfig(string httpMethod, string path);
IDictionary<string, string> ExtractPathParameters(ApiGatewayRouteConfig routeConfig, string requestPath);
}
}
Loading

0 comments on commit 2e94f8b

Please sign in to comment.