Skip to content

Commit 4b98492

Browse files
committed
refac: extract body string to constant
1 parent f64ba04 commit 4b98492

File tree

8 files changed

+30
-27
lines changed

8 files changed

+30
-27
lines changed

docs/source/nuget/packages.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ This package contains a middleware that parses a JSON to an object of an explici
273273

274274
Configuration
275275
^^^^^^^^^^^^^
276-
When you use the middleware, You need to specify the type you want to convert the JSON object into. The middleware will put the object in the AdditionalContext["Body"] property. To access the object, you will need to perform an explicit cast of that property.
276+
When you use the middleware, You need to specify the type you want to convert the JSON object into. The middleware will put the object in the AdditionalContext[Constants.BodyContextKey] property. To access the object, you will need to perform an explicit cast of that property.
277277

278278
Sample code
279279
^^^^^^^^^^^
@@ -288,7 +288,7 @@ A typical use of the middelware will look like this for Rest API::
288288

289289
protected override async Task<APIGatewayProxyResponse> Handle(APIGatewayProxyRequest apiEvent, MiddyNetContext context)
290290
{
291-
var foo = ((Foo)context.AdditionalContext["Body"]);
291+
var foo = ((Foo)context.AdditionalContext[Constants.BodyContextKey]);
292292
293293
// Do stuff with foo
294294

@@ -313,7 +313,7 @@ And like this for Http API::
313313

314314
protected override async Task<APIGatewayHttpApiV2ProxyResponse> Handle(APIGatewayHttpApiV2ProxyResponse apiEvent, MiddyNetContext context)
315315
{
316-
var foo = ((Foo)context.AdditionalContext["Body"]);
316+
var foo = ((Foo)context.AdditionalContext[Constants.BodyContextKey]);
317317

318318
// Do stuff with typed foo
319319

samples/HttpJsonBodyParser/Voxel.MiddyNet.HttpJsonBodyParserSample/HttpJsonBodyParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public HttpJsonBodyParser()
1515

1616
protected override Task<APIGatewayProxyResponse> Handle(APIGatewayProxyRequest lambdaEvent, MiddyNetContext context)
1717
{
18-
var person = ((Person) context.AdditionalContext["Body"]);
19-
context.Logger.Log(LogLevel.Info, "Function called", new LogProperty("body", person));
18+
var person = ((Person) context.AdditionalContext[Constants.BodyContextKey]);
19+
context.Logger.Log(LogLevel.Info, "Function called", new LogProperty(Constants.BodyContextKey, person));
2020
var result = new APIGatewayProxyResponse
2121
{
2222
StatusCode = 200,

samples/HttpV2JsonBodyParser/Voxel.MiddyNet.HttpV2JsonBodyParserSample/HttpV2JsonBodyParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public HttpV2JsonBodyParser()
1515

1616
protected override Task<APIGatewayHttpApiV2ProxyResponse> Handle(APIGatewayHttpApiV2ProxyRequest lambdaEvent, MiddyNetContext context)
1717
{
18-
var person = ((Person)context.AdditionalContext["Body"]);
19-
context.Logger.Log(LogLevel.Info, "Function called", new LogProperty("body", person));
18+
var person = ((Person)context.AdditionalContext[Constants.BodyContextKey]);
19+
context.Logger.Log(LogLevel.Info, "Function called", new LogProperty(Constants.BodyContextKey, person));
2020
var result = new APIGatewayHttpApiV2ProxyResponse
2121
{
2222
StatusCode = 200,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Voxel.MiddyNet.HttpJsonBodyParserMiddleware
2+
{
3+
public class Constants
4+
{
5+
public const string BodyContextKey = "Body";
6+
}
7+
}

src/Voxel.MiddyNet.HttpJsonBodyParserMiddleware/HttpJsonBodyParserMiddleware.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ namespace Voxel.MiddyNet.HttpJsonBodyParserMiddleware
88
{
99
public class HttpJsonBodyParserMiddleware<T> : ILambdaMiddleware<APIGatewayProxyRequest, APIGatewayProxyResponse>
1010
{
11-
private const string BodyContextKey = "Body";
12-
1311
public Task Before(APIGatewayProxyRequest lambdaEvent, MiddyNetContext context)
1412
{
1513
if (!HasJsonContentHeaders(lambdaEvent))
1614
{
17-
context.AdditionalContext.Add(BodyContextKey, lambdaEvent.Body);
15+
context.AdditionalContext.Add(Constants.BodyContextKey, lambdaEvent.Body);
1816
return Task.CompletedTask;
1917
}
2018

@@ -26,7 +24,7 @@ public Task Before(APIGatewayProxyRequest lambdaEvent, MiddyNetContext context)
2624
var source = JsonSerializer.Deserialize<T>(lambdaEvent.Body);
2725

2826

29-
context.AdditionalContext.Add(BodyContextKey, source);
27+
context.AdditionalContext.Add(Constants.BodyContextKey, source);
3028
return Task.CompletedTask;
3129
}
3230

src/Voxel.MiddyNet.HttpJsonBodyParserMiddleware/HttpV2JsonBodyParserMiddleware.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ namespace Voxel.MiddyNet.HttpJsonBodyParserMiddleware
88
{
99
public class HttpV2JsonBodyParserMiddleware<T> : ILambdaMiddleware<APIGatewayHttpApiV2ProxyRequest, APIGatewayHttpApiV2ProxyResponse>
1010
{
11-
private const string BodyContextKey = "Body";
12-
1311
public Task Before(APIGatewayHttpApiV2ProxyRequest lambdaEvent, MiddyNetContext context)
1412
{
1513
if (!HasJsonContentHeaders(lambdaEvent))
1614
{
17-
context.AdditionalContext.Add(BodyContextKey, lambdaEvent.Body);
15+
context.AdditionalContext.Add(Constants.BodyContextKey, lambdaEvent.Body);
1816
return Task.CompletedTask;
1917
}
2018

@@ -26,7 +24,7 @@ public Task Before(APIGatewayHttpApiV2ProxyRequest lambdaEvent, MiddyNetContext
2624
var source = JsonSerializer.Deserialize<T>(lambdaEvent.Body);
2725

2826

29-
context.AdditionalContext.Add(BodyContextKey, source);
27+
context.AdditionalContext.Add(Constants.BodyContextKey, source);
3028
return Task.CompletedTask;
3129
}
3230

test/Voxel.MiddyNet.HttpJsonBodyParserMiddleware.Tests/HttpJsonBodyParserMiddlewareShould.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public async Task ProcessTheJsonRequest()
3838
var middleware = new HttpJsonBodyParserMiddleware<TestObject>();
3939
await middleware.Before(request, context);
4040

41-
context.AdditionalContext.ContainsKey("Body").Should().BeTrue();
42-
context.AdditionalContext["Body"].Should().BeEquivalentTo(expectation);
41+
context.AdditionalContext.ContainsKey(Constants.BodyContextKey).Should().BeTrue();
42+
context.AdditionalContext[Constants.BodyContextKey].Should().BeEquivalentTo(expectation);
4343
}
4444

4545
[Fact]
@@ -67,8 +67,8 @@ public async Task NotProcessTheBodyIfNoHeaderIsPassed()
6767
var middleware = new HttpJsonBodyParserMiddleware<TestObject>();
6868
await middleware.Before(request, context);
6969

70-
context.AdditionalContext.ContainsKey("Body").Should().BeTrue();
71-
context.AdditionalContext["Body"].Should().Be(serializedExpectation);
70+
context.AdditionalContext.ContainsKey(Constants.BodyContextKey).Should().BeTrue();
71+
context.AdditionalContext[Constants.BodyContextKey].Should().Be(serializedExpectation);
7272
}
7373

7474
[Fact]
@@ -86,8 +86,8 @@ public async Task HandleABase64Body()
8686
var middleware = new HttpJsonBodyParserMiddleware<TestObject>();
8787
await middleware.Before(request, context);
8888

89-
context.AdditionalContext.ContainsKey("Body").Should().BeTrue();
90-
context.AdditionalContext["Body"].Should().BeEquivalentTo(expectation);
89+
context.AdditionalContext.ContainsKey(Constants.BodyContextKey).Should().BeTrue();
90+
context.AdditionalContext[Constants.BodyContextKey].Should().BeEquivalentTo(expectation);
9191
}
9292

9393
[Fact]

test/Voxel.MiddyNet.HttpJsonBodyParserMiddleware.Tests/HttpV2JsonBodyParserMiddlewareShould.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public async Task ProcessTheJsonRequest()
3838
var middleware = new HttpV2JsonBodyParserMiddleware<TestObject>();
3939
await middleware.Before(request, context);
4040

41-
context.AdditionalContext.ContainsKey("Body").Should().BeTrue();
42-
context.AdditionalContext["Body"].Should().BeEquivalentTo(expectation);
41+
context.AdditionalContext.ContainsKey(Constants.BodyContextKey).Should().BeTrue();
42+
context.AdditionalContext[Constants.BodyContextKey].Should().BeEquivalentTo(expectation);
4343
}
4444

4545
[Fact]
@@ -67,8 +67,8 @@ public async Task NotProcessTheBodyIfNoHeaderIsPassed()
6767
var middleware = new HttpV2JsonBodyParserMiddleware<TestObject>();
6868
await middleware.Before(request, context);
6969

70-
context.AdditionalContext.ContainsKey("Body").Should().BeTrue();
71-
context.AdditionalContext["Body"].Should().Be(serializedExpectation);
70+
context.AdditionalContext.ContainsKey(Constants.BodyContextKey).Should().BeTrue();
71+
context.AdditionalContext[Constants.BodyContextKey].Should().Be(serializedExpectation);
7272
}
7373

7474
[Fact]
@@ -86,8 +86,8 @@ public async Task HandleABase64Body()
8686
var middleware = new HttpV2JsonBodyParserMiddleware<TestObject>();
8787
await middleware.Before(request, context);
8888

89-
context.AdditionalContext.ContainsKey("Body").Should().BeTrue();
90-
context.AdditionalContext["Body"].Should().BeEquivalentTo(expectation);
89+
context.AdditionalContext.ContainsKey(Constants.BodyContextKey).Should().BeTrue();
90+
context.AdditionalContext[Constants.BodyContextKey].Should().BeEquivalentTo(expectation);
9191
}
9292

9393
[Fact]

0 commit comments

Comments
 (0)