public async Task <List <string?> > TranslateBatchAsync(List <string> list, string from, string to) { string authToken = await AzureAccessToken.GetAccessTokenAsync(AzureKey, Proxy); var body = new XElement("TranslateArrayRequest", new XElement("AppId"), new XElement("From", from), new XElement("Options", new XElement(Ns + "ContentType", "text/html") ), new XElement("Texts", list.Select(str => new XElement(ArrayNs + "string", str)) ), new XElement("To", to) ); using (var client = ExtendedHttpClient.GetClientWithProxy(Proxy)) using (var request = new HttpRequestMessage()) { request.Method = HttpMethod.Post; request.RequestUri = new Uri("https://api.microsofttranslator.com/v2/Http.svc/TranslateArray"); request.Content = new StringContent(body.ToString(), Encoding.UTF8, "text/xml"); request.Headers.Add("Authorization", authToken); var response = await client.SendAsync(request); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); XDocument doc = XDocument.Parse(responseBody); var result = doc.Descendants(Ns + "TranslateArrayResponse").Select(r => (string?)r.Element(Ns + "TranslatedText").Value).ToList(); return(result); } }
public static async Task <string> GetAccessTokenAsync(string subscriptionKey, string?proxy) { using (var client = ExtendedHttpClient.GetClientWithProxy(proxy)) using (var request = new HttpRequestMessage()) { request.Method = HttpMethod.Post; request.RequestUri = new Uri("https://api.cognitive.microsoft.com/sts/v1.0/issueToken"); request.Content = new StringContent(string.Empty); request.Headers.TryAddWithoutValidation("Ocp-Apim-Subscription-Key", subscriptionKey); var response = await client.SendAsync(request); response.EnsureSuccessStatusCode(); var token = await response.Content.ReadAsStringAsync(); return("Bearer " + token); } }
public async Task <List <string?>?> TranslateBatchAsync(List <string> list, string from, string to) { var azureKey = AzureKey(); if (string.IsNullOrEmpty(azureKey)) { return(null); } object[] body = list.Select(s => new { Text = s }).ToArray(); var text = JsonSerializer.Serialize(body); using (var client = ExtendedHttpClient.GetClientWithProxy(Proxy?.Invoke())) using (var request = new HttpRequestMessage()) { request.Method = HttpMethod.Post; request.RequestUri = new Uri($"https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from={from}&to={to}"); request.Content = new StringContent(text, Encoding.UTF8, "application/json"); request.Headers.Add("Ocp-Apim-Subscription-Key", azureKey); var region = Region?.Invoke(); if (region.HasText()) { request.Headers.Add("Ocp-Apim-Subscription-Region", region); } var response = await client.SendAsync(request); response.EnsureSuccessStatusCode(); var responseBody = await response.Content.ReadAsByteArrayAsync(); TranslationResult[] deserializedOutput = JsonSerializer.Deserialize <TranslationResult[]>(responseBody, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) !; return(deserializedOutput.Select(a => (string?)a.Translations.Single().Text).ToList()); } }