Exemple #1
0
        public void ToQueryString_WhenToIsInvalid_ExpectException(params string[] data)
        {
            var rp = new RequestParameter();

            rp.To = data;

            Assert.Throws <Exception>(() => rp.ToQueryString());
        }
Exemple #2
0
        public void ToQueryString_WhenToIsValid_ExpectCorrectQueryString(string expected, params string[] data)
        {
            var rp = new RequestParameter();

            rp.To = data;

            var qs = rp.ToQueryString();

            Assert.Equal(expected, qs);
        }
Exemple #3
0
        /// <summary>
        /// Translates the content.
        /// </summary>
        /// <param name="content">The content. (Max.: 25 items)</param>
        /// <param name="options">The options.</param>
        /// <returns>
        /// The translated content
        /// </returns>
        public async Task <IList <ResponseBody> > TranslateAsync(IEnumerable <RequestContent> content, RequestParameter options)
        {
            if (content.Count() > MaxNumberOfRequestContent)
            {
                throw new Exception($"Maximum amount of text to be translated have been reached (Max: {MaxNumberOfRequestContent})");
            }
            else if (string.Join(string.Empty, content).Length > MaxNumberOfCharacterPerRequest)
            {
                throw new Exception($"Maximum length of all the text to be translated is {MaxNumberOfCharacterPerRequest} characters.");
            }

            var qs = options.ToQueryString();

            using (var request = new HttpRequestMessage(HttpMethod.Post, $"{UriExtensionPath}?{qs}"))
            {
                var requestBody = JsonConvert.SerializeObject(content.ToList());

                // For the request, don't forget the request header stuff.
                request.Content = new StringContent(requestBody, Encoding.UTF8, Constants.RequestMediaType);
                // Todo We should be alternating between the subscription key. When we detect it's not working.
                request.Headers.Add(Constants.RequestHeaderSubscriptionKey, _cognitiveServiceConfig.SubscriptionKey);
                request.Headers.Add(Constants.RequestHeaderSubscriptionRegion, _cognitiveServiceConfig.SubscriptionRegion);

                using (var response = await _httpClient.SendAsync(request).ConfigureAwait(false))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        var responseBody = await response.Content.ReadAsStringAsync();

                        var result = JsonConvert.DeserializeObject <List <ResponseBody> >(responseBody);

                        return(result);
                    }
                    else
                    {
                        var message = $"Problem happened during translation. " +
                                      $"Status code: {response.StatusCode}, " +
                                      $"Reason: {response.ReasonPhrase}";

                        var exception = new TranslateException(message);
                        exception.ParseResponseError(response);
                        throw exception;
                    }
                }
            }
        }