-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add raw json sqs envelope mapper for incoming messages
- Loading branch information
1 parent
452d0ee
commit 1eee4a5
Showing
6 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/Samples/Diagnostics/DiagnosticsApp/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"profiles": { | ||
"DiagnosticsApp": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "https://localhost:55166;http://localhost:55167" | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Testing/OpenTelemetry/TracingTests/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"profiles": { | ||
"TracingTests": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "https://localhost:55164;http://localhost:55165" | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Transports/AWS/Wolverine.AmazonSqs.Tests/RawJsonSqsEnvelopeMapperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Text.Json; | ||
using Amazon.SQS.Model; | ||
using Wolverine.AmazonSqs.Internal; | ||
using Wolverine.Util; | ||
|
||
namespace Wolverine.AmazonSqs.Tests | ||
{ | ||
public class RawJsonSqsEnvelopeMapperTests | ||
{ | ||
[Fact] | ||
public void deserializes_json_message_with_specified_type() | ||
{ | ||
var sut = new RawJsonSqsEnvelopeMapper(typeof(TextDetected), new JsonSerializerOptions()); | ||
|
||
string body = @"{ | ||
""JobId"": ""fe0ffd41549670f4190cd4c07c94141aa9cb79519a23056721857e3157cd8bf1"", | ||
""Status"": ""SUCCEEDED"", | ||
""API"": ""StartDocumentTextDetection"", | ||
""Timestamp"": 1692958398261, | ||
""DocumentLocation"": { | ||
""S3ObjectName"": ""my.pdf"", | ||
""S3Bucket"": ""mybucket"" | ||
} | ||
}"; | ||
Message nativeSqsMessage = new Message | ||
{ | ||
Body = body, | ||
}; | ||
|
||
AmazonSqsEnvelope envelope = new AmazonSqsEnvelope(nativeSqsMessage); | ||
|
||
sut.ReadEnvelopeData(envelope, nativeSqsMessage.Body, nativeSqsMessage.MessageAttributes); | ||
|
||
Assert.Equal(typeof(TextDetected).ToMessageTypeName(), envelope.MessageType); | ||
} | ||
|
||
public class TextDetected | ||
{ | ||
public string JobId { get; set; } = string.Empty; | ||
|
||
public string JobTag { get; set; } = string.Empty; | ||
|
||
public string Status { get; set; } = string.Empty; | ||
|
||
public DocumentLocation DocumentLocation { get; set; } | ||
|
||
public long Timestamp { get; set; } = -1; | ||
|
||
public string Api { get; set; } = string.Empty; | ||
} | ||
|
||
public class DocumentLocation | ||
{ | ||
public string S3ObjectName { get; set; } = string.Empty; | ||
|
||
public string S3Bucket { get; set; } = string.Empty; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/Transports/AWS/Wolverine.AmazonSqs/RawJsonSqsEnvelopeMapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Text.Json; | ||
using Amazon.SQS.Model; | ||
using Wolverine.Transports; | ||
using Wolverine.Util; | ||
|
||
namespace Wolverine.AmazonSqs; | ||
|
||
internal class RawJsonSqsEnvelopeMapper : ISqsEnvelopeMapper | ||
{ | ||
private readonly Type _defaultMessageType; | ||
private readonly JsonSerializerOptions _serializerOptions; | ||
|
||
public RawJsonSqsEnvelopeMapper(Type defaultMessageType, JsonSerializerOptions serializerOptions) | ||
{ | ||
_defaultMessageType = defaultMessageType; | ||
_serializerOptions = serializerOptions; | ||
} | ||
|
||
public string BuildMessageBody(Envelope envelope) | ||
{ | ||
return JsonSerializer.Serialize( | ||
envelope.Message, | ||
_defaultMessageType, | ||
_serializerOptions); | ||
} | ||
|
||
public IEnumerable<KeyValuePair<string, MessageAttributeValue>> ToAttributes(Envelope envelope) | ||
{ | ||
yield return new KeyValuePair<string, MessageAttributeValue>(TransportConstants.ProtocolVersion, | ||
new MessageAttributeValue { StringValue = "1.0", DataType = "String" }); | ||
} | ||
|
||
public void ReadEnvelopeData(Envelope envelope, string messageBody, IDictionary<string, MessageAttributeValue> attributes) | ||
{ | ||
// assuming json serialized message | ||
envelope.MessageType = _defaultMessageType.ToMessageTypeName(); | ||
envelope.Message = JsonSerializer.Deserialize( | ||
messageBody, | ||
_defaultMessageType, | ||
_serializerOptions); | ||
} | ||
} |