Skip to content

Commit

Permalink
Various improvements
Browse files Browse the repository at this point in the history
- Added IApiClient interface to ApiClient to make extending easier
- Fixed operations classes having an internal constructor preventing extension
- Added DeviceOperations.AddSetupTokenAsync
- Sealed some classes which will never get inherited from
- Moved to 0.1.5
  • Loading branch information
alandoherty committed Jul 30, 2018
1 parent 207f985 commit c253599
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 17 deletions.
3 changes: 2 additions & 1 deletion 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.Operations;
using WifiPlug.Api.Schema;

namespace Example.Cli
Expand All @@ -17,7 +18,7 @@ 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 p = await cc.PingAsync();
DeviceSetupTokenEntity entity = await cc.Devices.AddDeviceSetupTokenAsync();
}
}
}
4 changes: 2 additions & 2 deletions src/WifiPlug.Api/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace WifiPlug.Api
/// <summary>
/// Provides access to the WIFIPLUG API services and systems.
/// </summary>
public class ApiClient
public class ApiClient : IApiClient
{
#region Constants
internal const string API_URL = "https://api.wifiplug.co.uk/v1.0/";
Expand Down Expand Up @@ -411,7 +411,7 @@ await RequestAsync(method, path, content, cancellationToken).ConfigureAwait(fals
/// Pings the API to check if it's up.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
/// <returns>The ping response.</returns>
public async Task<string> PingAsync(CancellationToken cancellationToken = default(CancellationToken)) {
return await (await RequestAsync(HttpMethod.Get, "ping", null, cancellationToken)).Content.ReadAsStringAsync();
}
Expand Down
2 changes: 1 addition & 1 deletion src/WifiPlug.Api/ApiError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace WifiPlug.Api
/// <summary>
/// Represents a single API error.
/// </summary>
public class ApiError
public sealed class ApiError
{
#region Properties
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/WifiPlug.Api/ApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace WifiPlug.Api
/// <summary>
/// Represents a failure response from the API.
/// </summary>
public class ApiException : Exception
public sealed class ApiException : Exception
{
private ApiError[] _errors;
private HttpResponseMessage _response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace WifiPlug.Api.Converters
/// <summary>
/// Downgrades any ISO 8061 to not format milliseconds.
/// </summary>
internal class InaccurateIsoDateTimeConverter : IsoDateTimeConverter
internal sealed class InaccurateIsoDateTimeConverter : IsoDateTimeConverter
{
public InaccurateIsoDateTimeConverter() {
DateTimeFormat = "yyyy-MM-ddTHH:mm:ssZ";
Expand Down
2 changes: 1 addition & 1 deletion src/WifiPlug.Api/Converters/TimerItemEntityConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace WifiPlug.Api.Converters
/// <summary>
/// Provides functionality to convert between a repetition enum and JSON.
/// </summary>
internal class TimerItemEntityConverter : JsonConverter
internal sealed class TimerItemEntityConverter : JsonConverter
{
public override bool CanConvert(Type objectType) {
return objectType == typeof(TimerItemEntity) || objectType == typeof(TimerItemEntity[]);
Expand Down
2 changes: 1 addition & 1 deletion src/WifiPlug.Api/Converters/TimerRepetitionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace WifiPlug.Api.Converters
/// <summary>
/// Provides functionality to convert between a repetition enum and JSON.
/// </summary>
internal class TimerRepetitionConverter : JsonConverter
internal sealed class TimerRepetitionConverter : JsonConverter
{
public override bool CanConvert(Type objectType) {
return objectType == typeof(TimerRepetition);
Expand Down
22 changes: 22 additions & 0 deletions src/WifiPlug.Api/Entities/DeviceSetupTokenEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace WifiPlug.Api.Entities
{
/// <summary>
/// Represents a device setup token.
/// </summary>
public class DeviceSetupTokenEntity
{
/// <summary>
/// Gets or sets the UUID.
/// </summary>
public Guid UUID { get; set; }

/// <summary>
/// Gets or sets the token.
/// </summary>
public string Token { get; set; }
}
}
78 changes: 78 additions & 0 deletions src/WifiPlug.Api/IApiClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WifiPlug.Api.Operations;

namespace WifiPlug.Api
{
/// <summary>
/// Defines the interface for an api client.
/// </summary>
public interface IApiClient
{
/// <summary>
/// Gets or sets the retry count for transient failures.
/// </summary>
int RetryCount { get; set; }

/// <summary>
/// Gets or sets the retry delay.
/// </summary>
TimeSpan RetryDelay { get; set; }

/// <summary>
/// Gets or sets the authentication method.
/// </summary>
ApiAuthentication Authentication { get; set; }

/// <summary>
/// Gets or sets the timeout for requests.
/// </summary>
TimeSpan Timeout { get; set; }

/// <summary>
/// Gets the underlying http client.
/// </summary>
HttpClient Client { get; }

/// <summary>
/// Gets or sets the base address.
/// </summary>
Uri BaseAddress { get; set; }

/// <summary>
/// Gets the device operations.
/// </summary>
IDeviceOperations Devices { get; }

/// <summary>
/// Gets the session operations.
/// </summary>
ISessionOperations Sessions { get; }

/// <summary>
/// Gets the user operations.
/// </summary>
IUserOperations Users { get; }

/// <summary>
/// Gets the group operations.
/// </summary>
IGroupOperations Groups { get; }

/// <summary>
/// Gets the event operations.
/// </summary>
IEventOperations Events { get; }

/// <summary>
/// Pings the API to check if it's up.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The ping response.</returns>
Task<string> PingAsync(CancellationToken cancellationToken = default(CancellationToken));
}
}
16 changes: 15 additions & 1 deletion src/WifiPlug.Api/Operations/DeviceOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,21 @@ public class DeviceOperations : IDeviceOperations
return events.ToArray();
}

internal DeviceOperations(ApiClient client) {
/// <summary>
/// Creates a device setup token.
/// </summary>
/// <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>The setup token.</returns>
public Task<DeviceSetupTokenEntity> AddDeviceSetupTokenAsync(CancellationToken cancellationToken = default(CancellationToken)) {
return _client.RequestJsonSerializedAsync<DeviceSetupTokenEntity>(HttpMethod.Post, $"device/setup_token/add", cancellationToken);
}

/// <summary>
/// Creates a device operations object.
/// </summary>
/// <param name="client">The client.</param>
protected internal DeviceOperations(ApiClient client) {
_client = client;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/WifiPlug.Api/Operations/EventOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public class EventOperations : IEventOperations
return _client.RequestJsonSerializedAsync<EventEntity>(HttpMethod.Get, $"event/{eventUuid}", cancellationToken);
}

internal EventOperations(ApiClient client) {
/// <summary>
/// Creates a event operations object.
/// </summary>
/// <param name="client">The client.</param>
protected internal EventOperations(ApiClient client) {
_client = client;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/WifiPlug.Api/Operations/GroupOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,11 @@ public class GroupOperations : IGroupOperations
return items.ToArray();
}

internal GroupOperations(ApiClient client) {
/// <summary>
/// Creates a group operations object.
/// </summary>
/// <param name="client">The client.</param>
protected internal GroupOperations(ApiClient client) {
_client = client;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/WifiPlug.Api/Operations/IDeviceOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,13 @@ public interface IDeviceOperations
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>All events.</returns>
Task<EventEntity[]> ListDeviceEventsAsync(Guid deviceUuid, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Creates a device setup token.
/// </summary>
/// <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>The setup token.</returns>
Task<DeviceSetupTokenEntity> AddDeviceSetupTokenAsync(CancellationToken cancellationToken = default(CancellationToken));
}
}
6 changes: 5 additions & 1 deletion src/WifiPlug.Api/Operations/SessionOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public class SessionOperations : ISessionOperations
}, cancellationToken);
}

internal SessionOperations(ApiClient client) {
/// <summary>
/// Creates a session operations object.
/// </summary>
/// <param name="client">The client.</param>
protected internal SessionOperations(ApiClient client) {
_client = client;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/WifiPlug.Api/Operations/UserOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ public class UserOperations : IUserOperations
return _client.RequestJsonSerializedAsync(HttpMethod.Post, "user/notification/add", entity, cancellationToken);
}

internal UserOperations(ApiClient client) {
/// <summary>
/// Creates a user operations object.
/// </summary>
/// <param name="client">The client.</param>
protected internal UserOperations(ApiClient client) {
_client = client;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/WifiPlug.Api/ScanResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace WifiPlug.Api
/// Represents the result of a scan operation.
/// </summary>
/// <typeparam name="TEntity">The entity type.</typeparam>
public class ScanResult<TEntity>
public sealed class ScanResult<TEntity>
{
#region Fields
private int _total;
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.4</AssemblyVersion>
<FileVersion>0.1.0.4</FileVersion>
<AssemblyVersion>0.1.0.5</AssemblyVersion>
<FileVersion>0.1.0.5</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.4</Version>
<Version>1.0.5</Version>
</PropertyGroup>

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

0 comments on commit c253599

Please sign in to comment.