Skip to content

Commit

Permalink
Added events and device variant
Browse files Browse the repository at this point in the history
- Added events
- Added device events
- Added user notifications (internal)
- Moved to 1.0.3
  • Loading branch information
alandoherty committed Jul 19, 2018
1 parent 559dbeb commit 4dc2416
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 7 deletions.
6 changes: 3 additions & 3 deletions samples/Example.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class Program
static async Task AsyncMain(string[] args) {
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);

var q = await cc.Devices.ListDevicesAsync();
var qq = await cc.Devices.ListDeviceEventsAsync(q[2].UUID);
}
}
}
6 changes: 6 additions & 0 deletions src/WifiPlug.Api/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public Uri BaseAddress {
/// Gets the group operations.
/// </summary>
public virtual IGroupOperations Groups { get; protected set; }

/// <summary>
/// Gets the event operations.
/// </summary>
public virtual IEventOperations Events { get; protected set; }
#endregion

#region Rest Methods
Expand Down Expand Up @@ -455,6 +460,7 @@ public ApiClient(string apiUrl) {
Users = new UserOperations(this);
Sessions = new SessionOperations(this);
Groups = new GroupOperations(this);
Events = new EventOperations(this);
}

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions src/WifiPlug.Api/Entities/DeviceEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public class DeviceEntity
[JsonProperty(PropertyName = "type", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Type { get; set; }

/// <summary>
/// Gets or sets the variant, always 8-digit hexidecimal.
/// </summary>
[JsonProperty(PropertyName = "variant", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Variant { get; set; }

/// <summary>
/// Gets or sets the number of users attached to the device.
/// </summary>
Expand Down
55 changes: 55 additions & 0 deletions src/WifiPlug.Api/Entities/EventEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace WifiPlug.Api.Entities
{
/// <summary>
/// Represents an event.
/// </summary>
public class EventEntity
{
/// <summary>
/// Gets or sets the timestamp.
/// </summary>
[JsonProperty("time_created")]
public DateTime Timestamp { get; set; }

/// <summary>
/// Gets or sets the timestamp.
/// </summary>
[JsonProperty("data_format")]
public string DataFormat { get; set; }

/// <summary>
/// Gets or sets the timestamp.
/// </summary>
[JsonProperty("data")]
public object Data { get; set; }

/// <summary>
/// Gets or sets the timestamp.
/// </summary>
[JsonProperty("type")]
public string Type { get; set; }

/// <summary>
/// Gets or sets the timestamp.
/// </summary>
[JsonProperty("uuid")]
public Guid UUID { get; set; }

/// <summary>
/// Gets or sets the timestamp.
/// </summary>
[JsonProperty("resource_uuid")]
public Guid ResourceUUID { get; set; }

/// <summary>
/// Gets or sets the resource type.
/// </summary>
[JsonProperty("resource_type")]
public string ResourceType { get; set; }
}
}
31 changes: 31 additions & 0 deletions src/WifiPlug.Api/Entities/EventResultsEntity.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 event scan.
/// </summary>
public class EventResultsEntity
{
/// <summary>
/// Gets or sets the events.
/// </summary>
[JsonProperty(PropertyName = "events")]
public EventEntity[] Events { 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 events.
/// </summary>
[JsonProperty(PropertyName = "total_event_count")]
public int TotalEvents { get; set; }
}
}
25 changes: 25 additions & 0 deletions src/WifiPlug.Api/Entities/UserNotificationAddEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace WifiPlug.Api.Entities
{
/// <summary>
/// Represents a request to add a notification token.
/// </summary>
public class UserNotificationAddEntity
{
/// <summary>
/// Gets or sets the token.
/// </summary>
[JsonProperty(PropertyName = "token")]
public string Token { get; set; }

/// <summary>
/// Gets or sets the device type.
/// </summary>
[JsonProperty(PropertyName = "device_type")]
public string DeviceType { get; set; }
}
}
42 changes: 42 additions & 0 deletions src/WifiPlug.Api/Operations/DeviceOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,48 @@ public class DeviceOperations : IDeviceOperations
return _client.RequestAsync(HttpMethod.Delete, $"device/{deviceUuid}/timer/{timerUuid}", null, cancellationToken);
}

/// <summary>
/// Scans the device events list.
/// </summary>
/// <param name="deviceUuid">The device 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>The event results.</returns>
public async Task<ScanResult<EventEntity>> ScanDeviceEventsAsync(Guid deviceUuid, 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 = $"device/{deviceUuid}/event?limit={limit}";

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

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

return new ScanResult<EventEntity>(entity.Events, entity.TotalEvents, entity.Cursor);
}

/// <summary>
/// Gets all device events. This may return alot of results, see <see cref="ScanDeviceEventsAsync"/> instead.
/// </summary>
/// <param name="deviceUuid">The device UUID.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>All events.</returns>
public async Task<EventEntity[]> ListDeviceEventsAsync(Guid deviceUuid, CancellationToken cancellationToken = default(CancellationToken)) {
ScanResult<EventEntity> scan = null;
List<EventEntity> events = new List<EventEntity>();

while (scan == null || !scan.Cursor.IsEnd) {
scan = await ScanDeviceEventsAsync(deviceUuid, 50, scan == null ? default(Cursor) : scan.Cursor, cancellationToken).ConfigureAwait(false);
events.AddRange(scan.Entities);
}

return events.ToArray();
}

internal DeviceOperations(ApiClient client) {
_client = client;
}
Expand Down
35 changes: 35 additions & 0 deletions src/WifiPlug.Api/Operations/EventOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WifiPlug.Api.Entities;

namespace WifiPlug.Api.Operations
{
/// <summary>
/// Provides operations for event resources.
/// </summary>
public class EventOperations : IEventOperations
{
/// <summary>
/// The API client.
/// </summary>
protected ApiClient _client;

/// <summary>
/// Gets a event by UUID.
/// </summary>
/// <param name="eventUuid">The UUID.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The event.</returns>
public Task<EventEntity> GetEventAsync(Guid eventUuid, CancellationToken cancellationToken = default(CancellationToken)) {
return _client.RequestJsonSerializedAsync<EventEntity>(HttpMethod.Get, $"event/{eventUuid}", cancellationToken);
}

internal EventOperations(ApiClient client) {
_client = client;
}
}
}
20 changes: 19 additions & 1 deletion src/WifiPlug.Api/Operations/IDeviceOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public interface IDeviceOperations
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
Task<ScanResult<DeviceUserEntity>> ScanDeviceUsersAsync(Guid deviceUuid, int limit = 50, Cursor cursor = default(Cursor), CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Adds a user to a device.
/// </summary>
Expand Down Expand Up @@ -274,5 +274,23 @@ public interface IDeviceOperations
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
Task DeleteDeviceTimerAsync(Guid deviceUuid, Guid timerUuid, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Scans the device events list.
/// </summary>
/// <param name="deviceUuid">The device 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>The event results.</returns>
Task<ScanResult<EventEntity>> ScanDeviceEventsAsync(Guid deviceUuid, int limit = 50, Cursor cursor = default(Cursor), CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Gets all device events. This may return alot of results, see <see cref="ScanDeviceEventsAsync"/> instead.
/// </summary>
/// <param name="deviceUuid">The device UUID.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>All events.</returns>
Task<EventEntity[]> ListDeviceEventsAsync(Guid deviceUuid, CancellationToken cancellationToken = default(CancellationToken));
}
}
23 changes: 23 additions & 0 deletions src/WifiPlug.Api/Operations/IEventOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WifiPlug.Api.Entities;

namespace WifiPlug.Api.Operations
{
/// <summary>
/// Defines an interface for performing event operations.
/// </summary>
public interface IEventOperations
{
/// <summary>
/// Gets a event by UUID.
/// </summary>
/// <param name="eventUuid">The UUID.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The event.</returns>
Task<EventEntity> GetEventAsync(Guid eventUuid, CancellationToken cancellationToken = default(CancellationToken));
}
}
9 changes: 9 additions & 0 deletions src/WifiPlug.Api/Operations/IUserOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,14 @@ public interface IUserOperations
/// <remarks>This is an internal API and is not guarenteed to be stable between versions.</remarks>
/// <returns></returns>
Task<UserEntity> ActivateUserAsync(ActivateUserEntity entity, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Adds a user notification token to the current user.
/// </summary>
/// <param name="entity">The entity.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <remarks>This operation is internal and won't work with normal API keys. Nor is it stable.</remarks>
/// <returns></returns>
Task AddUserNotificationAsync(UserNotificationAddEntity entity, CancellationToken cancellationToken = default(CancellationToken));
}
}
11 changes: 11 additions & 0 deletions src/WifiPlug.Api/Operations/UserOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ public class UserOperations : IUserOperations
return _client.RequestJsonSerializedAsync<ActivateUserEntity, UserEntity>(HttpMethod.Post, "user/activate", entity, cancellationToken);
}

/// <summary>
/// Adds a user notification token to the current user.
/// </summary>
/// <param name="entity">The entity.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <remarks>This operation is internal and won't work with normal API keys. Nor is it stable.</remarks>
/// <returns></returns>
public Task AddUserNotificationAsync(UserNotificationAddEntity entity, CancellationToken cancellationToken = default(CancellationToken)) {
return _client.RequestJsonSerializedAsync(HttpMethod.Post, "user/notification/add", entity, cancellationToken);
}

internal UserOperations(ApiClient client) {
_client = client;
}
Expand Down
6 changes: 3 additions & 3 deletions src/WifiPlug.Api/WifiPlug.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
<PackageProjectUrl>https://wifiplug.co.uk</PackageProjectUrl>
<RepositoryUrl>https://github.com/wifiplug/api-client-net</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<AssemblyVersion>0.1.0.2</AssemblyVersion>
<FileVersion>0.1.0.1</FileVersion>
<AssemblyVersion>0.1.0.3</AssemblyVersion>
<FileVersion>0.1.0.3</FileVersion>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageIconUrl>https://s3.eu-west-2.amazonaws.com/wifiplug-pub/nuget-icons/wifiplug.png</PackageIconUrl>
<PackageLicenseUrl>https://github.com/wifiplug/api-client-net/blob/master/LICENSE</PackageLicenseUrl>
<Version>1.0.2</Version>
<Version>1.0.3</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down

0 comments on commit 4dc2416

Please sign in to comment.