This commit is contained in:
2021-08-24 18:53:04 +00:00
parent f89fada37b
commit 361d52098c
3 changed files with 42 additions and 29 deletions

View File

@@ -22,6 +22,7 @@ namespace AyaNova.PlugIn.V8
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
private const int API_RETRY_DELAY = 3000;//pause in ms before retrying api call
public static HttpClient client = new HttpClient();
//url once known to be good
internal static string ApiBaseUrl { get; set; }
@@ -72,7 +73,7 @@ namespace AyaNova.PlugIn.V8
{
while (ex.InnerException != null)
ex = ex.InnerException;
return "Failed exception: \n" + ex.Message;
return "Failed exception: \r\n" + ex.Message;
}
}
@@ -132,10 +133,11 @@ namespace AyaNova.PlugIn.V8
if (FirstException == null)
FirstException = ex;
}
await Task.Delay(API_RETRY_DELAY);
}
//no luck re-throw the exception
throw FirstException;
throw new Exception("API call failed after " + MAX_TRIES.ToString() + " attempts", FirstException);
}
private async static Task<ApiResponse> TryGetAsync(string route)
@@ -156,14 +158,14 @@ 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 + "\r\nError:" + Err + "\r\nInner error:" + InnerErr + "\r\nStack:" + ex.StackTrace);
}
var responseAsString = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw new Exception("GET error, route: " + route + "\n" + responseAsString + "\n" + response.ReasonPhrase);
throw new Exception("GET error, route: " + route + "\r\n" + responseAsString + "\r\n" + response.ReasonPhrase);
}
else
return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) };
@@ -187,10 +189,11 @@ namespace AyaNova.PlugIn.V8
if (FirstException == null)
FirstException = ex;
}
await Task.Delay(API_RETRY_DELAY);
}
//no luck re-throw the exception
throw FirstException;
throw new Exception("API call failed after " + MAX_TRIES.ToString() + " attempts", FirstException);
}
public async static Task<ApiResponse> PostAsync(string route, string s = null)
@@ -208,10 +211,11 @@ namespace AyaNova.PlugIn.V8
if (FirstException == null)
FirstException = ex;
}
await Task.Delay(API_RETRY_DELAY);
}
//no luck re-throw the exception
throw FirstException;
throw new Exception("API call failed after " + MAX_TRIES.ToString() + " attempts", FirstException);
}
@@ -236,14 +240,14 @@ namespace AyaNova.PlugIn.V8
var InnerErr = "";
if (ex.InnerException != null)
InnerErr = ex.InnerException.Message;
throw new Exception("POST error, route: " + route + "\nError:" + Err + "\nInner error:" + InnerErr + "\nStack:" + ex.StackTrace + "\nPOSTED OBJECT:\n" + postJson);
throw new Exception("POST error, route: " + route + "\r\nError:" + Err + "\r\nInner error:" + InnerErr + "\r\nStack:" + ex.StackTrace + "\r\nPOSTED OBJECT:\r\n" + postJson);
}
var responseAsString = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
if (string.IsNullOrWhiteSpace(postJson))
postJson = "n/a";
throw new Exception("POST error, route: " + route + "\n" + responseAsString + "\n" + response.ReasonPhrase + "\nPOSTED OBJECT:\n" + postJson);
throw new Exception("POST error, route: " + route + "\r\n" + responseAsString + "\r\n" + response.ReasonPhrase + "\r\nPOSTED OBJECT:\r\n" + postJson);
}
else
return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) };
@@ -273,10 +277,11 @@ namespace AyaNova.PlugIn.V8
if (FirstException == null)
FirstException = ex;
}
await Task.Delay(API_RETRY_DELAY);
}
//no luck re-throw the exception
throw FirstException;
throw new Exception("API call failed after " + MAX_TRIES.ToString() + " attempts", FirstException);
}
public async static Task<ApiResponse> PutAsync(string route)
@@ -294,10 +299,11 @@ namespace AyaNova.PlugIn.V8
if (FirstException == null)
FirstException = ex;
}
await Task.Delay(API_RETRY_DELAY);
}
//no luck re-throw the exception
throw FirstException;
throw new Exception("API call failed after " + MAX_TRIES.ToString() + " attempts", FirstException);
}
public async static Task<ApiResponse> TryPutAsync(string route, string putJson = null)
@@ -320,14 +326,14 @@ namespace AyaNova.PlugIn.V8
var InnerErr = "";
if (ex.InnerException != null)
InnerErr = ex.InnerException.Message;
throw new Exception("PUT error, route: " + route + "\nError:" + Err + "\nInner error:" + InnerErr + "\nStack:" + ex.StackTrace + "\nPOSTED OBJECT:\n" + putJson);
throw new Exception("PUT error, route: " + route + "\r\nError:" + Err + "\r\nInner error:" + InnerErr + "\r\nStack:" + ex.StackTrace + "\r\nPOSTED OBJECT:\r\n" + putJson);
}
var responseAsString = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
if (string.IsNullOrWhiteSpace(putJson))
putJson = "n/a";
throw new Exception("PUT error, route: " + route + "\n" + responseAsString + "\n" + response.ReasonPhrase + "\nPUT OBJECT:\n" + putJson);
throw new Exception("PUT error, route: " + route + "\r\n" + responseAsString + "\r\n" + response.ReasonPhrase + "\r\nPUT OBJECT:\r\n" + putJson);
}
else
return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) };
@@ -356,10 +362,11 @@ namespace AyaNova.PlugIn.V8
if (FirstException == null)
FirstException = ex;
}
await Task.Delay(API_RETRY_DELAY);
}
//no luck re-throw the exception
throw FirstException;
throw new Exception("API call failed after " + MAX_TRIES.ToString() + " attempts", FirstException);
}
@@ -384,14 +391,14 @@ 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 + "\r\nError:" + Err + "\r\nInner error:" + InnerErr + "\r\nStack:" + ex.StackTrace);
}
var responseAsString = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw new Exception("POST FORMDATA error, route: " + route + "\n" + responseAsString + "\n" + response.ReasonPhrase);
throw new Exception("POST FORMDATA error, route: " + route + "\r\n" + responseAsString + "\r\n" + response.ReasonPhrase);
}
else
return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) };