This commit is contained in:
2021-08-24 17:31:14 +00:00
parent 35f05848f8
commit a9d9326db8

View File

@@ -21,6 +21,7 @@ namespace AyaNova.PlugIn.V8
public static GZTW.AyaNova.BLL.LocalizedTextTable LocaleText = null; public static GZTW.AyaNova.BLL.LocalizedTextTable LocaleText = null;
public const string TEST_ROUTE = "notify/hello"; public const string TEST_ROUTE = "notify/hello";
public const string API_BASE_ROUTE = "api/v8/"; 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(); public static HttpClient client = new HttpClient();
//url once known to be good //url once known to be good
internal static string ApiBaseUrl { get; set; } internal static string ApiBaseUrl { get; set; }
@@ -116,14 +117,34 @@ namespace AyaNova.PlugIn.V8
public async static Task<ApiResponse> GetAsync(string route) public async static Task<ApiResponse> 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<ApiResponse> TryGetAsync(string route)
{ {
var requestMessage = new HttpRequestMessage(HttpMethod.Get, ApiBaseUrl + route); var requestMessage = new HttpRequestMessage(HttpMethod.Get, ApiBaseUrl + route);
requestMessage.Headers.Add("X-AY-Import-Mode", "1"); requestMessage.Headers.Add("X-AY-Import-Mode", "1");
if (!string.IsNullOrWhiteSpace(JWT)) if (!string.IsNullOrWhiteSpace(JWT))
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JWT); requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", JWT);
HttpResponseMessage response = null; HttpResponseMessage response = null;
try try
{ {
@@ -135,7 +156,7 @@ namespace AyaNova.PlugIn.V8
var InnerErr = ""; var InnerErr = "";
if (ex.InnerException != null) if (ex.InnerException != null)
InnerErr = ex.InnerException.Message; 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) }; return new ApiResponse() { HttpResponse = response, ObjectResponse = Parse(responseAsString) };
} }
//
public async static Task<ApiResponse> PostAsync(string route, dynamic d) public async static Task<ApiResponse> 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<ApiResponse> PostAsync(string route, string postJson = null) public async static Task<ApiResponse> 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<ApiResponse> TryPostAsync(string route, string postJson = null)
{ {
var requestMessage = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + route); var requestMessage = new HttpRequestMessage(HttpMethod.Post, ApiBaseUrl + route);
@@ -237,7 +298,7 @@ namespace AyaNova.PlugIn.V8
requestMessage.Content = formContent; requestMessage.Content = formContent;
HttpResponseMessage response = null; HttpResponseMessage response = null;
try try
{ {
@@ -249,7 +310,7 @@ namespace AyaNova.PlugIn.V8
var InnerErr = ""; var InnerErr = "";
if (ex.InnerException != null) if (ex.InnerException != null)
InnerErr = ex.InnerException.Message; 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);
} }