Skip to content

Commit 255cfbb

Browse files
authored
Merge pull request #13 from JSkimming/model-interfaces
Added interfaces for all the models
2 parents 35584b0 + ad01147 commit 255cfbb

File tree

7 files changed

+139
-17
lines changed

7 files changed

+139
-17
lines changed

src/Tesla.NET/Models/AccessTokenResponse.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ namespace Tesla.NET.Models
55
{
66
using System;
77
using System.Diagnostics;
8-
using Newtonsoft.Json;
98

109
/// <summary>
1110
/// The response to an access token or refresh token request.
1211
/// </summary>
1312
[DebuggerDisplay("{DebuggerDisplay,nq}")]
14-
public class AccessTokenResponse
13+
public class AccessTokenResponse : IAccessTokenResponse
1514
{
1615
/// <summary>
1716
/// Initializes a new instance of the <see cref="AccessTokenResponse"/> class.
@@ -40,49 +39,41 @@ public AccessTokenResponse(
4039
/// <summary>
4140
/// Gets the access token.
4241
/// </summary>
43-
[JsonProperty("access_token")]
4442
public string AccessToken { get; }
4543

4644
/// <summary>
4745
/// Gets the type of the <see cref="AccessToken"/>.
4846
/// </summary>
49-
[JsonProperty("token_type")]
5047
public string TokenType { get; }
5148

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

5854
/// <summary>
5955
/// Gets the expiry duration of the <see cref="AccessToken"/>.
6056
/// </summary>
61-
[JsonIgnore]
6257
public TimeSpan ExpiresInTimespan => TimeSpan.FromSeconds(ExpiresIn);
6358

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

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

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

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

8879
private string DebuggerDisplay => $"{GetType().Name}: {AccessToken.Substring(0, 6)}… Expires {ExpiresUtc:R}";
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) 2018 James Skimming. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace Tesla.NET.Models
5+
{
6+
using System;
7+
using Newtonsoft.Json;
8+
9+
/// <summary>
10+
/// The response to an access token or refresh token request.
11+
/// </summary>
12+
public interface IAccessTokenResponse
13+
{
14+
/// <summary>
15+
/// Gets the access token.
16+
/// </summary>
17+
[JsonProperty("access_token")]
18+
string AccessToken { get; }
19+
20+
/// <summary>
21+
/// Gets the type of the <see cref="AccessToken"/>.
22+
/// </summary>
23+
[JsonProperty("token_type")]
24+
string TokenType { get; }
25+
26+
/// <summary>
27+
/// Gets the expiry duration in seconds of the <see cref="AccessToken"/>.
28+
/// </summary>
29+
[JsonProperty("expires_in")]
30+
long ExpiresIn { get; }
31+
32+
/// <summary>
33+
/// Gets the expiry duration of the <see cref="AccessToken"/>.
34+
/// </summary>
35+
[JsonIgnore]
36+
TimeSpan ExpiresInTimespan { get; }
37+
38+
/// <summary>
39+
/// Gets the UTC <see cref="DateTime"/> when the <see cref="AccessToken"/> expires.
40+
/// </summary>
41+
[JsonIgnore]
42+
DateTime ExpiresUtc { get; }
43+
44+
/// <summary>
45+
/// Gets the Epoch timestamp when the <see cref="AccessToken"/> was issued.
46+
/// </summary>
47+
[JsonProperty("created_at")]
48+
long CreatedAt { get; }
49+
50+
/// <summary>
51+
/// Gets the UTC <see cref="DateTime"/> when the <see cref="AccessToken"/> was issued.
52+
/// </summary>
53+
[JsonIgnore]
54+
DateTime CreatedUtc { get; }
55+
56+
/// <summary>
57+
/// Gets the refresh token that can be used to acquire a new <see cref="AccessToken"/>.
58+
/// </summary>
59+
[JsonProperty("refresh_token")]
60+
string RefreshToken { get; }
61+
}
62+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2018 James Skimming. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace Tesla.NET.Models
5+
{
6+
using System;
7+
8+
/// <summary>
9+
/// The message response from the Tesla Owner API.
10+
/// </summary>
11+
/// <typeparam name="TData">The <see cref="Type"/> of the <see cref="Data"/>.</typeparam>
12+
public interface IMessageResponse<out TData> : IMessageResponse
13+
where TData : class
14+
{
15+
/// <summary>
16+
/// Gets the <typeparamref name="TData"/> object.
17+
/// </summary>
18+
TData Data { get; }
19+
}
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2018 James Skimming. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace Tesla.NET.Models
5+
{
6+
using System.Net;
7+
using Newtonsoft.Json;
8+
using Newtonsoft.Json.Linq;
9+
10+
/// <summary>
11+
/// The message response from the Tesla Owner API.
12+
/// </summary>
13+
public interface IMessageResponse
14+
{
15+
/// <summary>
16+
/// Gets the <see cref="HttpStatusCode"/>.
17+
/// </summary>
18+
HttpStatusCode HttpStatusCode { get; }
19+
20+
/// <summary>
21+
/// Gets the raw JSON of the <see cref="IMessageResponse"/>.
22+
/// </summary>
23+
JObject RawJson { get; }
24+
25+
/// <summary>
26+
/// Gets the raw JSON of the <see cref="IMessageResponse"/>.
27+
/// </summary>
28+
[JsonIgnore]
29+
string RawJsonAsString { get; }
30+
}
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2018 James Skimming. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace Tesla.NET.Models
5+
{
6+
using System;
7+
using Newtonsoft.Json;
8+
9+
/// <summary>
10+
/// The wrapper object for a response from the Tesla Owner API.
11+
/// </summary>
12+
/// <typeparam name="TResponse">The <see cref="Type"/> of the <see cref="Response"/>.</typeparam>
13+
public interface IResponseDataWrapper<out TResponse>
14+
{
15+
/// <summary>
16+
/// Gets the <typeparamref name="TResponse"/> object.
17+
/// </summary>
18+
[JsonProperty("response")]
19+
TResponse Response { get; }
20+
}
21+
}

src/Tesla.NET/Models/MessageResponse.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Tesla.NET.Models
1414
/// </summary>
1515
/// <typeparam name="TData">The <see cref="Type"/> of the <see cref="Data"/>.</typeparam>
1616
[DebuggerDisplay("{DebuggerDisplay,nq}")]
17-
public class MessageResponse<TData>
17+
public class MessageResponse<TData> : IMessageResponse<TData>
1818
where TData : class
1919
{
2020
private readonly JObject _rawJson;
@@ -38,14 +38,13 @@ public MessageResponse(HttpStatusCode httpStatusCode, JObject rawJson = null, TD
3838
public HttpStatusCode HttpStatusCode { get; }
3939

4040
/// <summary>
41-
/// Gets the raw JSON of the <see cref="Data"/>.
41+
/// Gets the raw JSON of the <see cref="IMessageResponse"/>.
4242
/// </summary>
4343
public JObject RawJson => (JObject)_rawJson?.DeepClone();
4444

4545
/// <summary>
46-
/// Gets the raw JSON of the <see cref="Data"/>.
46+
/// Gets the raw JSON of the <see cref="IMessageResponse"/>.
4747
/// </summary>
48-
[JsonIgnore]
4948
public string RawJsonAsString => _rawJson?.ToString(Formatting.None) ?? string.Empty;
5049

5150
/// <summary>

src/Tesla.NET/Models/ResponseDataWrapper.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ namespace Tesla.NET.Models
55
{
66
using System;
77
using System.Diagnostics;
8-
using Newtonsoft.Json;
98

109
/// <summary>
1110
/// The wrapper object for a response from the Tesla Owner API.
1211
/// </summary>
1312
/// <typeparam name="TResponse">The <see cref="Type"/> of the <see cref="Response"/>.</typeparam>
1413
[DebuggerDisplay("{DebuggerDisplay,nq}")]
15-
public class ResponseDataWrapper<TResponse>
14+
public class ResponseDataWrapper<TResponse> : IResponseDataWrapper<TResponse>
1615
{
1716
/// <summary>
1817
/// Initializes a new instance of the <see cref="ResponseDataWrapper{TResponse}"/> class.
@@ -26,7 +25,6 @@ public ResponseDataWrapper(TResponse response = default)
2625
/// <summary>
2726
/// Gets the <typeparamref name="TResponse"/> object.
2827
/// </summary>
29-
[JsonProperty("response")]
3028
public TResponse Response { get; }
3129

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

0 commit comments

Comments
 (0)