One of the main problems building a Cloud Identity Translation Gateway (like my TTIV3AzureIoTConnector) is getting enough devices to make testing (esp. scalability) realistic. This is a problem because I have only got two devices, a Sparkfun Satellite Transceiver Breakout – Swarm M138 and a Swarm Asset Tracker. (Considering buying a Swarm Eval Kit)
So, I can simulate lots of devices and test more complex configurations I have started build a Swarm Bumble Bee Hive emulator based on the API and Delivery-API OpenAPI files.
As well as generating clients NSwagStudio can also generate ASP.NET Core web APIs. To test my approach, I built the simplest possible client I could which calls the generated PostLoginAsync and GetDeviceCountAsync.
Initially the BumblebeeHiveBasicClientConsole login method would fail with an HTTP 415 Unsupported Media Type error.
After some trial and error, I modified the HiveController.cs and HiveControllerImplementation.cs Login method signatures so the payload was “application/x-www-form-urlencoded” rather than “application/json” by changing FromBody to FromForm
Task<Response> IAuthController.PostLoginAsync([FromForm] LoginForm body)
{
return Task.FromResult(new Response()
{
Token = Guid.NewGuid().ToString()
});
}
Modifying code generated by a tool like NSwagStudio should be avoided but I couldn’t work out a simpler solution
/// <summary>
/// POST login
/// </summary>
/// <remarks>
/// <p>Use username and password to log in.</p><p>On success: returns status code 200. The response body is the JSON <code>{"token": "&lt;token&gt;"}</code>, along with the header <code>Set-Cookie: JSESSIONID=&lt;token&gt;; Path=/; Secure; HttpOnly;</code>. The tokens in the return value and the <code>Set-Cookie</code> header are the same. The token is a long string of letters, numbers, and punctuation.</p><p>On failure: returns status code 401.</p><p>To make authenticated requests, there are two ways: <ul><li>(Preferred) Use the token as a Bearer Authentication token by including the HTTP header <code>Authorization: Bearer &lt;token&gt;</code> in further requests.</li><li>(Deprecated) Use the token as the JSESSIONID cookie in further requests.</li></ul></p>
/// </remarks>
/// <returns>Login success</returns>
[Microsoft.AspNetCore.Mvc.HttpPost, Microsoft.AspNetCore.Mvc.Route("login")]
public System.Threading.Tasks.Task<Response> PostLogin([Microsoft.AspNetCore.Mvc.FromForm] LoginForm body)
{
return _implementation.PostLoginAsync(body);
}
After some initial problems with content-types the Swarm Hive API (not tried the Delivery-API yet) appears to be documented and easy to use. Though, some of the variable type choices do seem a bit odd.
public virtual async System.Threading.Tasks.Task<string> GetDeviceCountAsync(int? devicetype, System.Threading.CancellationToken cancellationToken)