Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added interfaces for all the models #13

Merged
merged 1 commit into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/Tesla.NET/Models/AccessTokenResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ namespace Tesla.NET.Models
{
using System;
using System.Diagnostics;
using Newtonsoft.Json;

/// <summary>
/// The response to an access token or refresh token request.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class AccessTokenResponse
public class AccessTokenResponse : IAccessTokenResponse
{
/// <summary>
/// Initializes a new instance of the <see cref="AccessTokenResponse"/> class.
Expand Down Expand Up @@ -40,49 +39,41 @@ public AccessTokenResponse(
/// <summary>
/// Gets the access token.
/// </summary>
[JsonProperty("access_token")]
public string AccessToken { get; }

/// <summary>
/// Gets the type of the <see cref="AccessToken"/>.
/// </summary>
[JsonProperty("token_type")]
public string TokenType { get; }

/// <summary>
/// Gets the expiry duration in seconds of the <see cref="AccessToken"/>.
/// </summary>
[JsonProperty("expires_in")]
public long ExpiresIn { get; }

/// <summary>
/// Gets the expiry duration of the <see cref="AccessToken"/>.
/// </summary>
[JsonIgnore]
public TimeSpan ExpiresInTimespan => TimeSpan.FromSeconds(ExpiresIn);

/// <summary>
/// Gets the UTC <see cref="DateTime"/> when the <see cref="AccessToken"/> expires.
/// </summary>
[JsonIgnore]
public DateTime ExpiresUtc => EpochConversion.FromSeconds(CreatedAt + ExpiresIn);

/// <summary>
/// Gets the Epoch timestamp when the <see cref="AccessToken"/> was issued.
/// </summary>
[JsonProperty("created_at")]
public long CreatedAt { get; }

/// <summary>
/// Gets the UTC <see cref="DateTime"/> when the <see cref="AccessToken"/> was issued.
/// </summary>
[JsonIgnore]
public DateTime CreatedUtc => EpochConversion.FromSeconds(CreatedAt);

/// <summary>
/// Gets the refresh token that can be used to acquire a new <see cref="AccessToken"/>.
/// </summary>
[JsonProperty("refresh_token")]
public string RefreshToken { get; }

private string DebuggerDisplay => $"{GetType().Name}: {AccessToken.Substring(0, 6)}… Expires {ExpiresUtc:R}";
Expand Down
62 changes: 62 additions & 0 deletions src/Tesla.NET/Models/IAccessTokenResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2018 James Skimming. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Tesla.NET.Models
{
using System;
using Newtonsoft.Json;

/// <summary>
/// The response to an access token or refresh token request.
/// </summary>
public interface IAccessTokenResponse
{
/// <summary>
/// Gets the access token.
/// </summary>
[JsonProperty("access_token")]
string AccessToken { get; }

/// <summary>
/// Gets the type of the <see cref="AccessToken"/>.
/// </summary>
[JsonProperty("token_type")]
string TokenType { get; }

/// <summary>
/// Gets the expiry duration in seconds of the <see cref="AccessToken"/>.
/// </summary>
[JsonProperty("expires_in")]
long ExpiresIn { get; }

/// <summary>
/// Gets the expiry duration of the <see cref="AccessToken"/>.
/// </summary>
[JsonIgnore]
TimeSpan ExpiresInTimespan { get; }

/// <summary>
/// Gets the UTC <see cref="DateTime"/> when the <see cref="AccessToken"/> expires.
/// </summary>
[JsonIgnore]
DateTime ExpiresUtc { get; }

/// <summary>
/// Gets the Epoch timestamp when the <see cref="AccessToken"/> was issued.
/// </summary>
[JsonProperty("created_at")]
long CreatedAt { get; }

/// <summary>
/// Gets the UTC <see cref="DateTime"/> when the <see cref="AccessToken"/> was issued.
/// </summary>
[JsonIgnore]
DateTime CreatedUtc { get; }

/// <summary>
/// Gets the refresh token that can be used to acquire a new <see cref="AccessToken"/>.
/// </summary>
[JsonProperty("refresh_token")]
string RefreshToken { get; }
}
}
20 changes: 20 additions & 0 deletions src/Tesla.NET/Models/IMessageResponse.Generic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2018 James Skimming. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Tesla.NET.Models
{
using System;

/// <summary>
/// The message response from the Tesla Owner API.
/// </summary>
/// <typeparam name="TData">The <see cref="Type"/> of the <see cref="Data"/>.</typeparam>
public interface IMessageResponse<out TData> : IMessageResponse
where TData : class
{
/// <summary>
/// Gets the <typeparamref name="TData"/> object.
/// </summary>
TData Data { get; }
}
}
31 changes: 31 additions & 0 deletions src/Tesla.NET/Models/IMessageResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2018 James Skimming. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Tesla.NET.Models
{
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

/// <summary>
/// The message response from the Tesla Owner API.
/// </summary>
public interface IMessageResponse
{
/// <summary>
/// Gets the <see cref="HttpStatusCode"/>.
/// </summary>
HttpStatusCode HttpStatusCode { get; }

/// <summary>
/// Gets the raw JSON of the <see cref="IMessageResponse"/>.
/// </summary>
JObject RawJson { get; }

/// <summary>
/// Gets the raw JSON of the <see cref="IMessageResponse"/>.
/// </summary>
[JsonIgnore]
string RawJsonAsString { get; }
}
}
21 changes: 21 additions & 0 deletions src/Tesla.NET/Models/IResponseDataWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2018 James Skimming. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace Tesla.NET.Models
{
using System;
using Newtonsoft.Json;

/// <summary>
/// The wrapper object for a response from the Tesla Owner API.
/// </summary>
/// <typeparam name="TResponse">The <see cref="Type"/> of the <see cref="Response"/>.</typeparam>
public interface IResponseDataWrapper<out TResponse>
{
/// <summary>
/// Gets the <typeparamref name="TResponse"/> object.
/// </summary>
[JsonProperty("response")]
TResponse Response { get; }
}
}
7 changes: 3 additions & 4 deletions src/Tesla.NET/Models/MessageResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Tesla.NET.Models
/// </summary>
/// <typeparam name="TData">The <see cref="Type"/> of the <see cref="Data"/>.</typeparam>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class MessageResponse<TData>
public class MessageResponse<TData> : IMessageResponse<TData>
where TData : class
{
private readonly JObject _rawJson;
Expand All @@ -38,14 +38,13 @@ public MessageResponse(HttpStatusCode httpStatusCode, JObject rawJson = null, TD
public HttpStatusCode HttpStatusCode { get; }

/// <summary>
/// Gets the raw JSON of the <see cref="Data"/>.
/// Gets the raw JSON of the <see cref="IMessageResponse"/>.
/// </summary>
public JObject RawJson => (JObject)_rawJson?.DeepClone();

/// <summary>
/// Gets the raw JSON of the <see cref="Data"/>.
/// Gets the raw JSON of the <see cref="IMessageResponse"/>.
/// </summary>
[JsonIgnore]
public string RawJsonAsString => _rawJson?.ToString(Formatting.None) ?? string.Empty;

/// <summary>
Expand Down
4 changes: 1 addition & 3 deletions src/Tesla.NET/Models/ResponseDataWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ namespace Tesla.NET.Models
{
using System;
using System.Diagnostics;
using Newtonsoft.Json;

/// <summary>
/// The wrapper object for a response from the Tesla Owner API.
/// </summary>
/// <typeparam name="TResponse">The <see cref="Type"/> of the <see cref="Response"/>.</typeparam>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class ResponseDataWrapper<TResponse>
public class ResponseDataWrapper<TResponse> : IResponseDataWrapper<TResponse>
{
/// <summary>
/// Initializes a new instance of the <see cref="ResponseDataWrapper{TResponse}"/> class.
Expand All @@ -26,7 +25,6 @@ public ResponseDataWrapper(TResponse response = default)
/// <summary>
/// Gets the <typeparamref name="TResponse"/> object.
/// </summary>
[JsonProperty("response")]
public TResponse Response { get; }

private string DebuggerDisplay => $"{GetType().Name}: {typeof(TResponse).Name}";
Expand Down