@@ -28,6 +28,7 @@ public class BlinkClient
2828 private HttpClient ? _http ;
2929 private readonly string _email ;
3030 private readonly string _password ;
31+ private const string _baseUrl = "https://rest-prod.immedia-semi.com" ;
3132
3233 /// <summary>
3334 /// Create Blink client with email and password.
@@ -96,9 +97,8 @@ public async Task<BlinkAuthorizationData> AuthorizeAsync()
9697 email = _email ,
9798 password = _password
9899 } ;
99- string baseUrl = "https://rest-prod.immedia-semi.com" ;
100- _http = CreateHttpClient ( baseUrl , string . Empty ) ;
101- var response = await _http . PostAsJsonAsync ( "/api/v5/account/login" , body ) ;
100+ var httpClient = GetHttpClient ( ) ;
101+ var response = await httpClient . PostAsJsonAsync ( "/api/v5/account/login" , body ) ;
102102 if ( ! response . IsSuccessStatusCode )
103103 {
104104 throw new BlinkClientException ( "Failed to authorize - " + response . ReasonPhrase ) ;
@@ -107,7 +107,7 @@ public async Task<BlinkAuthorizationData> AuthorizeAsync()
107107 ?? throw new BlinkClientException ( "Failed to authorize - no content" ) ;
108108 if ( ! string . IsNullOrWhiteSpace ( loginResult . Account . Tier ) && ! string . IsNullOrWhiteSpace ( loginResult . Auth . Token ) )
109109 {
110- baseUrl = $ "https://rest-{ loginResult . Account . Tier } .immedia-semi.com";
110+ string baseUrl = $ "https://rest-{ loginResult . Account . Tier } .immedia-semi.com";
111111 _http = CreateHttpClient ( baseUrl , loginResult . Auth . Token ) ;
112112 }
113113 _accountId = loginResult . Account . AccountId ;
@@ -121,7 +121,7 @@ public async Task<BlinkAuthorizationData> AuthorizeAsync()
121121 } ;
122122 }
123123
124- private HttpClient CreateHttpClient ( string baseUrl , string token )
124+ private HttpClient CreateHttpClient ( string baseUrl , string token = "" )
125125 {
126126 if ( string . IsNullOrWhiteSpace ( baseUrl ) )
127127 {
@@ -161,7 +161,8 @@ public async Task VerifyPinAsync(string code)
161161 {
162162 pin = code
163163 } ;
164- var response = await _http . PostAsJsonAsync ( url , body ) ;
164+ var httpClient = GetHttpClient ( ) ;
165+ var response = await httpClient . PostAsJsonAsync ( url , body ) ;
165166 if ( ! response . IsSuccessStatusCode )
166167 {
167168 throw new BlinkClientException ( "Failed to verify pin - " + response . ReasonPhrase ) ;
@@ -181,7 +182,8 @@ public async Task<Dashboard> GetDashboardAsync()
181182 throw new BlinkClientException ( "Not authorized" ) ;
182183 }
183184 string url = $ "/api/v3/accounts/{ _accountId } /homescreen";
184- var response = await _http . GetAsync ( url ) ;
185+ var httpClient = GetHttpClient ( ) ;
186+ var response = await httpClient . GetAsync ( url ) ;
185187 if ( ! response . IsSuccessStatusCode )
186188 {
187189 throw new BlinkClientException ( "Failed to get dashboard - " + response . ReasonPhrase ) ;
@@ -208,7 +210,8 @@ public async Task<IEnumerable<BlinkVideoInfo>> GetVideosAsync()
208210 string url = $ "/api/v1/accounts/{ _accountId } /networks/{ module . NetworkId } /" +
209211 $ "sync_modules/{ module . Id } /local_storage/manifest/request";
210212 await Task . Delay ( GeneralSleepTime ) ; // I don't know why, but their server returns empty response without this delay
211- var result = await _http . PostAsync ( url , null ) ;
213+ var httpClient = GetHttpClient ( ) ;
214+ var result = await httpClient . PostAsync ( url , null ) ;
212215 if ( ! result . IsSuccessStatusCode )
213216 {
214217 throw new BlinkClientException ( "Failed to get videos - " + result . ReasonPhrase ) ;
@@ -217,7 +220,7 @@ public async Task<IEnumerable<BlinkVideoInfo>> GetVideosAsync()
217220 ?? throw new BlinkClientException ( "Failed to get videos - no content" ) ;
218221 url += $ "/{ manifestData . Id } ";
219222 await Task . Delay ( GeneralSleepTime ) ; // I don't know why, but their server returns empty response without this delay
220- var response = await _http . GetAsync ( url ) ;
223+ var response = await httpClient . GetAsync ( url ) ;
221224 var videoResponse = await response . Content . ReadFromJsonAsync < VideoResponse > ( )
222225 ?? throw new BlinkClientException ( "Failed to get videos - no content" ) ;
223226 foreach ( var video in videoResponse . Videos )
@@ -252,12 +255,13 @@ public async Task<byte[]> GetVideoAsync(BlinkVideoInfo video, int tryCount = 3)
252255 int count = 0 ;
253256 string contentType = string . Empty ;
254257 HttpResponseMessage ? response = null ;
258+ var httpClient = GetHttpClient ( ) ;
255259 while ( count ++ < tryCount )
256260 {
257- await _http . PostAsync ( url , null ) ;
261+ await httpClient . PostAsync ( url , null ) ;
258262 await Task . Delay ( GeneralSleepTime ) ;
259263
260- response = await _http . GetAsync ( url ) ;
264+ response = await httpClient . GetAsync ( url ) ;
261265 contentType = response . Content . Headers . ContentType ? . MediaType ?? string . Empty ;
262266 if ( contentType == "video/mp4" )
263267 {
@@ -285,8 +289,14 @@ public async Task DeleteVideoAsync(BlinkVideoInfo video)
285289 }
286290 string url = $ "/api/v1/accounts/{ _accountId } /networks/{ video . NetworkId } /" +
287291 $ "sync_modules/{ video . ModuleId } /local_storage/manifest/{ video . ManifestId } /clip/delete/{ video . Id } ";
288- var result = await _http . PostAsync ( url , null ) ;
292+ var httpClient = GetHttpClient ( ) ;
293+ var result = await httpClient . PostAsync ( url , null ) ;
289294 result . EnsureSuccessStatusCode ( ) ;
290295 }
296+
297+ private HttpClient GetHttpClient ( )
298+ {
299+ return _http ??= CreateHttpClient ( _baseUrl ) ;
300+ }
291301 }
292302}
0 commit comments