Skip to content

Commit 133d4d5

Browse files
committed
Refactor BlinkClient for simplified authorization
Simplified BlinkClient instantiation by removing constructor overloads and handling authorization directly in AuthorizeAsync. Updated Program.cs to reflect these changes. Removed BlinkAuthorizationData and integrated its functionality into LoginResult, which now includes a ToJson method. Updated IBlinkClient interface and added exception handling for required fields in BlinkClient methods.
1 parent 132bfda commit 133d4d5

File tree

5 files changed

+47
-106
lines changed

5 files changed

+47
-106
lines changed

Sources/Blink.ConsoleTest/Program.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ private static async Task Start()
1313
{
1414
string json = File.ReadAllText("secrets.json");
1515
var secrets = JsonSerializer.Deserialize<Secrets>(json)!;
16-
BlinkClient client = string.IsNullOrEmpty(secrets.Token) ?
17-
new BlinkClient(secrets.Email, secrets.Password) :
18-
new(secrets.Email, secrets.Password, secrets.Token);
19-
20-
var authData = await client.AuthorizeAsync(reauth: string.IsNullOrEmpty(secrets.Token));
16+
BlinkClient client = new();
17+
var authData = await client.AuthorizeAsync(secrets.Email, secrets.Password, reauth: string.IsNullOrEmpty(secrets.Token));
2118

2219
if (string.IsNullOrEmpty(secrets.Token))
2320
{

Sources/Blink/BlinkClient.cs

Lines changed: 26 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ public class BlinkClient : IBlinkClient
2626
private int? _clientId;
2727
private int? _accountId;
2828
private HttpClient? _http;
29-
private readonly string _email;
30-
private readonly string _password;
31-
private const string _baseUrl = "https://rest-prod.immedia-semi.com";
29+
private readonly string _uniqueId = "15c204d5-1577-4825-9bc8-b09efe619f00";
3230

3331
/// <summary>
34-
/// Create Blink client with email and password.
32+
/// Authorize with email and password provided in constructor.
3533
/// </summary>
36-
/// <param name="email">Email</param>
37-
/// <param name="password">Password</param>
38-
public BlinkClient(string email, string password)
34+
/// <returns><see cref="LoginResult"/> object with authorization data</returns>
35+
/// <exception cref="BlinkClientException">Thrown when email or password is not provided in constructor</exception>
36+
/// <exception cref="BlinkClientException">Thrown when authorization fails</exception>
37+
/// <exception cref="BlinkClientException">Thrown when no content is returned</exception>
38+
public async Task<LoginResult> AuthorizeAsync(string email, string password, bool reauth = true)
3939
{
4040
if (string.IsNullOrWhiteSpace(email))
4141
{
@@ -45,55 +45,24 @@ public BlinkClient(string email, string password)
4545
{
4646
throw new BlinkClientException("Password is required to authorize");
4747
}
48-
_email = email;
49-
_password = password;
50-
}
51-
52-
/// <summary>
53-
/// Create Blink client with token, tier, client ID and account ID from
54-
/// <see cref="BlinkAuthorizationData"/> object returned by <see cref="AuthorizeAsync"/> method.
55-
/// </summary>
56-
/// <param name="email">Email</param>
57-
/// <param name="password">Password</param>
58-
/// <param name="token">Authorization token</param>
59-
public BlinkClient(string email, string password, string token) : this(email, password)
60-
{
61-
if (string.IsNullOrWhiteSpace(token))
62-
{
63-
throw new BlinkClientException("Token is required to authorize");
64-
}
65-
_http = CreateHttpClient(_baseUrl, token);
66-
}
6748

68-
/// <summary>
69-
/// Authorize with email and password provided in constructor.
70-
/// </summary>
71-
/// <returns><see cref="BlinkAuthorizationData"/> object with authorization data</returns>
72-
/// <exception cref="BlinkClientException">Thrown when email or password is not provided in constructor</exception>
73-
/// <exception cref="BlinkClientException">Thrown when authorization fails</exception>
74-
/// <exception cref="BlinkClientException">Thrown when no content is returned</exception>
75-
public async Task<BlinkAuthorizationData> AuthorizeAsync(bool reauth = false)
76-
{
77-
if (string.IsNullOrWhiteSpace(_email) || string.IsNullOrWhiteSpace(_password))
78-
{
79-
throw new BlinkClientException("Email and password are required in constructor to authorize");
80-
}
81-
82-
string appName = Assembly.GetEntryAssembly()!.GetName().Name + " v" + Assembly.GetEntryAssembly()!.GetName().Version;
49+
string clientName = Assembly.GetEntryAssembly()!.GetName().Name + "_v" + Assembly.GetEntryAssembly()!.GetName().Version;
8350
var body = new
8451
{
85-
unique_id = appName,
86-
email = _email,
87-
password = _password,
88-
client_name = "Blink.NET Library",
89-
reauth,
52+
unique_id = _uniqueId,
53+
email,
54+
password,
55+
client_name = clientName,
56+
reauth = reauth ? "true" : "false",
9057

9158
//app_version = "",
9259
//client_type = "",
9360
//device_identifier = "Amazon ",
9461
//notification_key = "",
9562
//os_version = "",
9663
};
64+
const string _baseUrl = "https://rest-prod.immedia-semi.com";
65+
_http = CreateHttpClient(_baseUrl);
9766
var httpClient = GetHttpClient();
9867
var response = await httpClient.PostAsJsonAsync("/api/v5/account/login", body);
9968
if (!response.IsSuccessStatusCode)
@@ -113,16 +82,14 @@ public async Task<BlinkAuthorizationData> AuthorizeAsync(bool reauth = false)
11382
}
11483
_accountId = loginResult.Account.AccountId;
11584
_clientId = loginResult.Account.ClientId;
116-
return new BlinkAuthorizationData
85+
if (_accountId == null || _clientId == null)
11786
{
118-
AccountId = loginResult.Account.AccountId,
119-
ClientId = loginResult.Account.ClientId,
120-
Tier = loginResult.Account.Tier,
121-
Token = loginResult.Auth.Token
122-
};
87+
throw new BlinkClientException("Failed to authorize - no account ID or client ID in response");
88+
}
89+
return loginResult;
12390
}
12491

125-
private HttpClient CreateHttpClient(string baseUrl, string token = "")
92+
private HttpClient CreateHttpClient(string baseUrl, string? token = "")
12693
{
12794
if (string.IsNullOrWhiteSpace(baseUrl))
12895
{
@@ -155,7 +122,11 @@ public async Task VerifyPinAsync(string code)
155122
{
156123
if (_accountId == null)
157124
{
158-
throw new BlinkClientException("Not authorized");
125+
throw new BlinkClientException("Account ID is required to verify pin");
126+
}
127+
if (_clientId == null)
128+
{
129+
throw new BlinkClientException("Client ID is required to verify pin");
159130
}
160131
string url = $"/api/v4/account/{_accountId}/client/{_clientId}/pin/verify";
161132
var body = new
@@ -297,7 +268,7 @@ public async Task DeleteVideoAsync(BlinkVideoInfo video)
297268

298269
private HttpClient GetHttpClient()
299270
{
300-
return _http ??= CreateHttpClient(_baseUrl);
271+
return _http ?? throw new BlinkClientException("Not authorized");
301272
}
302273
}
303274
}

Sources/Blink/IBlinkClient.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Blink.Models;
2-
using System.Collections.Generic;
2+
using Blink.Exceptions;
33
using System.Threading.Tasks;
4+
using System.Collections.Generic;
45

56
namespace Blink
67
{
@@ -17,12 +18,13 @@ public interface IBlinkClient
1718
int GeneralSleepTime { get; set; }
1819

1920
/// <summary>
20-
/// Authorizes the client asynchronously.
21+
/// Authorize with email and password provided in constructor.
2122
/// </summary>
22-
/// <param name="reauth">If set to <c>true</c>, forces reauthorization.
23-
/// If set to <c>false</c>, the client will use the existing authorization token if it is still valid.</param>
24-
/// <returns>A task that represents the asynchronous authorization operation. The task result contains the authorization data.</returns>
25-
Task<BlinkAuthorizationData> AuthorizeAsync(bool reauth = false);
23+
/// <returns><see cref="LoginResult"/> object with authorization data</returns>
24+
/// <exception cref="BlinkClientException">Thrown when email or password is not provided in constructor</exception>
25+
/// <exception cref="BlinkClientException">Thrown when authorization fails</exception>
26+
/// <exception cref="BlinkClientException">Thrown when no content is returned</exception>
27+
Task<LoginResult> AuthorizeAsync(string email, string password, bool reauth = true);
2628

2729
/// <summary>
2830
/// Deletes a specified video asynchronously.

Sources/Blink/Models/BlinkAuthorizationData.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

Sources/Blink/Models/LoginResult.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text.Json.Serialization;
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
23

34
namespace Blink.Models
45
{
@@ -42,5 +43,14 @@ public class LoginResult
4243
/// </summary>
4344
[JsonPropertyName("allow_pin_resend_seconds")]
4445
public int AllowPinResendSeconds { get; set; }
46+
47+
/// <summary>
48+
/// Convert to JSON string
49+
/// </summary>
50+
/// <returns>JSON string</returns>
51+
public string ToJson()
52+
{
53+
return JsonSerializer.Serialize(this);
54+
}
4555
}
4656
}

0 commit comments

Comments
 (0)