Skip to content

Commit

Permalink
add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gcbeattyAWS committed Dec 10, 2024
1 parent 4a86383 commit 8bb6fa9
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class HttpContextExtensions
/// Translates an <see cref="HttpContext"/> to an <see cref="APIGatewayHttpApiV2ProxyRequest"/>.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> to be translated.</param>
/// <param name="apiGatewayRouteConfig">The configuration of the API Gateway route, including the HTTP method, path, and other metadata.</param>
/// <returns>An <see cref="APIGatewayHttpApiV2ProxyRequest"/> object representing the translated request.</returns>
public static APIGatewayHttpApiV2ProxyRequest ToApiGatewayHttpV2Request(
this HttpContext context,
Expand Down Expand Up @@ -67,6 +68,7 @@ public static APIGatewayHttpApiV2ProxyRequest ToApiGatewayHttpV2Request(
/// Translates an <see cref="HttpContext"/> to an <see cref="APIGatewayProxyRequest"/>.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/> to be translated.</param>
/// <param name="apiGatewayRouteConfig">The configuration of the API Gateway route, including the HTTP method, path, and other metadata.</param>
/// <returns>An <see cref="APIGatewayProxyRequest"/> object representing the translated request.</returns>
public static APIGatewayProxyRequest ToApiGatewayRequest(
this HttpContext context,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,78 +1,78 @@
using Amazon.Lambda.TestTool.Commands.Settings;
using Amazon.Lambda.TestTool.Extensions;
using Amazon.Lambda.TestTool.Models;
using Amazon.Lambda.TestTool.Services;

namespace Amazon.Lambda.TestTool.Processes;

/// <summary>
/// A process that runs the API Gatewat emulator.
/// </summary>
public class ApiGatewayEmulatorProcess
{
/// <summary>
/// The service provider that will contain all the registered services.
/// </summary>
using Amazon.Lambda.TestTool.Commands.Settings;
using Amazon.Lambda.TestTool.Extensions;
using Amazon.Lambda.TestTool.Models;
using Amazon.Lambda.TestTool.Services;

namespace Amazon.Lambda.TestTool.Processes;

/// <summary>
/// A process that runs the API Gatewat emulator.
/// </summary>
public class ApiGatewayEmulatorProcess
{
/// <summary>
/// The service provider that will contain all the registered services.
/// </summary>
public required IServiceProvider Services { get; init; }

/// <summary>
/// The API Gatewat emulator task that was started.
/// </summary>
/// <summary>
/// The API Gatewat emulator task that was started.
/// </summary>
public required Task RunningTask { get; init; }

/// <summary>
/// The endpoint of the API Gatewat emulator.
/// </summary>
/// </summary>
public required string ServiceUrl { get; init; }

/// <summary>
/// Creates the Web API and runs it in the background.
/// </summary>
public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, CancellationToken cancellationToken = default)
{
var builder = WebApplication.CreateBuilder();

builder.Services.AddApiGatewayEmulatorServices();

var serviceUrl = $"http://{settings.Host}:{settings.ApiGatewayEmulatorPort}";
builder.WebHost.UseUrls(serviceUrl);
/// </summary>
public static ApiGatewayEmulatorProcess Startup(RunCommandSettings settings, CancellationToken cancellationToken = default)
{
var builder = WebApplication.CreateBuilder();

builder.Services.AddApiGatewayEmulatorServices();

var serviceUrl = $"http://{settings.Host}:{settings.ApiGatewayEmulatorPort}";
builder.WebHost.UseUrls(serviceUrl);
builder.WebHost.SuppressStatusMessages(true);

builder.Services.AddHealthChecks();

var app = builder.Build();

var app = builder.Build();

app.UseHttpsRedirection();

app.MapHealthChecks("/health");

app.Map("/{**catchAll}", (HttpContext context, IApiGatewayRouteConfigService routeConfigService) =>
{
var routeConfig = routeConfigService.GetRouteConfig(context.Request.Method, context.Request.Path);
if (routeConfig == null)
{
return Results.NotFound("Route not found");
}

if (settings.ApiGatewayEmulatorMode.Equals(ApiGatewayEmulatorMode.HttpV2))
{
// TODO: Translate to APIGatewayHttpApiV2ProxyRequest
}
else
{
// TODO: Translate to APIGatewayProxyRequest
}

return Results.Ok();
});

var runTask = app.RunAsync(cancellationToken);

return new ApiGatewayEmulatorProcess
{
Services = app.Services,
RunningTask = runTask,
ServiceUrl = serviceUrl
};
}
app.MapHealthChecks("/health");

app.Map("/{**catchAll}", (HttpContext context, IApiGatewayRouteConfigService routeConfigService) =>
{
var routeConfig = routeConfigService.GetRouteConfig(context.Request.Method, context.Request.Path);
if (routeConfig == null)
{
return Results.NotFound("Route not found");
}

if (settings.ApiGatewayEmulatorMode.Equals(ApiGatewayEmulatorMode.HttpV2))
{
// TODO: Translate to APIGatewayHttpApiV2ProxyRequest
}
else
{
// TODO: Translate to APIGatewayProxyRequest
}

return Results.Ok();
});

var runTask = app.RunAsync(cancellationToken);

return new ApiGatewayEmulatorProcess
{
Services = app.Services,
RunningTask = runTask,
ServiceUrl = serviceUrl
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,26 @@

namespace Amazon.Lambda.TestTool.Utilities
{
/// <summary>
/// Provides utility methods for working with route templates and extracting path parameters.
/// </summary>
public static class RouteTemplateUtility
{
/// <summary>
/// Extracts path parameters from an actual path based on a route template.
/// </summary>
/// <param name="routeTemplate">The route template to match against.</param>
/// <param name="actualPath">The actual path to extract parameters from.</param>
/// <returns>A dictionary of extracted path parameters and their values.</returns>
/// <example>
/// Using this method:
/// <code>
/// var routeTemplate = "/users/{id}/orders/{orderId}";
/// var actualPath = "/users/123/orders/456";
/// var parameters = RouteTemplateUtility.ExtractPathParameters(routeTemplate, actualPath);
/// // parameters will contain: { {"id", "123"}, {"orderId", "456"} }
/// </code>
/// </example>
public static Dictionary<string, string> ExtractPathParameters(string routeTemplate, string actualPath)
{
var template = TemplateParser.Parse(routeTemplate);
Expand All @@ -18,6 +36,19 @@ public static Dictionary<string, string> ExtractPathParameters(string routeTempl
return new Dictionary<string, string>();
}

/// <summary>
/// Gets the default values for parameters in a parsed route template.
/// </summary>
/// <param name="parsedTemplate">The parsed route template.</param>
/// <returns>A dictionary of default values for the template parameters.</returns>
/// <example>
/// Using this method:
/// <code>
/// var template = TemplateParser.Parse("/api/{version=v1}/users/{id}");
/// var defaults = RouteTemplateUtility.GetDefaults(template);
/// // defaults will contain: { {"version", "v1"} }
/// </code>
/// </example>
public static RouteValueDictionary GetDefaults(RouteTemplate parsedTemplate)
{
var result = new RouteValueDictionary();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit 8bb6fa9

Please sign in to comment.