-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge in the DocumentTranslationService repo as a folder (#132)
* Copy the DocumentTranslationService module in Copy the DocumentTranslationService module that was its own repor, as actual subfolder into the DocumentTranslation repo. * Copy the files of DocumentTranslationService * Change DocumentTranslationService path to be a folder instead of a seaparate repo.
- Loading branch information
1 parent
ce31aaf
commit 5e19850
Showing
26 changed files
with
2,981 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +0,0 @@ | ||
[submodule "DocumentTranslationService"] | ||
path = DocumentTranslationService | ||
url = https://github.com/MicrosoftTranslator/DocumentTranslationService | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule DocumentTranslationService
deleted from
f8b38a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Holds the configuration information for the document translation CLI | ||
*/ | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Text.Json; | ||
|
||
namespace DocumentTranslationService.Core | ||
{ | ||
/// <summary> | ||
/// Manage the storage of the application settings | ||
/// </summary> | ||
public static class AppSettingsSetter | ||
{ | ||
public static event EventHandler SettingsReadComplete; | ||
const string AppName = "Document Translation"; | ||
const string AppSettingsFileName = "appsettings.json"; | ||
|
||
/// <summary> | ||
/// Create JSON string for app settings. | ||
/// </summary> | ||
/// <returns>JSON for appsettings</returns> | ||
public static string GetJson(DocTransAppSettings settings) | ||
{ | ||
return JsonSerializer.Serialize(settings, new JsonSerializerOptions { IncludeFields = true, WriteIndented = true }); | ||
} | ||
|
||
/// <summary> | ||
/// Reads settings from file and from Azure KeyVault, if file settings indicate a key vault | ||
/// </summary> | ||
/// <param name="filename">File name to read settings from</param> | ||
/// <returns>Task</returns> | ||
/// <exception cref="KeyVaultAccessException" /> | ||
public static DocTransAppSettings Read(string filename = null) | ||
{ | ||
string appsettingsJson; | ||
try | ||
{ | ||
if (string.IsNullOrEmpty(filename)) filename = GetSettingsFilename(); | ||
appsettingsJson = File.ReadAllText(filename); | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (ex is FileNotFoundException || ex is DirectoryNotFoundException) | ||
{ | ||
DocTransAppSettings settings = new() | ||
{ | ||
ConnectionStrings = new Connectionstrings(), | ||
AzureRegion = "global", | ||
TextTransEndpoint = "https://api.cognitive.microsofttranslator.com/", | ||
AzureResourceName = "https://*.cognitiveservices.azure.com/" | ||
}; | ||
settings.ConnectionStrings.StorageConnectionString = "DefaultEndpointsProtocol=https;AccountName=*"; | ||
return settings; | ||
} | ||
throw; | ||
} | ||
DocTransAppSettings result = JsonSerializer.Deserialize<DocTransAppSettings>(appsettingsJson, new JsonSerializerOptions { IncludeFields = true }); | ||
if (!string.IsNullOrEmpty(result.AzureKeyVaultName)) | ||
{ | ||
Debug.WriteLine($"Authentication: Using Azure Key Vault {result.AzureKeyVaultName} to read credentials."); | ||
} | ||
else | ||
{ | ||
Debug.WriteLine("Authentication: Using appsettings.json file to read credentials."); | ||
SettingsReadComplete?.Invoke(null, EventArgs.Empty); | ||
} | ||
if (string.IsNullOrEmpty(result.AzureRegion)) result.AzureRegion = "global"; | ||
return result; | ||
} | ||
|
||
public static void Write(string filename, DocTransAppSettings settings) | ||
{ | ||
if (string.IsNullOrEmpty(filename)) | ||
{ | ||
filename = GetSettingsFilename(); | ||
} | ||
try | ||
{ | ||
File.WriteAllText(filename, GetJson(settings)); | ||
} | ||
catch (IOException) | ||
{ | ||
Console.WriteLine("ERROR: Failed writing settings to " + filename); | ||
} | ||
return; | ||
} | ||
|
||
private static string GetSettingsFilename() | ||
{ | ||
string filename; | ||
if (OperatingSystem.IsWindows()) | ||
{ | ||
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + AppName); | ||
filename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + AppName + Path.DirectorySeparatorChar + AppSettingsFileName; | ||
} | ||
else | ||
{ | ||
filename = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + Path.DirectorySeparatorChar + AppName + "_" + AppSettingsFileName; | ||
} | ||
|
||
return filename; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Throws an exception to indicate the missing settings value; | ||
/// </summary> | ||
/// <param name="settings">The settings object to check on</param> | ||
/// <exception cref="ArgumentException"/> | ||
public static void CheckSettings(DocTransAppSettings settings, bool textOnly = false) | ||
{ | ||
if (string.IsNullOrEmpty(settings.SubscriptionKey)) throw new ArgumentException("SubscriptionKey"); | ||
if (string.IsNullOrEmpty(settings.AzureRegion)) throw new ArgumentException("AzureRegion"); | ||
if (!textOnly) | ||
{ | ||
if (string.IsNullOrEmpty(settings.ConnectionStrings.StorageConnectionString)) throw new ArgumentException("StorageConnectionString"); | ||
if (string.IsNullOrEmpty(settings.AzureResourceName)) throw new ArgumentException("AzureResourceName"); | ||
} | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
|
||
namespace DocumentTranslationService.Core | ||
{ | ||
public partial class DocumentTranslationService | ||
{ | ||
[Serializable] | ||
public class CredentialsException : Exception | ||
{ | ||
public CredentialsException() : base() { } | ||
public CredentialsException(string reason) : base(reason) { } | ||
public CredentialsException(string reason, Exception innerException) : base(reason, innerException) { } | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Holds the configuration information for the document translation settings | ||
*/ | ||
|
||
namespace DocumentTranslationService.Core | ||
{ | ||
public class DocTransAppSettings | ||
{ | ||
/// <summary> | ||
/// Azure Translator resource URI | ||
/// </summary> | ||
public string AzureResourceName { get; set; } | ||
/// <summary> | ||
/// Hold sthe connection strings. | ||
/// </summary> | ||
public Connectionstrings ConnectionStrings { get; set; } | ||
/// <summary> | ||
/// The resource key to use. | ||
/// </summary> | ||
public string SubscriptionKey { get; set; } | ||
/// <summary> | ||
/// Whether to show experimental languages | ||
/// </summary> | ||
public bool ShowExperimental { get; set; } | ||
/// <summary> | ||
/// The Custom Translator category ID to use. | ||
/// </summary> | ||
public string Category { get; set; } | ||
/// <summary> | ||
/// Hold the Azure region. Important only for text translation. This is the region ID, not the region friendly name. | ||
/// </summary> | ||
public string AzureRegion { get; set; } | ||
/// <summary> | ||
/// Holds the URI of the Azure key vault to use instead of local settings. | ||
/// If not null or empty, other secrets and region will be ignored. | ||
/// </summary> | ||
public string AzureKeyVaultName { get; set; } | ||
/// <summary> | ||
/// Holds the Text Translation Endpoint | ||
/// </summary> | ||
public string TextTransEndpoint { get; set; } | ||
/// <summary> | ||
/// Holds the string for experimental flights | ||
/// </summary> | ||
public string FlightString { get; set; } | ||
public bool UsingKeyVault | ||
{ | ||
get | ||
{ | ||
if (string.IsNullOrEmpty(AzureKeyVaultName?.Trim())) return false; | ||
else return true; | ||
} | ||
} | ||
public bool UsingProxy | ||
{ | ||
get | ||
{ | ||
if (string.IsNullOrEmpty(ProxyAddress?.Trim())) return false; | ||
else if (ProxyUseDefaultCredentials) return true; | ||
else return true; | ||
} | ||
} | ||
/// <summary> | ||
/// Whether to use user credentials when using a proxy | ||
/// </summary> | ||
public bool ProxyUseDefaultCredentials { get; set; } | ||
|
||
/// <summary> | ||
/// Proxy server address | ||
/// </summary> | ||
public string ProxyAddress { get; set; } | ||
} | ||
|
||
public class Connectionstrings | ||
{ | ||
/// <summary> | ||
/// Azure storage connection string, copied from the portal. | ||
/// </summary> | ||
public string StorageConnectionString { get; set; } | ||
} | ||
} |
Oops, something went wrong.