public AddTermRequest(TextModeratableContent content)
 {
     this.Content = new Content()
     {
         DataRepresentation = content.DataRepresentation,
         Value = content.ContentAsString,
         EnglishTranslation = content.EnglishTranslation,
     };
 }
        public void IdentifyLanguageTest()
        {
            IModeratorService moderatorService = new ModeratorService(this.serviceOptions);

            TextModeratableContent textContent = new TextModeratableContent("Hola este es un texto en otro idioma");
            var identifyLanguageResponse = moderatorService.IdentifyLanguageAsync(textContent);
            var actualResult = identifyLanguageResponse.Result;
            Assert.IsTrue(actualResult != null, "Expected valid result");
            Assert.AreEqual("spa", actualResult.DetectedLanguage, "Expected valid result");
        }
        public BaseTextRequest(TextModeratableContent textContent)
        {
            if (textContent == null)
            {
                throw new ArgumentNullException("textContent");
            }

            this.DataRepresentation = textContent.DataRepresentation;
            this.Value = textContent.ContentAsString;
        }
        public void V2AddTermTest()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);

            // We are creating a term "FakeProfanity" in english (thus provide tha same english translation), then matching against it.
            TextModeratableContent textContent = new TextModeratableContent(text: "ertuythfg", englishTranslation: "FakeProfanity");
            var taskResult = moderatorService.AddTermAsync(textContent, "eng");

            var actualResult = taskResult.Result;
            Assert.IsTrue((actualResult.StatusCode != System.Net.HttpStatusCode.Created) || (actualResult.StatusCode != System.Net.HttpStatusCode.MultipleChoices), "Expected valid result for AddTerm");

            var refreshTask = moderatorService.RefreshTextIndexAsync("eng");
            var refreshResult = refreshTask.Result;
            Assert.IsTrue(refreshResult != null, "Expected valid result for RefreshIndex");

            var screenResponse = moderatorService.ScreenTextAsync(new TextModeratableContent("This is a ertuythfg!"), "eng");
            var screenResult = screenResponse.Result;

            var deleteTask = moderatorService.RemoveTermAsync(textContent, "eng");
            var deleteResult = deleteTask.Result;
            Assert.IsTrue(deleteResult.IsSuccessStatusCode, "Expected valid result for DeleteTerm");
        }
 /// <summary>
 /// Request to screen text, determining if the content violates any policy (adult content, etc...)
 /// </summary>
 /// <param name="content"></param>
 public ScreenTextRequest(TextModeratableContent content)
     : base(content)
 {
     this.Metadata = new string[] { };
 }
 /// <summary>
 /// Request to screen text, determining if the content violates any policy (adult content, etc...)
 /// </summary>
 /// <param name="content"></param>
 public IdentifyLanguageRequest(TextModeratableContent content)
 {
     this.Content = content.ContentAsString;
 }
        public void ScreenTextTest()
        {
            IModeratorService moderatorService = new ModeratorService(this.serviceOptions);

            // Import the term list. This needs to only be done once before screen
            moderatorService.ImportTermListAsync("eng").Wait();

            moderatorService.RefreshTextIndexAsync("eng").Wait();

            // Run screen to match, validating match result
            string text = "My evil freaking text!";
            TextModeratableContent textContent = new TextModeratableContent(text);

            var screenResponse = moderatorService.ScreenTextAsync(textContent, "eng");
            var screenResult = screenResponse.Result;
            Assert.IsTrue(screenResult != null, "Expected valid result");
            Assert.IsTrue(screenResult.MatchDetails != null, "Expected valid Match Details");
            Assert.IsTrue(screenResult.MatchDetails.MatchFlags != null, "Expected valid Match Flags");

            var matchFlag = screenResult.MatchDetails.MatchFlags.FirstOrDefault();
            Assert.IsTrue(matchFlag != null, "Expected to see a match flag!");
            Assert.AreEqual("freaking", matchFlag.Source, "Expected term to match");
        }
        /// <summary>
        /// Remove a term from the term list
        /// </summary>
        /// <param name="textContent">Text content</param>
        /// <param name="language">Language of term to remove</param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> RemoveTermAsync(TextModeratableContent textContent, string language)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(this.options.HostUrl);

                //string urlPath =
                //    $"{this.options.TextServiceCustomListPath}{$"/Text/{textContent.ContentAsString}?language={language}&subscription-key={this.options.TextServiceCustomListKey}"}";
                string urlPath = string.Format("{0}/{1}?language={2}&subscription-key={3}",
                    this.options.TextServiceCustomListPath, textContent.ContentAsString, language,
                    this.options.TextServiceCustomListKey);
                HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Delete, urlPath);

                // ServiceHelpers.Addkey(message, this.options.TextServiceCustomListKey);
                //ServiceHelpers.AddTextContentSourceKey(message, this.options.TextContentSourceId);

                return await ServiceHelpers.SendRequest(client, message);
            }
        }
        /// <summary>
        /// Adds a term to the term list
        /// </summary>
        /// <param name="textContent">Term text</param>
        /// <param name="language">Term language</param>
        /// <returns>Add Term Response</returns>
        public async Task<HttpResponseMessage> AddTermAsync(TextModeratableContent textContent, string language)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(this.options.HostUrl);

                string urlPath = string.Format("{0}/{1}?language={2}&subscription-key={3}",
                    this.options.TextServiceCustomListPath, textContent.ContentAsString, language,
                    this.options.TextServiceCustomListKey);
                HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, urlPath);

                
                HttpResponseMessage response = await ServiceHelpers.SendRequest(client, message);

                return response;

            }
        }
        public async Task<IdentifyLanguageResult> IdentifyLanguageAsync(TextModeratableContent textContent)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(this.options.HostUrl);

                
                string urlPath = string.Format("{0}/language", this.options.TextServicePathV2);
                HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, urlPath);
                ServiceHelpers.Addkey(message, this.options.TextServiceKey);               

                message.Content = new StringContent(
                    textContent.ContentAsString,
                    Encoding.Default,
                    "text/plain");
                message.Content.Headers.ContentType.MediaType = "text/plain";
                message.Content.Headers.ContentType.CharSet = null;

                return await ServiceHelpers.SendRequest<IdentifyLanguageResult>(client, message);
            }
        }
        /// <summary>
        /// Screen Text against the term list. Note that Import Term List needs to be run
        /// against the appropriate language before calling this method, for all the terms
        /// of that language to have been imported.
        /// </summary>
        /// <param name="textContent">Text to screen</param>
        /// <param name="language">Language in ISO-639-3 format</param>
        /// <returns></returns>
        public async Task<ScreenTextResult> ScreenTextAsync(TextModeratableContent textContent, string language = "eng")
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(this.options.HostUrl);

                //string urlPath = $"{this.options.TextServicePathV2}{$"/Screen?language={language}"}";
                string urlPath = string.Format("{0}/Screen?language={1}", this.options.TextServicePathV2, language);
                HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, urlPath);

                ServiceHelpers.Addkey(message, this.options.TextServiceKey);

               // message.Headers.Add("x-contentsources", this.options.TextContentSourceId);

                message.Content = new StringContent(
                    textContent.ContentAsString,
                    Encoding.Default,
                    "text/plain");
                message.Content.Headers.ContentType.MediaType = "text/plain";
                message.Content.Headers.ContentType.CharSet = null;

                return await ServiceHelpers.SendRequest<ScreenTextResult>(client, message);
            }
        }
        public async Task<IdentifyLanguageResult> IdentifyLanguageAsync(TextModeratableContent textContent)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(this.options.HostUrl);

                //string urlPath = $"{this.options.TextServicePath}{$"/Text/IdentifyLanguage?subscription-key={this.options.TextServiceCustomListKey}"}";
            string urlPath = string.Format("{0}/Language/Identify?subscription-key={1}", this.options.TextServicePath,this.options.TextServiceCustomListKey);
                HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, urlPath);

              //  ServiceHelpers.Addkey(message, this.options.TextServiceKey);
                message.Headers.Add("x-contentsources", this.options.TextContentSourceId);
                IdentifyLanguageRequest request = new IdentifyLanguageRequest(textContent);
                message.Content = new StringContent(
                    JsonConvert.SerializeObject(request),
                    Encoding.UTF8,
                    "application/json");
                return await ServiceHelpers.SendRequest<IdentifyLanguageResult>(client, message);
            }
        }
        /// <summary>
        /// Screen Text against the term list. Note that Import Term List needs to be run
        /// against the appropriate language before calling this method, for all the terms
        /// of that language to have been imported.
        /// </summary>
        /// <param name="textContent">Text to screen</param>
        /// <param name="language">Language in ISO-639-3 format</param>
        /// <returns></returns>
        public async Task<ScreenTextResult> ScreenTextAsync(TextModeratableContent textContent, string language = "eng")
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(this.options.HostUrl);

                
                string urlPath = string.Format("{0}/Text/Screen?language={1}&subscription-key={2}",
                    this.options.TextServicePath, language, this.options.TextServiceKey);
            HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, urlPath);

                ServiceHelpers.Addkey(message, this.options.TextServiceKey);
                message.Headers.Add("x-contentsources", this.options.TextContentSourceId);
                ScreenTextRequest request = new ScreenTextRequest(textContent);
                message.Content = new StringContent(
                    JsonConvert.SerializeObject(request),
                    Encoding.UTF8,
                    "application/json");
                return await ServiceHelpers.SendRequest<ScreenTextResult>(client, message);
            }
        }
        public void V2ScreenTextTest()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);

            // Import the term list. This needs to only be done once before screen
            moderatorService.ImportTermListAsync("eng").Wait();

            moderatorService.RefreshTextIndexAsync("eng").Wait();

            // Run screen to match, validating match result
            string text = "The <a href=\"www.bunnies.com\">qu!ck</a> brown  <a href=\"b.suspiciousdomain.com\">f0x</a> jumps over the lzay dog www.benign.net. freaking awesome.";
            TextModeratableContent textContent = new TextModeratableContent(text);
            var screenResponse = moderatorService.ScreenTextAsync(textContent, "eng");
            var screenResult = screenResponse.Result;

            var expectedScreenTextResult = JsonConvert.DeserializeObject<ScreenTextResult>(ResultJsonForComparison.TextV2_ScreenTextTest);

            Assert.IsTrue(screenResult != null, "Expected valid result");
            Assert.AreEqual(expectedScreenTextResult.NormalizedText, screenResult.NormalizedText);
            Assert.IsTrue(expectedScreenTextResult.Terms.SequenceEqual(screenResult.Terms, new TermMatchComparer()));
            Assert.IsTrue(expectedScreenTextResult.Terms.SequenceEqual(screenResult.Terms, new TermMatchComparer()));
            Assert.IsTrue(expectedScreenTextResult.Urls.SequenceEqual(screenResult.Urls, new URLMatchComparer()));
        }