public string Translate(string text, string from, string to)
{
using (var client = new HttpClient())
{
var url = string.Format(
_translatorApiUrlFormat,
HttpUtility.UrlEncode(text),
HttpUtility.UrlEncode(from),
HttpUtility.UrlEncode(to));
using (var message = new HttpRequestMessage(HttpMethod.Get, url))
{
message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", GetAccessToken());
using (var result = client.SendAsync(message).Result)
{
if (!result.IsSuccessStatusCode)
{
throw new TrosiTranslationException(result.Content.ReadAsStringAsync().Result);
}
using (var responseStream = result.Content.ReadAsStreamAsync().Result)
{
var serializer = new DataContractSerializer(typeof(string));
return serializer.ReadObject(responseStream) as string;
}
}
}
}
}