Skip to content

Commit

Permalink
Merge in the DocumentTranslationService repo as a folder (#132)
Browse files Browse the repository at this point in the history
* 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
chriswendt1 authored Sep 15, 2024
1 parent ce31aaf commit 5e19850
Show file tree
Hide file tree
Showing 26 changed files with 2,981 additions and 5 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "DocumentTranslationService"]
path = DocumentTranslationService
url = https://github.com/MicrosoftTranslator/DocumentTranslationService
2 changes: 1 addition & 1 deletion DocumentTranslation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "images", "images", "{C0EB30
docs\images\translatorkey.png = docs\images\translatorkey.png
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DocumentTranslationService", "..\DocumentTranslationService\DocumentTranslationService.csproj", "{AABFD3BB-CC1E-4AA7-B792-72A1BC83A155}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DocumentTranslationService", "DocumentTranslationService\DocumentTranslationService.csproj", "{AABFD3BB-CC1E-4AA7-B792-72A1BC83A155}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
1 change: 0 additions & 1 deletion DocumentTranslationService
Submodule DocumentTranslationService deleted from f8b38a
125 changes: 125 additions & 0 deletions DocumentTranslationService/AppSettingsSetter.cs
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;
}
}
}
16 changes: 16 additions & 0 deletions DocumentTranslationService/CredentialsException.cs
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) { }
}
}
}

81 changes: 81 additions & 0 deletions DocumentTranslationService/DocTransAppSettings.cs
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; }
}
}
Loading

0 comments on commit 5e19850

Please sign in to comment.