-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #476 from WildernessLabs/feature/device-client
Add new DeviceClient implementation
- Loading branch information
Showing
9 changed files
with
169 additions
and
9 deletions.
There are no files selected for viewing
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,32 @@ | ||
namespace Meadow.Cloud.Client.Devices; | ||
|
||
public class AddDeviceRequest(string id, string orgId, string publicKey) | ||
{ | ||
public AddDeviceRequest(string id, string name, string orgId, string publicKey) | ||
: this(id, orgId, publicKey) | ||
{ | ||
Name = name; | ||
} | ||
|
||
public AddDeviceRequest(string id, string name, string orgId, string collectionId, string publicKey) | ||
: this(id, orgId, publicKey) | ||
{ | ||
Name = name; | ||
CollectionId = collectionId; | ||
} | ||
|
||
[JsonPropertyName("id")] | ||
public string Id { get; set; } = id; | ||
|
||
[JsonPropertyName("name")] | ||
public string? Name { get; set; } | ||
|
||
[JsonPropertyName("orgId")] | ||
public string OrgId { get; set; } = orgId; | ||
|
||
[JsonPropertyName("collectionId")] | ||
public string? CollectionId { get; set; } | ||
|
||
[JsonPropertyName("publicKey")] | ||
public string PublicKey { get; set; } = publicKey; | ||
} |
16 changes: 16 additions & 0 deletions
16
Source/v2/Meadow.Cloud.Client/Devices/AddDeviceResponse.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,16 @@ | ||
namespace Meadow.Cloud.Client.Devices; | ||
|
||
public class AddDeviceResponse(string id, string name, string orgId, string collectionId) | ||
{ | ||
[JsonPropertyName("id")] | ||
public string Id { get; set; } = id; | ||
|
||
[JsonPropertyName("name")] | ||
public string? Name { get; set; } = name; | ||
|
||
[JsonPropertyName("orgId")] | ||
public string OrgId { get; set; } = orgId; | ||
|
||
[JsonPropertyName("collectionId")] | ||
public string? CollectionId { get; set; } = collectionId; | ||
} |
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 |
---|---|---|
@@ -1,9 +1,22 @@ | ||
namespace Meadow.Cloud.Client.Devices; | ||
|
||
public interface IDeviceClient | ||
public class DeviceClient : MeadowCloudClientBase, IDeviceClient | ||
{ | ||
} | ||
public DeviceClient(MeadowCloudContext meadowCloudContext, ILogger logger) | ||
: base(meadowCloudContext, logger) | ||
{ | ||
} | ||
|
||
public class DeviceClient : IDeviceClient | ||
{ | ||
} | ||
public async Task<AddDeviceResponse> AddDevice(AddDeviceRequest request, CancellationToken cancellationToken = default) | ||
{ | ||
if (request == null) | ||
{ | ||
throw new ArgumentNullException(nameof(request)); | ||
} | ||
|
||
using var httpRequest = CreateHttpRequestMessage(HttpMethod.Post, "api/v1/devices", request); | ||
using var httpResponse = await HttpClient.SendAsync(httpRequest, cancellationToken); | ||
|
||
return await ProcessResponse<AddDeviceResponse>(httpResponse, cancellationToken); | ||
} | ||
} |
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,6 @@ | ||
namespace Meadow.Cloud.Client.Devices; | ||
|
||
public interface IDeviceClient | ||
{ | ||
Task<AddDeviceResponse> AddDevice(AddDeviceRequest request, CancellationToken cancellationToken = default); | ||
} |
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
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
72 changes: 72 additions & 0 deletions
72
Source/v2/Tests/Meadow.Cloud.Client.Unit.Tests/DeviceClientTests/AddDeviceTests.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,72 @@ | ||
namespace Meadow.Cloud.Client.Unit.Tests.DeviceClientTests; | ||
|
||
public class AddDeviceTests | ||
{ | ||
private readonly FakeableHttpMessageHandler _handler; | ||
private readonly DeviceClient _deviceClient; | ||
|
||
public AddDeviceTests() | ||
{ | ||
_handler = A.Fake<FakeableHttpMessageHandler>(); | ||
var httpClient = new HttpClient(_handler) { BaseAddress = new Uri("https://example.org") }; | ||
|
||
A.CallTo(() => _handler | ||
.FakeSendAsync(A<HttpRequestMessage>.Ignored, A<CancellationToken>.Ignored)) | ||
.Returns(new HttpResponseMessage(HttpStatusCode.NotFound)); | ||
|
||
var context = new MeadowCloudContext(httpClient, new Uri("https://example.org"), new MeadowCloudUserAgent("Meadow.Cloud.Client.Unit.Tests")); | ||
_deviceClient = new DeviceClient(context, NullLogger.Instance); | ||
} | ||
|
||
[Fact] | ||
public async Task AddDevice_WithNullRequest_ShouldThrowException() | ||
{ | ||
// Act/Assert | ||
await Assert.ThrowsAsync<ArgumentNullException>(() => _deviceClient.AddDevice(null!)); | ||
} | ||
|
||
[Theory] | ||
[InlineData(HttpStatusCode.BadRequest)] | ||
[InlineData(HttpStatusCode.Conflict)] | ||
[InlineData(HttpStatusCode.NotFound)] | ||
[InlineData(HttpStatusCode.Unauthorized)] | ||
[InlineData(HttpStatusCode.InternalServerError)] | ||
public async Task AddDevice_WithUnsuccessfulResponse_ShouldThrowException(HttpStatusCode httpStatusCode) | ||
{ | ||
// Arrange | ||
var addDeviceRequest = new AddDeviceRequest("id", "orgId", "publicKey"); | ||
|
||
A.CallTo(() => _handler | ||
.FakeSendAsync( | ||
A<HttpRequestMessage>.That.Matches(r => r.RequestUri!.AbsolutePath == $"/api/v1/devices"), | ||
A<CancellationToken>.Ignored)) | ||
.Returns(new HttpResponseMessage(httpStatusCode)); | ||
|
||
// Act/Assert | ||
var ex = await Assert.ThrowsAsync<MeadowCloudException>(() => _deviceClient.AddDevice(addDeviceRequest)); | ||
Assert.Equal(httpStatusCode, ex.StatusCode); | ||
} | ||
|
||
[Fact] | ||
public async Task AddDevice_WithResponse_ShouldReturnResult() | ||
{ | ||
// Arrange | ||
var addDeviceRequest = new AddDeviceRequest("device-id", "device-org-id", "device-public-key"); | ||
|
||
A.CallTo(() => _handler | ||
.FakeSendAsync( | ||
A<HttpRequestMessage>.That.Matches(r => r.RequestUri!.AbsolutePath == $"/api/v1/devices"), | ||
A<CancellationToken>.Ignored)) | ||
.Returns(new HttpResponseMessage(HttpStatusCode.OK) | ||
{ | ||
Content = JsonContent.Create(new AddDeviceResponse("device-id", "name", "device-org-id", "device-collection-id")) | ||
}); | ||
|
||
// Act | ||
var response = await _deviceClient.AddDevice(addDeviceRequest); | ||
|
||
// Assert | ||
Assert.NotNull(response); | ||
Assert.Equal("device-id", response.Id); | ||
} | ||
} |
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