Skip to content

Commit c253599

Browse files
committed
Various improvements
- 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
1 parent 207f985 commit c253599

17 files changed

+156
-17
lines changed

samples/Example.Cli/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using WifiPlug.Api;
66
using WifiPlug.Api.Authentication;
77
using WifiPlug.Api.Entities;
8+
using WifiPlug.Api.Operations;
89
using WifiPlug.Api.Schema;
910

1011
namespace Example.Cli
@@ -17,7 +18,7 @@ static async Task AsyncMain(string[] args) {
1718
ApiClient cc = new ApiClient(Environment.GetEnvironmentVariable("API_KEY"), Environment.GetEnvironmentVariable("API_SECRET"));
1819
cc.Authentication = new SessionAuthentication(Environment.GetEnvironmentVariable("SESSION_TOKEN"));
1920

20-
var p = await cc.PingAsync();
21+
DeviceSetupTokenEntity entity = await cc.Devices.AddDeviceSetupTokenAsync();
2122
}
2223
}
2324
}

src/WifiPlug.Api/ApiClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace WifiPlug.Api
1717
/// <summary>
1818
/// Provides access to the WIFIPLUG API services and systems.
1919
/// </summary>
20-
public class ApiClient
20+
public class ApiClient : IApiClient
2121
{
2222
#region Constants
2323
internal const string API_URL = "https://api.wifiplug.co.uk/v1.0/";
@@ -411,7 +411,7 @@ await RequestAsync(method, path, content, cancellationToken).ConfigureAwait(fals
411411
/// Pings the API to check if it's up.
412412
/// </summary>
413413
/// <param name="cancellationToken">The cancellation token.</param>
414-
/// <returns></returns>
414+
/// <returns>The ping response.</returns>
415415
public async Task<string> PingAsync(CancellationToken cancellationToken = default(CancellationToken)) {
416416
return await (await RequestAsync(HttpMethod.Get, "ping", null, cancellationToken)).Content.ReadAsStringAsync();
417417
}

src/WifiPlug.Api/ApiError.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace WifiPlug.Api
77
/// <summary>
88
/// Represents a single API error.
99
/// </summary>
10-
public class ApiError
10+
public sealed class ApiError
1111
{
1212
#region Properties
1313
/// <summary>

src/WifiPlug.Api/ApiException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace WifiPlug.Api
77
/// <summary>
88
/// Represents a failure response from the API.
99
/// </summary>
10-
public class ApiException : Exception
10+
public sealed class ApiException : Exception
1111
{
1212
private ApiError[] _errors;
1313
private HttpResponseMessage _response;

src/WifiPlug.Api/Converters/InaccurateIsoDateTimeConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace WifiPlug.Api.Converters
88
/// <summary>
99
/// Downgrades any ISO 8061 to not format milliseconds.
1010
/// </summary>
11-
internal class InaccurateIsoDateTimeConverter : IsoDateTimeConverter
11+
internal sealed class InaccurateIsoDateTimeConverter : IsoDateTimeConverter
1212
{
1313
public InaccurateIsoDateTimeConverter() {
1414
DateTimeFormat = "yyyy-MM-ddTHH:mm:ssZ";

src/WifiPlug.Api/Converters/TimerItemEntityConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace WifiPlug.Api.Converters
1010
/// <summary>
1111
/// Provides functionality to convert between a repetition enum and JSON.
1212
/// </summary>
13-
internal class TimerItemEntityConverter : JsonConverter
13+
internal sealed class TimerItemEntityConverter : JsonConverter
1414
{
1515
public override bool CanConvert(Type objectType) {
1616
return objectType == typeof(TimerItemEntity) || objectType == typeof(TimerItemEntity[]);

src/WifiPlug.Api/Converters/TimerRepetitionConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace WifiPlug.Api.Converters
1010
/// <summary>
1111
/// Provides functionality to convert between a repetition enum and JSON.
1212
/// </summary>
13-
internal class TimerRepetitionConverter : JsonConverter
13+
internal sealed class TimerRepetitionConverter : JsonConverter
1414
{
1515
public override bool CanConvert(Type objectType) {
1616
return objectType == typeof(TimerRepetition);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace WifiPlug.Api.Entities
6+
{
7+
/// <summary>
8+
/// Represents a device setup token.
9+
/// </summary>
10+
public class DeviceSetupTokenEntity
11+
{
12+
/// <summary>
13+
/// Gets or sets the UUID.
14+
/// </summary>
15+
public Guid UUID { get; set; }
16+
17+
/// <summary>
18+
/// Gets or sets the token.
19+
/// </summary>
20+
public string Token { get; set; }
21+
}
22+
}

src/WifiPlug.Api/IApiClient.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net.Http;
4+
using System.Text;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using WifiPlug.Api.Operations;
8+
9+
namespace WifiPlug.Api
10+
{
11+
/// <summary>
12+
/// Defines the interface for an api client.
13+
/// </summary>
14+
public interface IApiClient
15+
{
16+
/// <summary>
17+
/// Gets or sets the retry count for transient failures.
18+
/// </summary>
19+
int RetryCount { get; set; }
20+
21+
/// <summary>
22+
/// Gets or sets the retry delay.
23+
/// </summary>
24+
TimeSpan RetryDelay { get; set; }
25+
26+
/// <summary>
27+
/// Gets or sets the authentication method.
28+
/// </summary>
29+
ApiAuthentication Authentication { get; set; }
30+
31+
/// <summary>
32+
/// Gets or sets the timeout for requests.
33+
/// </summary>
34+
TimeSpan Timeout { get; set; }
35+
36+
/// <summary>
37+
/// Gets the underlying http client.
38+
/// </summary>
39+
HttpClient Client { get; }
40+
41+
/// <summary>
42+
/// Gets or sets the base address.
43+
/// </summary>
44+
Uri BaseAddress { get; set; }
45+
46+
/// <summary>
47+
/// Gets the device operations.
48+
/// </summary>
49+
IDeviceOperations Devices { get; }
50+
51+
/// <summary>
52+
/// Gets the session operations.
53+
/// </summary>
54+
ISessionOperations Sessions { get; }
55+
56+
/// <summary>
57+
/// Gets the user operations.
58+
/// </summary>
59+
IUserOperations Users { get; }
60+
61+
/// <summary>
62+
/// Gets the group operations.
63+
/// </summary>
64+
IGroupOperations Groups { get; }
65+
66+
/// <summary>
67+
/// Gets the event operations.
68+
/// </summary>
69+
IEventOperations Events { get; }
70+
71+
/// <summary>
72+
/// Pings the API to check if it's up.
73+
/// </summary>
74+
/// <param name="cancellationToken">The cancellation token.</param>
75+
/// <returns>The ping response.</returns>
76+
Task<string> PingAsync(CancellationToken cancellationToken = default(CancellationToken));
77+
}
78+
}

src/WifiPlug.Api/Operations/DeviceOperations.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,21 @@ public class DeviceOperations : IDeviceOperations
474474
return events.ToArray();
475475
}
476476

477-
internal DeviceOperations(ApiClient client) {
477+
/// <summary>
478+
/// Creates a device setup token.
479+
/// </summary>
480+
/// <param name="cancellationToken">The cancellation token.</param>
481+
/// <remarks>This operation is internal and won't work with normal API keys. Nor is it stable.</remarks>
482+
/// <returns>The setup token.</returns>
483+
public Task<DeviceSetupTokenEntity> AddDeviceSetupTokenAsync(CancellationToken cancellationToken = default(CancellationToken)) {
484+
return _client.RequestJsonSerializedAsync<DeviceSetupTokenEntity>(HttpMethod.Post, $"device/setup_token/add", cancellationToken);
485+
}
486+
487+
/// <summary>
488+
/// Creates a device operations object.
489+
/// </summary>
490+
/// <param name="client">The client.</param>
491+
protected internal DeviceOperations(ApiClient client) {
478492
_client = client;
479493
}
480494
}

0 commit comments

Comments
 (0)