Skip to content

Commit 265ab79

Browse files
committed
Add retry mechanism to GetVideoAsync in BlinkClient
- Added `System.Net.Mime` namespace to `BlinkClient.cs`. - Modified `GetVideoAsync` to include optional `tryCount` parameter. - Implemented retry logic in `GetVideoAsync`: - Attempts to retrieve video up to `tryCount` times. - Posts to URL and waits for `GeneralSleepTime` before retrying. - Returns video as byte array if content type is "video/mp4". - Throws `BlinkClientException` if content type is not "video/mp4" after retries.
1 parent 3484190 commit 265ab79

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

Sources/Blink/BlinkClient.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Net.Http.Json;
88
using System.Threading.Tasks;
99
using System.Collections.Generic;
10+
using System.Net.Mime;
1011

1112
namespace Blink
1213
{
@@ -224,9 +225,10 @@ public async Task<IEnumerable<BlinkVideoInfo>> GetVideosAsync()
224225
/// Get video from Blink camera.
225226
/// </summary>
226227
/// <param name="video"><see cref="BlinkVideoInfo"/> object with video data</param>
228+
/// <param name="tryCount">Number of tries to get video</param>
227229
/// <returns>Video as byte array</returns>
228230
/// <exception cref="BlinkClientException">Thrown when not authorized</exception>
229-
public async Task<byte[]> GetVideoAsync(BlinkVideoInfo video)
231+
public async Task<byte[]> GetVideoAsync(BlinkVideoInfo video, int tryCount = 3)
230232
{
231233
if (_accountId == null)
232234
{
@@ -239,16 +241,22 @@ public async Task<byte[]> GetVideoAsync(BlinkVideoInfo video)
239241
string url = $"/api/v1/accounts/{_accountId}/networks/{video.NetworkId}/" +
240242
$"sync_modules/{video.ModuleId}/local_storage/manifest/{video.ManifestId}/clip/request/{video.Id}";
241243

242-
await _http.PostAsync(url, null);
243-
await Task.Delay(GeneralSleepTime);
244-
245-
var response = await _http.GetAsync(url);
246-
string contentType = response.Content.Headers.ContentType?.MediaType ?? string.Empty;
247-
if (contentType != "video/mp4")
244+
int count = 0;
245+
string contentType = string.Empty;
246+
HttpResponseMessage? response = null;
247+
while (count++ < tryCount)
248248
{
249-
throw new BlinkClientException($"Failed to get video {video.Id}, contentType {contentType} - {response.ReasonPhrase}");
249+
await _http.PostAsync(url, null);
250+
await Task.Delay(GeneralSleepTime);
251+
252+
response = await _http.GetAsync(url);
253+
contentType = response.Content.Headers.ContentType?.MediaType ?? string.Empty;
254+
if (contentType == "video/mp4")
255+
{
256+
return await response.Content.ReadAsByteArrayAsync();
257+
}
250258
}
251-
return await response.Content.ReadAsByteArrayAsync();
259+
throw new BlinkClientException($"Failed to get video {video.Id}, contentType {contentType} - {response?.ReasonPhrase ?? "Unknown Error"}");
252260
}
253261

254262
/// <summary>

0 commit comments

Comments
 (0)