Skip to content

Commit

Permalink
Remove use of ILambdaContext
Browse files Browse the repository at this point in the history
Remove use of `ILambdaContext` to log and get a logger from DI instead.
  • Loading branch information
martincostello committed Nov 26, 2023
1 parent 252f2e2 commit ac6ddc9
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 103 deletions.
9 changes: 4 additions & 5 deletions src/LondonTravel.Skill/AlexaFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Alexa.NET.Request;
using Alexa.NET.Response;
using Amazon.Lambda.Core;
using MartinCostello.LondonTravel.Skill.Extensions;
using MartinCostello.LondonTravel.Skill.Intents;
using Microsoft.ApplicationInsights;
Expand Down Expand Up @@ -72,15 +71,15 @@ public async virtual ValueTask DisposeAsync()
/// Handles a request to the skill as an asynchronous operation.
/// </summary>
/// <param name="request">The skill request.</param>
/// <param name="context">The AWS Lambda execution context.</param>
/// <returns>
/// A <see cref="Task{TResult}"/> representing the asynchronous operation to get the skill's response.
/// </returns>
public async Task<SkillResponse> HandlerAsync(SkillRequest request, ILambdaContext context)
public async Task<SkillResponse> HandlerAsync(SkillRequest request)
{
context.Logger.LogLine($"Invoking skill request of type {request.Request.GetType().Name}.");

var handler = _serviceProvider.GetRequiredService<FunctionHandler>();
var logger = _serviceProvider.GetRequiredService<ILogger<AlexaFunction>>();

Log.InvokingSkillRequest(logger, request.Request.Type);

return await handler.HandleAsync(request);
}
Expand Down
6 changes: 2 additions & 4 deletions src/LondonTravel.Skill/AlexaFunctionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Alexa.NET.Request;
using Alexa.NET.Response;
using Amazon.Lambda.Core;

namespace MartinCostello.LondonTravel.Skill;

Expand All @@ -19,14 +18,13 @@ public static class AlexaFunctionHandler
/// Handles a request to the skill as an asynchronous operation.
/// </summary>
/// <param name="request">The skill request.</param>
/// <param name="context">The current Lambda context.</param>
/// <returns>
/// A <see cref="Task{TResult}"/> representing the asynchronous operation to get the skill's response.
/// </returns>
public static async Task<SkillResponse> HandleAsync(SkillRequest request, ILambdaContext context)
public static async Task<SkillResponse> HandleAsync(SkillRequest request)
{
await EnsureInitialized();
return await _function.HandlerAsync(request, context);
return await _function.HandlerAsync(request);
}

private static async Task EnsureInitialized()
Expand Down
6 changes: 6 additions & 0 deletions src/LondonTravel.Skill/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ public static partial void SystemError(
Level = LogLevel.Warning,
Message = "Unknown intent {IntentName} cannot be handled for session Id {SessionId}.")]
public static partial void UnknownIntent(ILogger logger, string intentName, string sessionId);

[LoggerMessage(
EventId = 7,
Level = LogLevel.Information,
Message = "Invoking skill request of type {RequestType}.")]
public static partial void InvokingSkillRequest(ILogger logger, string requestType);
}
6 changes: 2 additions & 4 deletions test/LondonTravel.Skill.Tests/AlexaFunctionHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Alexa.NET.Request;
using Alexa.NET.Response;
using Amazon.Lambda.Core;

namespace MartinCostello.LondonTravel.Skill;

Expand All @@ -14,10 +13,9 @@ public async Task Can_Invoke_Static_Function()
{
// Arrange
SkillRequest request = CreateIntentRequest("AMAZON.HelpIntent");
ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await AlexaFunctionHandler.HandleAsync(request, context);
SkillResponse actual = await AlexaFunctionHandler.HandleAsync(request);

// Assert
ResponseBody response = AssertResponse(actual, shouldEndSession: false);
Expand All @@ -26,7 +24,7 @@ public async Task Can_Invoke_Static_Function()
response.OutputSpeech.Type.ShouldBe("SSML");

// Act
actual = await AlexaFunctionHandler.HandleAsync(request, context);
actual = await AlexaFunctionHandler.HandleAsync(request);

// Assert
response = AssertResponse(actual, shouldEndSession: false);
Expand Down
12 changes: 3 additions & 9 deletions test/LondonTravel.Skill.Tests/AlexaFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Alexa.NET.Request;
using Alexa.NET.Request.Type;
using Alexa.NET.Response;
using Amazon.Lambda.Core;

namespace MartinCostello.LondonTravel.Skill;

Expand All @@ -19,11 +18,9 @@ public async Task Cannot_Invoke_Function_If_Application_Id_Incorrect()
SkillRequest request = CreateIntentRequest("AMAZON.HelpIntent");
request.Session.Application.ApplicationId = "not-my-skill-id";

ILambdaContext context = CreateContext();

// Act
InvalidOperationException exception = await Assert.ThrowsAsync<InvalidOperationException>(
() => function.HandlerAsync(request, context));
() => function.HandlerAsync(request));

// Assert
exception.Message.ShouldBe("Request application Id 'not-my-skill-id' and configured skill Id 'my-skill-id' mismatch.");
Expand All @@ -43,10 +40,8 @@ public async Task Can_Invoke_Function_If_Locale_Is_Invalid(string locale)
SkillRequest request = CreateIntentRequest("AMAZON.HelpIntent");
request.Request.Locale = locale;

ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
ResponseBody response = AssertResponse(actual, shouldEndSession: false);
Expand All @@ -60,7 +55,6 @@ public async Task Cannot_Invoke_Function_With_System_Failure()
{
// Arrange
AlexaFunction function = await CreateFunctionAsync();
ILambdaContext context = CreateContext();

var error = new SystemExceptionRequest()
{
Expand All @@ -78,7 +72,7 @@ public async Task Cannot_Invoke_Function_With_System_Failure()
var request = CreateRequest(error);

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
ResponseBody response = AssertResponse(actual);
Expand Down
4 changes: 1 addition & 3 deletions test/LondonTravel.Skill.Tests/CancelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Alexa.NET.Request;
using Alexa.NET.Response;
using Amazon.Lambda.Core;

namespace MartinCostello.LondonTravel.Skill;

Expand All @@ -16,10 +15,9 @@ public async Task Can_Invoke_Function()
AlexaFunction function = await CreateFunctionAsync();

SkillRequest request = CreateIntentRequest("AMAZON.CancelIntent");
ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
ResponseBody response = AssertResponse(actual);
Expand Down
19 changes: 6 additions & 13 deletions test/LondonTravel.Skill.Tests/CommuteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Alexa.NET.Request;
using Alexa.NET.Response;
using Amazon.Lambda.Core;
using JustEat.HttpClientInterception;

namespace MartinCostello.LondonTravel.Skill;
Expand All @@ -16,10 +15,9 @@ public async Task Can_Invoke_Function_When_The_Skill_Is_Not_Linked()
// Arrange
AlexaFunction function = await CreateFunctionAsync();
SkillRequest request = CreateIntentRequestWithToken(accessToken: null);
ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
ResponseBody response = AssertResponse(actual);
Expand All @@ -44,10 +42,9 @@ public async Task Can_Invoke_Function_When_The_Skill_Token_Is_Invalid()

AlexaFunction function = await CreateFunctionAsync();
SkillRequest request = CreateIntentRequestWithToken(accessToken: "invalid-access-token");
ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
ResponseBody response = AssertResponse(actual);
Expand All @@ -70,10 +67,9 @@ public async Task Can_Invoke_Function_When_The_Skill_Api_Fails()
// Arrange
AlexaFunction function = await CreateFunctionAsync();
SkillRequest request = CreateIntentRequestWithToken(accessToken: "random-access-token");
ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
ResponseBody response = AssertResponse(actual);
Expand All @@ -97,10 +93,9 @@ public async Task Can_Invoke_Function_When_The_Skill_Is_Linked_And_Has_No_Favori

AlexaFunction function = await CreateFunctionAsync();
SkillRequest request = CreateIntentRequestWithToken(accessToken: "token-for-no-favorites");
ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
AssertResponse(
Expand All @@ -118,10 +113,9 @@ public async Task Can_Invoke_Function_When_The_Skill_Is_Linked_And_Has_One_Favor

AlexaFunction function = await CreateFunctionAsync();
SkillRequest request = CreateIntentRequestWithToken(accessToken: "token-for-one-favorite");
ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
AssertResponse(
Expand All @@ -139,10 +133,9 @@ public async Task Can_Invoke_Function_When_The_Skill_Is_Linked_And_Has_Two_Favor

AlexaFunction function = await CreateFunctionAsync();
SkillRequest request = CreateIntentRequestWithToken(accessToken: "token-for-two-favorites");
ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
AssertResponse(
Expand Down
13 changes: 4 additions & 9 deletions test/LondonTravel.Skill.Tests/DisruptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Alexa.NET.Request;
using Alexa.NET.Response;
using Amazon.Lambda.Core;
using JustEat.HttpClientInterception;

namespace MartinCostello.LondonTravel.Skill;
Expand All @@ -18,10 +17,9 @@ public async Task Can_Invoke_Function_When_There_Are_No_Disruptions()

AlexaFunction function = await CreateFunctionAsync();
SkillRequest request = CreateIntentRequest();
ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
AssertResponse(
Expand All @@ -38,10 +36,9 @@ public async Task Can_Invoke_Function_When_There_Is_One_Disruption()

AlexaFunction function = await CreateFunctionAsync();
SkillRequest request = CreateIntentRequest();
ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
AssertResponse(
Expand All @@ -58,10 +55,9 @@ public async Task Can_Invoke_Function_When_There_Are_Multiple_Disruptions()

AlexaFunction function = await CreateFunctionAsync();
SkillRequest request = CreateIntentRequest();
ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
AssertResponse(
Expand All @@ -76,10 +72,9 @@ public async Task Can_Invoke_Function_When_The_Api_Fails()
// Arrange
AlexaFunction function = await CreateFunctionAsync();
SkillRequest request = CreateIntentRequest();
ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
ResponseBody response = AssertResponse(actual);
Expand Down
10 changes: 0 additions & 10 deletions test/LondonTravel.Skill.Tests/FunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using Alexa.NET.Request;
using Alexa.NET.Request.Type;
using Alexa.NET.Response;
using Amazon.Lambda.Core;
using Amazon.Lambda.TestUtilities;
using JustEat.HttpClientInterception;
using MartinCostello.Logging.XUnit;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -50,14 +48,6 @@ protected virtual SkillConfiguration CreateConfiguration()
return config;
}

protected virtual ILambdaContext CreateContext()
{
return new TestLambdaContext()
{
Logger = new XunitLambdaLogger(OutputHelper),
};
}

protected virtual async Task<AlexaFunction> CreateFunctionAsync()
{
SkillConfiguration config = CreateConfiguration();
Expand Down
4 changes: 1 addition & 3 deletions test/LondonTravel.Skill.Tests/HelpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Alexa.NET.Request;
using Alexa.NET.Response;
using Amazon.Lambda.Core;

namespace MartinCostello.LondonTravel.Skill;

Expand All @@ -16,10 +15,9 @@ public async Task Can_Invoke_Function()
AlexaFunction function = await CreateFunctionAsync();

SkillRequest request = CreateIntentRequest("AMAZON.HelpIntent");
ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
ResponseBody response = AssertResponse(actual, shouldEndSession: false);
Expand Down
4 changes: 1 addition & 3 deletions test/LondonTravel.Skill.Tests/LaunchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Alexa.NET.Request;
using Alexa.NET.Request.Type;
using Alexa.NET.Response;
using Amazon.Lambda.Core;

namespace MartinCostello.LondonTravel.Skill;

Expand All @@ -17,10 +16,9 @@ public async Task Can_Invoke_Function()
AlexaFunction function = await CreateFunctionAsync();

SkillRequest request = CreateRequest<LaunchRequest>();
ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
ResponseBody response = AssertResponse(actual, shouldEndSession: false);
Expand Down
4 changes: 1 addition & 3 deletions test/LondonTravel.Skill.Tests/SessionEndedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Alexa.NET.Request;
using Alexa.NET.Request.Type;
using Alexa.NET.Response;
using Amazon.Lambda.Core;

namespace MartinCostello.LondonTravel.Skill;

Expand All @@ -17,10 +16,9 @@ public async Task Can_Invoke_Function()
AlexaFunction function = await CreateFunctionAsync();

SkillRequest request = CreateRequest<SessionEndedRequest>();
ILambdaContext context = CreateContext();

// Act
SkillResponse actual = await function.HandlerAsync(request, context);
SkillResponse actual = await function.HandlerAsync(request);

// Assert
ResponseBody response = AssertResponse(actual);
Expand Down
Loading

0 comments on commit ac6ddc9

Please sign in to comment.