Ejemplo n.º 1
0
        protected override async Task <TranslateResult> Find(TranslateRequest translateRequest)
        {
            string uri = string.Format(
                Configuration.Url,
                _applicationConfiguration.ToLanguage.Extension,
                _applicationConfiguration.ToLanguage.Extension,
                HttpUtility.UrlEncode(translateRequest.CurrentText, Encoding.UTF8));

            IRestResponse response = await _restClient.Manipulate(client =>
            {
                client.BaseUrl  = uri.ToUri();
                client.Encoding = Encoding.UTF8;
            })
                                     .ExecuteGetTaskAsync(
                new RestRequest(Method.GET)
                .AddHeader(Headers.AcceptLanguage, AcceptLanguage)
                .AddHeader(Headers.AcceptEncoding, AcceptEncoding)
                .AddHeader(Headers.UserAgent, UserAgent)
                .AddHeader(Headers.Accept, Accept));

            var mean = new Maybe <string>();

            if (response.Ok())
            {
                mean = await MeanOrganizer.OrganizeMean(response.Content);
            }

            return(new TranslateResult(true, mean));
        }
        protected override async Task <TranslateResult> Find(TranslateRequest translateRequest)
        {
            string parameter = $"sl=auto&text={Uri.EscapeUriString(translateRequest.CurrentText)}&tl={_applicationConfiguration.ToLanguage.Extension}";

            IRestResponse response = await _restClient.Manipulate(client =>
            {
                client.BaseUrl     = Configuration.Url.ToUri();
                client.Encoding    = Encoding.UTF8;
                client.CachePolicy = new HttpRequestCachePolicy(HttpCacheAgeControl.MaxAge, TimeSpan.FromHours(1));
            }).ExecutePostTaskAsync(
                new RestRequest(Method.POST)
                .AddHeader(Headers.AcceptLanguage, AcceptLanguage)
                .AddHeader(Headers.AcceptEncoding, AcceptEncoding)
                .AddHeader(Headers.ContentType, ContentType)
                .AddHeader(Headers.UserAgent, UserAgent)
                .AddHeader(Headers.Accept, Accept)
                .AddParameter(ContentType, parameter, ParameterType.RequestBody));

            var mean = new Maybe <string>();

            if (response.Ok())
            {
                mean = await MeanOrganizer.OrganizeMean(response.Content);
            }

            return(new TranslateResult(true, mean));
        }
        protected override async Task <TranslateResult> Find(TranslateRequest translateRequest)
        {
            var uri = new Uri(string.Format(Configuration.Url,
                                            translateRequest.CurrentText,
                                            translateRequest.FromLanguageExtension + _applicationConfiguration.ToLanguage.Extension)
                              );

            IRestResponse response = await _restClient
                                     .Manipulate(client =>
            {
                client.BaseUrl     = uri;
                client.Encoding    = Encoding.UTF8;
                client.CachePolicy = new HttpRequestCachePolicy(HttpCacheAgeControl.MaxAge, TimeSpan.FromHours(1));
            }).ExecuteGetTaskAsync(
                new RestRequest(Method.GET)
                .AddHeader(Headers.UserAgent, UserAgent)
                .AddHeader(Headers.AcceptLanguage, AcceptLanguage));

            var mean = new Maybe <string>();

            if (response.Ok())
            {
                mean = await MeanOrganizer.OrganizeMean(response.Content, translateRequest.FromLanguageExtension);
            }

            return(new TranslateResult(true, mean));
        }
        protected override async Task <TranslateResult> Find(TranslateRequest translateRequest)
        {
            string uri = string.Format(Configuration.Url,
                                       HttpUtility.UrlEncode(translateRequest.CurrentText, Encoding.UTF8));

            IRestResponse response = await new RestClient(uri)
            {
                Encoding = Encoding.UTF8
            }
            .ExecuteGetTaskAsync(
                new RestRequest(Method.GET)
                .AddHeader("Accept-Language", "en-US,en;q=0.8,tr;q=0.6")
                .AddHeader("Accept-Encoding", "gzip, deflate, sdch")
                .AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36")
                .AddHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"));

            var mean = new Maybe <string>();

            if (response.Ok())
            {
                mean = await MeanOrganizer.OrganizeMean(response.Content, translateRequest.FromLanguageExtension);
            }

            return(new TranslateResult(true, mean));
        }
        protected override async Task <TranslateResult> Find(TranslateRequest translateRequest)
        {
            var requestObject = new
            {
                languageFrom = _applicationConfiguration.FromLanguage.Name.ToLower(),
                languageTo   = _applicationConfiguration.ToLanguage.Name.ToLower(),
                txtTrans     = translateRequest.CurrentText
            };

            IRestResponse response = await _restClient
                                     .Manipulate(client => client.BaseUrl = Configuration.Url.ToUri())
                                     .ExecutePostTaskAsync(
                new RestRequest(Method.POST)
                .AddHeader(ContentTypeName, ContentType)
                .AddParameter(ContentType, requestObject.ToJsonString(true), ParameterType.RequestBody)
                );

            var mean = new Maybe <string>();

            if (response.Ok())
            {
                mean = await MeanOrganizer.OrganizeMean(response.Content);
            }

            return(new TranslateResult(true, mean));
        }
        protected override async Task <TranslateResult> Find(TranslateRequest translateRequest)
        {
            var requestObject = new
            {
                dirCode       = $"{translateRequest.FromLanguageExtension}-{_applicationConfiguration.ToLanguage.Extension}",
                template      = Configuration.Template,
                text          = translateRequest.CurrentText,
                lang          = translateRequest.FromLanguageExtension,
                limit         = Configuration.Limit,
                useAutoDetect = true,
                key           = string.Empty,
                ts            = Configuration.Ts,
                tid           = string.Empty,
                IsMobile      = false
            };

            IRestResponse response = await _restClient
                                     .Manipulate(client => { client.BaseUrl = Configuration.Url.ToUri(); }).ExecutePostTaskAsync(new RestRequest(Method.POST)
                                                                                                                                 .AddHeader(ContentTypeName, ContentType)
                                                                                                                                 .AddParameter(ContentType, requestObject.ToJsonString(false), ParameterType.RequestBody));

            var mean = new Maybe <string>();

            if (response.Ok())
            {
                mean = await MeanOrganizer.OrganizeMean(response.Content);
            }

            return(new TranslateResult(true, mean));
        }
        public async void Finder_Should_Work()
        {
            TranslatorConfiguration.CanSupport().Returns(true);
            TranslatorConfiguration.IsActive().Returns(true);

            MeanOrganizer.OrganizeMean(Arg.Any <string>(), Arg.Any <string>()).Returns(Task.FromResult(new Maybe <string>("selam")));

            RestClient.ExecuteGetTaskAsync(Arg.Any <RestRequest>()).Returns(Task.FromResult <IRestResponse>(new RestResponse {
                StatusCode = HttpStatusCode.OK
            }));

            TurengMeanFinder sut = ResolveSut();

            var             translateRequest = new TranslateRequest("hi", "en");
            TranslateResult response         = await sut.FindMean(translateRequest);

            response.IsSuccess.ShouldBe(true);
            response.ResultMessage.ShouldBe(new Maybe <string>("selam"));
        }
        protected override async Task <TranslateResult> Find(TranslateRequest translateRequest)
        {
            Uri           address;
            IRestResponse response;

            if (Configuration.ShouldBeAnonymous)
            {
                address = new Uri(string.Format(Configuration.Url +
                                                new StringBuilder()
                                                .Append($"id={Configuration.SId}")
                                                .Append(Headers.Ampersand)
                                                .Append("srv=tr-text")
                                                .Append(Headers.Ampersand)
                                                .Append($"lang={translateRequest.FromLanguageExtension}-{_applicationConfiguration.ToLanguage.Extension}")
                                                .Append(Headers.Ampersand)
                                                .Append($"text={Uri.EscapeUriString(translateRequest.CurrentText)}")));

                response = await _restClient.Manipulate(client => { client.BaseUrl = address; })
                           .ExecutePostTaskAsync(new RestRequest(Method.POST)
                                                 .AddParameter(Headers.ContentTypeDefinition, $"text={translateRequest.CurrentText}"));
            }
            else
            {
                address = new Uri(string.Format(Configuration.Url +
                                                new StringBuilder()
                                                .Append($"key={Configuration.ApiKey}")
                                                .Append(Headers.Ampersand)
                                                .Append($"lang={translateRequest.FromLanguageExtension}-{_applicationConfiguration.ToLanguage.Extension}")
                                                .Append(Headers.Ampersand)
                                                .Append($"text={Uri.EscapeUriString(translateRequest.CurrentText)}")));

                response = await _restClient.Manipulate(client => { client.BaseUrl = address; }).ExecutePostTaskAsync(new RestRequest(Method.POST));
            }

            var mean = new Maybe <string>();

            if (response.Ok())
            {
                mean = await MeanOrganizer.OrganizeMean(response.Content);
            }

            return(new TranslateResult(true, mean));
        }
Ejemplo n.º 9
0
        protected FinderTestBase()
        {
            ApplicationConfiguration = Substitute.For <IApplicationConfiguration, ApplicationConfiguration>();
            ApplicationConfiguration.FromLanguage.Returns(new Language("English", "en"));
            ApplicationConfiguration.ToLanguage.Returns(new Language("Turkish", "tr"));
            Register(ApplicationConfiguration);

            RestClient = Substitute.For <IRestClient>();
            Register(RestClient);

            TranslatorConfiguration = Substitute.For <TConfig, TConfigImplementation>();
            TranslatorConfiguration.Url.Returns("http://www.dummycorrecturl.com/");
            Register(TranslatorConfiguration);

            MeanOrganizer = Substitute.For <IMeanOrganizer, TMeanOrganizer>();
            MeanOrganizer.TranslatorType.Returns(FindTranslatorType());
            MeanOrganizer.OrganizeMean(Arg.Any <string>()).Returns(Task.FromResult(new Maybe <string>("selam")));
            MeanOrganizer.OrganizeMean(Arg.Any <string>(), Arg.Any <string>()).Returns(Task.FromResult(new Maybe <string>("selam")));
            Register((TMeanOrganizer)MeanOrganizer, DependencyLifeStyle.Transient);

            Register <TSut>();
        }