@@ -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}
0 commit comments