Skip to content

Commit

Permalink
Added group item listing/scanning
Browse files Browse the repository at this point in the history
- Moved to 1.0.2
  • Loading branch information
alandoherty committed Jul 16, 2018
1 parent cd26a3c commit d82476a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 10 deletions.
14 changes: 6 additions & 8 deletions samples/Example.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using WifiPlug.Api;
using WifiPlug.Api.Authentication;
using WifiPlug.Api.Entities;
using WifiPlug.Api.Schema;

namespace Example.Cli
{
Expand All @@ -13,14 +14,11 @@ class Program
static void Main(string[] args) => AsyncMain(args).Wait();

static async Task AsyncMain(string[] args) {
ApiClient client = new ApiClient(Environment.GetEnvironmentVariable("API_KEY"), Environment.GetEnvironmentVariable("API_SECRET"));
client.Authentication = new SessionAuthentication(Environment.GetEnvironmentVariable("SESSION_TOKEN"));

var l = await client.Devices.ListDevicesAsync();

foreach(var device in l) {
Console.WriteLine($"Device: {device.Name} IsOnline: {device.IsOnline}");
}
ApiClient cc = new ApiClient(Environment.GetEnvironmentVariable("API_KEY"), Environment.GetEnvironmentVariable("API_SECRET"));
cc.Authentication = new SessionAuthentication(Environment.GetEnvironmentVariable("SESSION_TOKEN"));

var q = await cc.Groups.ListGroupsAsync();
var i = await cc.Groups.ListGroupItemsAsync(q[1].UUID);
}
}
}
31 changes: 31 additions & 0 deletions src/WifiPlug.Api/Entities/GroupItemResultsEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace WifiPlug.Api.Entities
{
/// <summary>
/// Represents the result of a group item scan.
/// </summary>
public class GroupItemResultsEntity
{
/// <summary>
/// Gets or sets the items in the results.
/// </summary>
[JsonProperty(PropertyName = "items")]
public GroupItemEntity[] Items { get; set; }

/// <summary>
/// Gets or sets the next cursor.
/// </summary>
[JsonProperty(PropertyName = "cursor")]
public string Cursor { get; set; }

/// <summary>
/// Gets or sets the total number of group items.
/// </summary>
[JsonProperty(PropertyName = "total_item_count")]
public int TotalItems { get; set; }
}
}
45 changes: 43 additions & 2 deletions src/WifiPlug.Api/Operations/GroupOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ public class GroupOperations : IGroupOperations

return timers.ToArray();
}



/// <summary>
/// Adds a timer to the group.
/// </summary>
Expand Down Expand Up @@ -280,6 +279,48 @@ public class GroupOperations : IGroupOperations
return _client.RequestJsonSerializedAsync<TimerEntity>(HttpMethod.Get, $"group/{groupUuid}/timer/{timerUuid}", cancellationToken);
}

/// <summary>
/// Scans the group item list.
/// </summary>
/// <param name="groupUuid">The group UUID.</param>
/// <param name="limit">The limit, maximum of 50.</param>
/// <param name="cursor">The previously returned cursor.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<ScanResult<GroupItemEntity>> ScanGroupItemsAsync(Guid groupUuid, int limit = 50, Cursor cursor = default(Cursor), CancellationToken cancellationToken = default(CancellationToken)) {
if (limit < 0 || limit > 50)
throw new ArgumentOutOfRangeException(nameof(limit), "The limit cannot be larger than 50");

// build uri
string uri = $"group/{groupUuid}/item?limit={limit}";

if (!cursor.IsEnd)
uri += string.Format("&cursor={0}", WebUtility.UrlEncode(cursor.Token));

// load results for current cursor
GroupItemResultsEntity entity = await _client.RequestJsonSerializedAsync<GroupItemResultsEntity>(HttpMethod.Get, uri, cancellationToken).ConfigureAwait(false);

return new ScanResult<GroupItemEntity>(entity.Items, entity.TotalItems, entity.Cursor);
}

/// <summary>
/// Gets all group items.
/// </summary>
/// <param name="groupUuid">The group UUID.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<GroupItemEntity[]> ListGroupItemsAsync(Guid groupUuid, CancellationToken cancellationToken = default(CancellationToken)) {
ScanResult<GroupItemEntity> scan = null;
List<GroupItemEntity> items = new List<GroupItemEntity>();

while (scan == null || !scan.Cursor.IsEnd) {
scan = await ScanGroupItemsAsync(groupUuid, 1, scan == null ? default(Cursor) : scan.Cursor, cancellationToken).ConfigureAwait(false);
items.AddRange(scan.Entities);
}

return items.ToArray();
}

internal GroupOperations(ApiClient client) {
_client = client;
}
Expand Down
18 changes: 18 additions & 0 deletions src/WifiPlug.Api/Operations/IGroupOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,23 @@ public interface IGroupOperations
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
Task<TimerEntity> GetGroupTimerAsync(Guid groupUuid, Guid timerUuid, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Scans the group item list.
/// </summary>
/// <param name="groupUuid">The group UUID.</param>
/// <param name="limit">The limit, maximum of 50.</param>
/// <param name="cursor">The previously returned cursor.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
Task<ScanResult<GroupItemEntity>> ScanGroupItemsAsync(Guid groupUuid, int limit = 50, Cursor cursor = default(Cursor), CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Gets all group items.
/// </summary>
/// <param name="groupUuid">The group UUID.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
Task<GroupItemEntity[]> ListGroupItemsAsync(Guid groupUuid, CancellationToken cancellationToken = default(CancellationToken));
}
}

0 comments on commit d82476a

Please sign in to comment.