From a9d9326db8853b1860e267be4415e73ac4fc352e Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Tue, 24 Aug 2021 17:31:14 +0000 Subject: [PATCH] --- source/Plugins/AyaNova.Plugin.V8/util.cs | 77 +++++++++++++++++++++--- 1 file changed, 69 insertions(+), 8 deletions(-) diff --git a/source/Plugins/AyaNova.Plugin.V8/util.cs b/source/Plugins/AyaNova.Plugin.V8/util.cs index 0f4f06f..f1059bc 100644 --- a/source/Plugins/AyaNova.Plugin.V8/util.cs +++ b/source/Plugins/AyaNova.Plugin.V8/util.cs @@ -21,6 +21,7 @@ namespace AyaNova.PlugIn.V8 public static GZTW.AyaNova.BLL.LocalizedTextTable LocaleText = null; public const string TEST_ROUTE = "notify/hello"; public const string API_BASE_ROUTE = "api/v8/"; + private const int MAX_TRIES = 3;//max times to retry an api call before giving up public static HttpClient client = new HttpClient(); //url once known to be good internal static string ApiBaseUrl { get; set; } @@ -116,14 +117,34 @@ namespace AyaNova.PlugIn.V8 - public async static Task GetAsync(string route) + { + Exception FirstException = null; + + for (int x = 0; x < MAX_TRIES; x++) + { + try + { + return await TryGetAsync(route); + } + catch (Exception ex) + { + if (FirstException == null) + FirstException = ex; + } + } + + //no luck re-throw the exception + throw FirstException; + } + + private async static Task TryGetAsync(string route) { var requestMessage = new HttpRequestMessage(HttpMethod.Get, ApiBaseUrl + route); requestMessage.Headers.Add("X-AY-Import-Mode", "1"); if (!string.IsNullOrWhiteSpace(JWT)) requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JWT); - + HttpResponseMessage response = null; try { @@ -135,7 +156,7 @@ namespace AyaNova.PlugIn.V8 var InnerErr = ""; if (ex.InnerException != null) InnerErr = ex.InnerException.Message; - throw new Exception("GET error, route: " + route + "\nError:" + Err + "\nInner error:" + InnerErr + "\nStack:" + ex.StackTrace ); + throw new Exception("GET error, route: " + route + "\nError:" + Err + "\nInner error:" + InnerErr + "\nStack:" + ex.StackTrace); } @@ -148,13 +169,53 @@ namespace AyaNova.PlugIn.V8 return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) }; } - // + + + public async static Task PostAsync(string route, dynamic d) { - return await PostAsync(route, d.ToString(Newtonsoft.Json.Formatting.None)); + Exception FirstException = null; + + for (int x = 0; x < MAX_TRIES; x++) + { + try + { + return await TryPostAsync(route, d.ToString(Newtonsoft.Json.Formatting.None)); + } + catch (Exception ex) + { + if (FirstException == null) + FirstException = ex; + } + } + + //no luck re-throw the exception + throw FirstException; } - public async static Task PostAsync(string route, string postJson = null) + public async static Task PostAsync(string route, string s = null) + { + Exception FirstException = null; + + for (int x = 0; x < MAX_TRIES; x++) + { + try + { + return await TryPostAsync(route, s); + } + catch (Exception ex) + { + if (FirstException == null) + FirstException = ex; + } + } + + //no luck re-throw the exception + throw FirstException; + } + + + private async static Task TryPostAsync(string route, string postJson = null) { var requestMessage = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + route); @@ -237,7 +298,7 @@ namespace AyaNova.PlugIn.V8 requestMessage.Content = formContent; - + HttpResponseMessage response = null; try { @@ -249,7 +310,7 @@ namespace AyaNova.PlugIn.V8 var InnerErr = ""; if (ex.InnerException != null) InnerErr = ex.InnerException.Message; - throw new Exception("POST FORMDATA error, route: " + route + "\nError:" + Err + "\nInner error:" + InnerErr + "\nStack:" + ex.StackTrace ); + throw new Exception("POST FORMDATA error, route: " + route + "\nError:" + Err + "\nInner error:" + InnerErr + "\nStack:" + ex.StackTrace); }