diff --git a/OF DL/Helpers/APIHelper.cs b/OF DL/Helpers/APIHelper.cs index 80c595c..a1e534f 100644 --- a/OF DL/Helpers/APIHelper.cs +++ b/OF DL/Helpers/APIHelper.cs @@ -31,6 +31,8 @@ public class APIHelper : IAPIHelper private readonly Auth auth; private static DateTime? cachedDynamicRulesExpiration; private static DynamicRules? cachedDynamicRules; + private const int MaxAttempts = 30; + private const int DelayBetweenAttempts = 3000; static APIHelper() { @@ -2674,13 +2676,10 @@ public class APIHelper : IAPIHelper return DateTime.Now; } - public async Task GetDecryptionKeyCDRMProject(Dictionary drmHeaders, string licenceURL, string pssh) { Log.Debug("Calling GetDecryptionKey"); - const int maxAttempts = 30; - const int delayBetweenAttempts = 3000; int attempt = 0; try @@ -2701,7 +2700,7 @@ public class APIHelper : IAPIHelper Log.Debug($"Posting to CDRM Project: {json}"); - while (attempt < maxAttempts) + while (attempt < MaxAttempts) { attempt++; @@ -2727,19 +2726,19 @@ public class APIHelper : IAPIHelper } else { - Log.Debug($"CDRM response status not successful. Retrying... Attempt {attempt} of {maxAttempts}"); - if (attempt < maxAttempts) + Log.Debug($"CDRM response status not successful. Retrying... Attempt {attempt} of {MaxAttempts}"); + if (attempt < MaxAttempts) { - await Task.Delay(delayBetweenAttempts); + await Task.Delay(DelayBetweenAttempts); } } } else { - Log.Debug($"Status not in CDRM response. Retrying... Attempt {attempt} of {maxAttempts}"); - if (attempt < maxAttempts) + Log.Debug($"Status not in CDRM response. Retrying... Attempt {attempt} of {MaxAttempts}"); + if (attempt < MaxAttempts) { - await Task.Delay(delayBetweenAttempts); + await Task.Delay(DelayBetweenAttempts); } } } @@ -2764,11 +2763,10 @@ public class APIHelper : IAPIHelper { Log.Debug("Calling GetDecryptionOFDL"); - try { - string dcValue = string.Empty; HttpClient client = new(); + int attempt = 0; OFDLRequest ofdlRequest = new OFDLRequest { @@ -2781,17 +2779,27 @@ public class APIHelper : IAPIHelper Log.Debug($"Posting to ofdl.tools: {json}"); - HttpRequestMessage request = new(HttpMethod.Post, "https://ofdl.tools/WV") + while (attempt < MaxAttempts) { - Content = new StringContent(json, Encoding.UTF8, "application/json") - }; + attempt++; - using var response = await client.SendAsync(request); + HttpRequestMessage request = new(HttpMethod.Post, "https://ofdl.tools/WV") + { + Content = new StringContent(json, Encoding.UTF8, "application/json") + }; + + using var response = await client.SendAsync(request); + + if (!response.IsSuccessStatusCode) + continue; - if (response.IsSuccessStatusCode) - { string body = await response.Content.ReadAsStringAsync(); - return body; + + if (!body.TrimStart().StartsWith('{')) + return body; + + Log.Debug($"Received JSON object instead of string. Retrying... Attempt {attempt} of {MaxAttempts}"); + await Task.Delay(DelayBetweenAttempts); } } catch (Exception ex) @@ -2805,6 +2813,7 @@ public class APIHelper : IAPIHelper Log.Error("Inner Exception: {0}\n\nStackTrace: {1}", ex.InnerException.Message, ex.InnerException.StackTrace); } } + return null; }