Skip to content

Commit

Permalink
Catch errors, initialize variables correctly for the command line ver…
Browse files Browse the repository at this point in the history
…sion.
  • Loading branch information
chriswendt1 committed Aug 9, 2020
1 parent 199de14 commit ad9d4a5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public SetCredentials(ConsoleLogger Logger)
this.AzureKey = new Argument(
"APIkey",
false,
"API key to use for the calls to the Translator service.");
"API key to use for the calls to the Translator service.") ;

this.categoryID = new Argument(
"categoryID",
Expand Down Expand Up @@ -99,13 +99,8 @@ public SetCredentials(ConsoleLogger Logger)
false,
"Value of 'true' resets the credentials to their default values.");

this.Print = new Argument(
"Print",
false,
"Value of 'true' prints the currently saved credentials");

this.Arguments = new ArgumentList(
new[] { this.AzureKey, this.categoryID, this.Cloud, this.Region, this.Reset, this.Print},
new[] { this.AzureKey, this.categoryID, this.Cloud, this.Region, this.Reset},
Logger);
}

Expand Down Expand Up @@ -150,7 +145,6 @@ public override bool Execute()
try
{
if (!string.IsNullOrEmpty(this.Reset.ValueString)) TranslationServiceFacade.ResetCredentials();
if (!string.IsNullOrEmpty(this.Print.ValueString)) PrintCredentials();
if (!string.IsNullOrEmpty(this.AzureKey.ValueString)) TranslationServiceFacade.AzureKey = this.AzureKey.ValueString;
if (!string.IsNullOrEmpty(this.categoryID.ValueString)) TranslationServiceFacade.CategoryID = this.categoryID.ValueString;
if (!string.IsNullOrEmpty(this.Cloud.ValueString)) TranslationServiceFacade.AzureCloud = this.Cloud.ValueString;
Expand All @@ -165,6 +159,7 @@ public override bool Execute()
}

this.Logger.WriteLine(LogLevel.Msg, string.Format("Credentials saved."));

if (TranslationServiceFacade.IsTranslationServiceReady())
{
this.Logger.WriteLine(LogLevel.Msg, string.Format("Translator service is ready to use."));
Expand All @@ -176,15 +171,6 @@ public override bool Execute()
return true;
}

private void PrintCredentials()
{
TranslationServiceFacade.LoadCredentials();
Logger.WriteLine(LogLevel.Msg, "Cloud:\t{1}", TranslationServiceFacade.AzureCloud);
Logger.WriteLine(LogLevel.Msg, "Key:\t{1}", TranslationServiceFacade.AzureKey);
Logger.WriteLine(LogLevel.Msg, "Region:\t{1}", TranslationServiceFacade.AzureRegion);
Logger.WriteLine(LogLevel.Msg, "CategoryID:\t{1}", TranslationServiceFacade.CategoryID);
}

#endregion
}
}
26 changes: 22 additions & 4 deletions TranslationServices.Core/TranslationServiceFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static partial class TranslationServiceFacade
public static Dictionary<string, string> AvailableLanguages { get; } = new Dictionary<string, string>();
public static int Maxrequestsize { get => maxrequestsize; }
public static int Maxelements { get => maxelements; }
public static string AzureRegion { get; set; } = "Global";
public static string AzureRegion { get; set; } = null;
public static string AzureCloud { get; set; } = null;

public enum ContentType { plain, HTML };
Expand Down Expand Up @@ -229,8 +229,15 @@ public static async Task<bool> IsTranslationServiceReadyAsync()
/// <returns>Whether the translation servoce is ready to translate</returns>
public static bool IsTranslationServiceReady()
{
Task<bool> task = Task.Run(async () => await IsTranslationServiceReadyAsync().ConfigureAwait(false));
return task.Result;
try
{
Task<bool> task = Task.Run(async () => await IsTranslationServiceReadyAsync().ConfigureAwait(false));
return task.Result;
}
catch (TaskCanceledException)
{
return false;
}
}

/// <summary>
Expand Down Expand Up @@ -273,7 +280,18 @@ private static async Task<bool> IsCategoryValidV3Async(string category)
return returnvalue;
}



/// <summary>
/// Initializes the class.
/// </summary>
static TranslationServiceFacade()
{
Initialize(true);
return;
}



/// <summary>
/// Call once to initialize the static variables
/// </summary>
Expand Down

0 comments on commit ad9d4a5

Please sign in to comment.