Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addresses supporting byte[] in direct methods and making TwinProperties able to be directly converted to json #3340

Merged
merged 38 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
28ac489
changed object to byte[]
patilsnr Jun 15, 2023
6d33014
methods for byte[]
patilsnr Jun 15, 2023
97886e7
doc comments
patilsnr Jun 15, 2023
5020031
doc comments p2
patilsnr Jun 15, 2023
175d2a0
twinProperties to json
patilsnr Jun 21, 2023
719d0ac
response payload bytes
patilsnr Jun 28, 2023
3bb20c8
fix build errors
patilsnr Jun 28, 2023
acb7163
unit tests
patilsnr Jun 28, 2023
a813e61
net analyzers
patilsnr Jun 28, 2023
32dbcde
setter for payload
patilsnr Jun 30, 2023
b38923d
version number is now serialized correctly
patilsnr Jul 5, 2023
1c1f4fd
misc fixes
patilsnr Jul 6, 2023
adadd2e
green payload convention tests
patilsnr Jul 13, 2023
8e74d47
Delete haproxy.pem
patilsnr Jul 13, 2023
f735b44
direct method payload tests green
patilsnr Jul 13, 2023
48e57a5
digital twins tests green
patilsnr Jul 13, 2023
017dea0
method fault injection tests green
patilsnr Jul 13, 2023
c4b2e02
method amqp pooling tests green
patilsnr Jul 13, 2023
bf099e2
more tests green
patilsnr Jul 13, 2023
2203156
method tests green
patilsnr Jul 13, 2023
fb8bf21
more method tests
patilsnr Jul 13, 2023
9a9e1a2
etc
patilsnr Jul 13, 2023
b98a0fc
object overload
patilsnr Jul 18, 2023
54a1a7d
misc
patilsnr Jul 19, 2023
3ed2ff3
payload as object e2e test
patilsnr Jul 19, 2023
9ff00f5
doc comments
patilsnr Jul 19, 2023
d4827fb
green object overload test
patilsnr Jul 19, 2023
9451955
misc
patilsnr Jul 19, 2023
59f4118
misc2
patilsnr Jul 19, 2023
0b21380
default payload in ctor
patilsnr Jul 19, 2023
d07b825
fixes
patilsnr Jul 20, 2023
969c9b0
more
patilsnr Jul 20, 2023
6b2cc8f
update unit tests
patilsnr Jul 21, 2023
d2d0081
remove serialization layer on outdated test
patilsnr Jul 21, 2023
5f53c64
misc
patilsnr Jul 21, 2023
bcaa2bf
comments
patilsnr Jul 21, 2023
0a4d88d
fix unit test
patilsnr Jul 21, 2023
78b03fc
transport protocol back to websocket in e2e test
patilsnr Jul 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 80 additions & 2 deletions iothub/device/src/DirectMethod/EdgeModuleDirectMethodRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public EdgeModuleDirectMethodRequest(string methodName)
/// </summary>
/// <param name="methodName">The method name to invoke.</param>
/// <param name="payload">The direct method payload that will be serialized using <see cref="DefaultPayloadConvention"/>.</param>
public EdgeModuleDirectMethodRequest(string methodName, object payload)
public EdgeModuleDirectMethodRequest(string methodName, byte[] payload)
{
MethodName = methodName;
Payload = payload;
Expand Down Expand Up @@ -80,7 +80,7 @@ public EdgeModuleDirectMethodRequest(string methodName, object payload)
/// The direct method payload.
/// </summary>
[JsonProperty("payload", NullValueHandling = NullValueHandling.Include)]
internal object Payload { get; }
internal byte[] Payload { get; }
patilsnr marked this conversation as resolved.
Show resolved Hide resolved
patilsnr marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Method timeout, in seconds.
Expand All @@ -97,5 +97,83 @@ public EdgeModuleDirectMethodRequest(string methodName, object payload)
internal int? ConnectionTimeoutInSeconds => ConnectionTimeout.HasValue && ConnectionTimeout > TimeSpan.Zero
? (int)ConnectionTimeout.Value.TotalSeconds
: null;

/// <summary>
/// The direct method request payload, deserialized to the specified type.
/// </summary>
/// <remarks>
/// Use this method when the payload type is known and it can be deserialized using the configured
/// <see cref="PayloadConvention"/>. If it is not JSON or the type is not known, use <see cref="GetPayloadAsBytes"/>.
/// </remarks>
/// <typeparam name="T">The type to deserialize the direct method request payload to.</typeparam>
/// <param name="payload">When this method returns true, this contains the value of the direct method request payload.
/// When this method returns false, this contains the default value of the type <c>T</c> passed in.</param>
/// <returns><c>true</c> if the direct method request payload can be deserialized to type <c>T</c>; otherwise, <c>false</c>.</returns>
/// <example>
/// <code language="csharp">
/// await client.SetDirectMethodCallbackAsync((edgeModuleDirectMethodRequest) =>
/// {
/// if (edgeModuleDirectMethodRequest.TryGetPayload(out MyCustomType customTypePayload))
/// {
/// // do work
/// // ...
///
/// // Acknowlege the direct method call with the status code 200.
/// return Task.FromResult(new DirectMethodResponse(200));
/// }
/// else
/// {
/// // Acknowlege the direct method call the status code 400.
/// return Task.FromResult(new DirectMethodResponse(400));
/// }
///
///
/// // ...
/// },
/// cancellationToken);
/// </code>
/// </example>
public bool TryGetPayload<T>(out T payload)
{
payload = default;

try
{
payload = PayloadConvention.GetObject<T>(Payload);
return true;
}
catch (Exception ex) when (Logging.IsEnabled)
{
Logging.Error(this, $"Unable to convert payload to {typeof(T)} due to {ex}", nameof(TryGetPayload));
}

return false;
}

/// <summary>
/// Get the raw payload bytes.
/// </summary>
/// <remarks>
/// Use this method when the payload is not JSON or the type is not known or the type cannot be deserialized
/// using the configured <see cref="PayloadConvention"/>. Otherwise, use <see cref="TryGetPayload{T}(out T)"/>.
/// </remarks>
/// <returns>A copy of the raw payload as a byte array.</returns>
/// <example>
/// <code language="csharp">
/// await client.SetDirectMethodCallbackAsync((edgeModuleDirectMethodRequest) =>
/// {
/// byte[] arr = edgeModuleDirectMethodRequest.GetPayloadAsBytes();
/// // deserialize as needed and do work...
///
/// // Acknowlege the direct method call with the status code 200.
/// return Task.FromResult(new DirectMethodResponse(200));
/// },
/// cancellationToken);
/// </code>
/// </example>
public byte[] GetPayloadAsBytes()
{
return Payload == null || Payload.Length == 0 ? null : (byte[])Payload.Clone();
}
}
}
14 changes: 14 additions & 0 deletions iothub/device/src/Twin/TwinProperties.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Newtonsoft.Json;

namespace Microsoft.Azure.Devices.Client
{
/// <summary>
Expand All @@ -23,11 +25,23 @@ protected internal TwinProperties(DesiredProperties requestsFromService, Reporte
/// <summary>
/// The collection of desired property update requests received from service.
/// </summary>
[JsonProperty("desiredProperties")]
patilsnr marked this conversation as resolved.
Show resolved Hide resolved
public DesiredProperties Desired { get; }

/// <summary>
/// The collection of twin properties reported by the client.
/// </summary>
[JsonProperty("reportedProperties")]
public ReportedProperties Reported { get; }

/// <summary>
/// Gets the Twin as a JSON string
/// </summary>
/// <param name="formatting">Optional. Formatting for the output JSON string.</param>
/// <returns>JSON string</returns>
public string ToJson(Formatting formatting = Formatting.None)
patilsnr marked this conversation as resolved.
Show resolved Hide resolved
{
return JsonConvert.SerializeObject(this, formatting);
}
}
}