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 aaa877f commit e8035e8
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 106 deletions.
9 changes: 4 additions & 5 deletions src/LondonTravel.Skill/AlexaFunction.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Amazon.Lambda.Core;
using MartinCostello.LondonTravel.Skill.Extensions;
using MartinCostello.LondonTravel.Skill.Intents;
using MartinCostello.LondonTravel.Skill.Models;
Expand Down Expand Up @@ -69,15 +68,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.Type}.");

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

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

var converted = request.FromModel();

Expand Down
6 changes: 2 additions & 4 deletions src/LondonTravel.Skill/AlexaFunctionHandler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Amazon.Lambda.Core;
using MartinCostello.LondonTravel.Skill.Models;

namespace MartinCostello.LondonTravel.Skill;
Expand All @@ -18,14 +17,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 @@ -63,4 +63,10 @@ public static partial void SystemError(
Level = LogLevel.Debug,
Message = "Ended session for user Id {UserId} and session Id {SessionId}.")]
public static partial void SessionEnded(ILogger logger, string userId, string sessionId);

[LoggerMessage(
EventId = 9,
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
@@ -1,7 +1,6 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Amazon.Lambda.Core;
using MartinCostello.LondonTravel.Skill.Models;

namespace MartinCostello.LondonTravel.Skill;
Expand All @@ -13,10 +12,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 @@ -25,7 +23,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
@@ -1,7 +1,6 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Amazon.Lambda.Core;
using MartinCostello.LondonTravel.Skill.Models;

namespace MartinCostello.LondonTravel.Skill;
Expand All @@ -18,11 +17,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 @@ -42,10 +39,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 @@ -59,7 +54,6 @@ public async Task Cannot_Invoke_Function_With_System_Failure()
{
// Arrange
AlexaFunction function = await CreateFunctionAsync();
ILambdaContext context = CreateContext();

var error = new Request()
{
Expand All @@ -77,7 +71,7 @@ public async Task Cannot_Invoke_Function_With_System_Failure()
var request = CreateRequest("System.ExceptionEncountered", error);

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

// Assert
await Verify(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
@@ -1,7 +1,6 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Amazon.Lambda.Core;
using MartinCostello.LondonTravel.Skill.Models;

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
await Verify(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
@@ -1,7 +1,6 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Amazon.Lambda.Core;
using JustEat.HttpClientInterception;
using MartinCostello.LondonTravel.Skill.Models;

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
await Verify(actual);
Expand All @@ -46,10 +44,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
await Verify(actual);
Expand All @@ -74,10 +71,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
await Verify(actual);
Expand All @@ -103,10 +99,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
await Verify(actual);
Expand All @@ -126,10 +121,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
await Verify(actual);
Expand All @@ -149,10 +143,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
await Verify(actual);
Expand Down
13 changes: 4 additions & 9 deletions test/LondonTravel.Skill.Tests/DisruptionTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Amazon.Lambda.Core;
using JustEat.HttpClientInterception;
using MartinCostello.LondonTravel.Skill.Models;

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
await Verify(actual);
Expand All @@ -40,10 +38,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
await Verify(actual);
Expand All @@ -62,10 +59,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
await Verify(actual);
Expand All @@ -82,10 +78,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
await Verify(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
@@ -1,8 +1,6 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Amazon.Lambda.Core;
using Amazon.Lambda.TestUtilities;
using JustEat.HttpClientInterception;
using MartinCostello.Logging.XUnit;
using MartinCostello.LondonTravel.Skill.Models;
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
@@ -1,7 +1,6 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Amazon.Lambda.Core;
using MartinCostello.LondonTravel.Skill.Models;

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
await Verify(actual);
Expand Down
4 changes: 1 addition & 3 deletions test/LondonTravel.Skill.Tests/LaunchTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Amazon.Lambda.Core;
using MartinCostello.LondonTravel.Skill.Models;

namespace MartinCostello.LondonTravel.Skill;
Expand All @@ -16,10 +15,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
await Verify(actual);
Expand Down
4 changes: 1 addition & 3 deletions test/LondonTravel.Skill.Tests/SessionEndedTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using Amazon.Lambda.Core;
using MartinCostello.LondonTravel.Skill.Models;

namespace MartinCostello.LondonTravel.Skill;
Expand All @@ -16,10 +15,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
await Verify(actual);
Expand Down
Loading

0 comments on commit e8035e8

Please sign in to comment.