-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGetCapabilities.cs
134 lines (122 loc) · 5.66 KB
/
GetCapabilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Azure.AI.Translation.Document;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.Json;
using System.Threading.Tasks;
namespace DocumentTranslationService.Core
{
public partial class DocumentTranslationService
{
/// <summary>
/// Holds the list of file formats after initial retrieval from Service
/// </summary>
public List<LocalFormats.LocalDocumentTranslationFileFormat> FileFormats { get; private set; } = new();
public HashSet<string> Extensions { get; private set; } = new();
public HashSet<string> GlossaryExtensions { get; private set; } = new();
public event EventHandler OnFileFormatsUpdate;
public IReadOnlyList<DocumentTranslationFileFormat> GlossaryFormats { get; private set; }
public event EventHandler OnGlossaryFormatsUpdate;
public async Task<IReadOnlyList<LocalFormats.LocalDocumentTranslationFileFormat>> GetDocumentFormatsAsync()
{
if (FileFormats?.Count > 0) return FileFormats;
else return await GetFormatsInternal();
}
private async Task<IReadOnlyList<LocalFormats.LocalDocumentTranslationFileFormat>> GetFormatsInternal()
{
if (String.IsNullOrEmpty(AzureResourceName)) throw new CredentialsException("name");
for (int i = 0; i < 3; i++)
{
Azure.Response<IReadOnlyList<DocumentTranslationFileFormat>> result = null;
try
{
result = await documentTranslationClient.GetSupportedDocumentFormatsAsync();
}
catch (Azure.RequestFailedException ex)
{
if (ex.Status == 401 || ex.Status == 403) throw new CredentialsException(ex.Message, ex);
}
catch (System.AggregateException ex)
{
throw new Exception("Unknown host: " + ex.Message, ex);
}
if (result?.Value.Count > 0)
{
Debug.WriteLine($"GetFormats: Response: {JsonSerializer.Serialize(result, new JsonSerializerOptions() { IncludeFields = true })}");
foreach (var item in result.Value)
{
//Add the file formats and extensions from the service
FileFormats.Add(new LocalFormats.LocalDocumentTranslationFileFormat(item.Format, new List<string>((List<string>)item.FileExtensions)));
foreach (string ext in item.FileExtensions)
{
Extensions.Add(ext.ToLowerInvariant());
}
}
//Add the formats and extensions for the locally provided formats
foreach (var localFormat in LocalFormats.LocalFormats.Formats)
{
LocalFormats.LocalDocumentTranslationFileFormat localDocumentTranslationFileFormat = new(
localFormat.Format,
localFormat.FileExtensions,
localFormat.ConvertToMarkdown,
localFormat.ConvertFromMarkdown
);
FileFormats.Add(localDocumentTranslationFileFormat);
foreach (string ext in localFormat.FileExtensions)
{
Extensions.Add(ext.ToLowerInvariant());
}
}
OnFileFormatsUpdate?.Invoke(this, EventArgs.Empty);
return FileFormats;
}
else
{
Debug.WriteLine("GetFormatsInternal: Get file formats failed.");
await Task.Delay(1000);
}
}
return null;
}
public async Task<IReadOnlyList<DocumentTranslationFileFormat>> GetGlossaryFormatsAsync()
{
if (GlossaryFormats?.Count > 0) return GlossaryFormats;
else return await GetGlossaryFormatsInternal();
}
private async Task<IReadOnlyList<DocumentTranslationFileFormat>> GetGlossaryFormatsInternal()
{
for (int i = 0; i < 3; i++)
{
Azure.Response<IReadOnlyList<DocumentTranslationFileFormat>> result = null;
try
{
result = await documentTranslationClient.GetSupportedGlossaryFormatsAsync();
}
catch (Azure.RequestFailedException ex)
{
if (ex.Status == 401 || ex.Status == 403) throw new CredentialsException(ex.Message, ex);
}
if (result?.Value.Count > 0)
{
Debug.WriteLine($"GetGlossaryFormats: Response: {JsonSerializer.Serialize(result, new JsonSerializerOptions() { IncludeFields = true })}");
GlossaryFormats = result.Value;
foreach (var item in result.Value)
{
foreach (string ext in item.FileExtensions)
{
GlossaryExtensions.Add(ext.ToLowerInvariant());
}
}
OnGlossaryFormatsUpdate?.Invoke(this, EventArgs.Empty);
return GlossaryFormats;
}
else
{
Debug.WriteLine("GetGlossaryFormatsInternal: Get glossary formats failed.");
await Task.Delay(1000);
}
}
return null;
}
}
}