/// <summary>
        /// Call Evaluate Image, to determine whether the image violates any policy. This api returns a single
        /// score. 
        /// </summary>
        /// <param name="imageContent">Image Content</param>
        /// <returns>Evaluate result</returns>
        public async Task<EvaluateImageResult> EvaluateImageAsync(ImageModeratableContent imageContent)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(this.options.HostUrl);                
                string urlPath = string.Format("{0}/Image/EvaluateImage",this.options.ImageServicePath);
                HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, urlPath);

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

                EvaluateImageRequest request = new EvaluateImageRequest(imageContent);

                if (imageContent.BinaryContent == null)
                {
                    message.Content = new StringContent(
                        JsonConvert.SerializeObject(request),
                        Encoding.UTF8,
                        "application/json");
                }
                else
                {
                    message.Content = new StreamContent(imageContent.BinaryContent.Stream);
                    message.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(imageContent.BinaryContent.ContentType);
                }

                return await ServiceHelpers.SendRequest<EvaluateImageResult>(client, message);
            }
        }
        /// <summary>
        /// Call Evaluate Image, to determine whether the image violates any policy
        /// </summary>
        /// <param name="imageContent">Image Content</param>
        /// <returns>Evaluate result</returns>
        public async Task<DetectFaceResult> DetectFaceAsync(ImageModeratableContent imageContent, bool cacheContent = false)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(this.options.HostUrl);
                //string urlPath = $"{this.options.ImageServicePath}{string.Format("/Image/DetectFaces{0}", cacheContent ? "?cacheImage=true" : string.Empty)}";
                //string urlPath = $"{this.options.ImageServicePathV2}{string.Format("/Faces{0}", cacheContent ? "?cacheImage=true" : string.Empty)}";
                string urlPath = string.Format("{0}/Faces{1}", this.options.ImageServicePathV2,
                    cacheContent ? "?cacheImage=true" : string.Empty);
                HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, urlPath);

                ServiceHelpers.Addkey(message, this.options.ImageServiceKey);
                DetectFaceRequest request = new DetectFaceRequest(imageContent);

                if (imageContent.BinaryContent == null)
                {
                    message.Content = new StringContent(
                        JsonConvert.SerializeObject(request),
                        Encoding.UTF8,
                        "application/json");
                }
                else
                {
                    message.Content = new StreamContent(imageContent.BinaryContent.Stream);
                    message.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(imageContent.BinaryContent.ContentType);
                }

                return await ServiceHelpers.SendRequest<DetectFaceResult>(client, message);
            }
        }
        public void AddImageTest()
        {
            IModeratorService moderatorService = new ModeratorService(this.serviceOptions);

            // Add Image (with labels)
            // See label details in the response documentation: https://developer.microsoftmoderator.com/docs/services/54f7932727037412a0cda396/operations/54f793272703740c70627a24
            using (Stream stream = new FileStream(TestImageContent, FileMode.Open, FileAccess.Read))
            {
                ImageModeratableContent imageContent =
                    new ImageModeratableContent(new BinaryContent(stream, "image/jpeg"));
                var addResponse = moderatorService.ImageAddAsync(imageContent);
                var addResult = addResponse.Result;
                Assert.IsTrue(addResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(addResult));
                Assert.IsTrue(string.IsNullOrWhiteSpace(addResult.ImageId)
                    || string.Compare(addResult.Status.Description, "Error occurred while processing request :: Failure Adding a valid image  :: Image already exists") == 0,
                    "Image Id can be null only if the Image already exists, Response: {0}", JsonConvert.SerializeObject(addResult));

                // Refresh index
                var refreshResponse = moderatorService.RefreshImageIndexAsync();
                var refreshResult = refreshResponse.Result;
                Assert.IsTrue(refreshResult.IsUpdateSuccess, "Expected update Success on refresh, Response: {0}", JsonConvert.SerializeObject(refreshResult));
            }

            using (Stream stream = new FileStream(TestImageContent, FileMode.Open, FileAccess.Read))
            {
                ImageModeratableContent imageContent =
                    new ImageModeratableContent(new BinaryContent(stream, "image/jpeg"));

                // Match
                var matchResponse = moderatorService.MatchImageAsync(imageContent);
                var matchResult = matchResponse.Result;
                Assert.IsTrue(matchResult.IsMatch, "Expected match, Response: {0}", JsonConvert.SerializeObject(matchResult));
            }
        }
 public ImageAddRequest(ImageModeratableContent content)
 {
     this.Content = new Content()
     {
         Value = content.ContentAsString,
         DataRepresentation = content.DataRepresentation
     };
 }
        /// <summary>
        /// Create image request
        /// </summary>
        /// <param name="imageContent"></param>
        public BaseImageRequest(ImageModeratableContent imageContent)
        {
            if (imageContent == null)
            {
                throw new ArgumentNullException("imageContent");
            }

            this.DataRepresentation = imageContent.DataRepresentation;
            this.Value = imageContent.ContentAsString;
        }
        public void ValidateImageBinaryContentNoMatch()
        {
            using (Stream stream = new FileStream(noMatchImageContent, FileMode.Open, FileAccess.Read))
            {
                IPDNAService pdnaService = new PDNAService(this.pdnaServiceOptions);

                ImageModeratableContent imageContent = new ImageModeratableContent(new BinaryContent(stream, "image/jpeg"));
                var moderateResult = pdnaService.ValidateImageAsync(imageContent, false);
                var actualResult = moderateResult.Result;
                Assert.IsTrue(actualResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));
                Assert.IsTrue(actualResult.MatchDetails.MatchFlags.Count() == 0, "No Match was expected for this image, Response: {0}", JsonConvert.SerializeObject(actualResult));

            }
        }
        private static Service.Results.DetectFaceResult DetectFaceContent(IModeratorService moderatorService)
        {
            using (Stream stream = new FileStream(TestImageContent, FileMode.Open, FileAccess.Read))
            {
                ImageModeratableContent imageContent =
                    new ImageModeratableContent(new BinaryContent(stream, "image/jpeg"));
                var extractResponse = moderatorService.DetectFaceAsync(imageContent);
                return extractResponse.Result;

            }
        }
        public void ExtractTextTest()
        {
            IModeratorService moderatorService = new ModeratorService(this.serviceOptions);

            using (Stream stream = new FileStream(TestImageContent, FileMode.Open, FileAccess.Read))
            {
                ImageModeratableContent imageContent =
                    new ImageModeratableContent(new BinaryContent(stream, "image/jpeg"));

                // extract
                var extractResponse = moderatorService.ExtractTextAsync(imageContent, "eng");
                var extractResult = extractResponse.Result;

                Assert.IsTrue(extractResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(extractResult));
                Assert.IsTrue(extractResult.AdvancedInfo != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(extractResult));

                var text =
                    extractResult.AdvancedInfo.First(
                        x => string.Equals(x.Key, "Text", StringComparison.OrdinalIgnoreCase));

                Assert.AreEqual("Windows10 \r\nSatya Nadella \r\n", text.Value, "Text message was unexpected, Response: {0}", JsonConvert.SerializeObject(extractResult));
            }
        }
        public void EvaluateImageUrlTest()
        {
            IModeratorService moderatorService = new ModeratorService(this.serviceOptions);

            ImageModeratableContent imageContent = new ImageModeratableContent(TestImageUrl);
            var moderateResult = moderatorService.EvaluateImageAsync(imageContent);
            var actualResult = moderateResult.Result;
            Assert.IsTrue(actualResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.IsTrue(actualResult.AdvancedInfo != null, "AdvancedInfo is NULL, Response: {0}",  JsonConvert.SerializeObject(actualResult));

            var score = actualResult.AdvancedInfo.First(x => string.Equals(x.Key, "score", StringComparison.OrdinalIgnoreCase));
            Assert.AreNotEqual("0.000", score.Value, "score value, Response: {0}", JsonConvert.SerializeObject(actualResult));
        }
 public DetectFaceRequest(ImageModeratableContent content)
     : base(content)
 {
 }
 public ExtractTextRequest(ImageModeratableContent content)
     : base(content)
 {
 }
        public void V2EvaluateImageUrlV2Test()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);

            ImageModeratableContent imageContent = new ImageModeratableContent(TestImageUrl);
            var moderateResult = moderatorService.EvaluateImageAsync(imageContent);
            var actualResult = moderateResult.Result;
            Assert.IsTrue(actualResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.IsTrue(actualResult.AdvancedInfo != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));

            Assert.AreNotEqual(actualResult.AdultClassificationScore, "0.000", "Adult Score, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.AreNotEqual(actualResult.RacyClassificationScore, "0.000", "Racy Score, Response: {0}", JsonConvert.SerializeObject(actualResult));
        }
        private static Service.Results.ExtractTextResult ExtractTextContent(IModeratorServiceV2 moderatorService, bool cacheImage = false)
        {
            using (Stream stream = new FileStream(TestImageContent, FileMode.Open, FileAccess.Read))
            {
                ImageModeratableContent imageContent =
                    new ImageModeratableContent(new BinaryContent(stream, "image/jpeg"));
                var extractResponse = moderatorService.ExtractTextAsync(imageContent, "eng", true);
                return extractResponse.Result;

            }
        }
        public void EvaluateImageContentTest()
        {
            using (Stream stream = new FileStream(TestImageContent, FileMode.Open, FileAccess.Read))
            {
                IModeratorService moderatorService = new ModeratorService(this.serviceOptions);

                ImageModeratableContent imageContent = new ImageModeratableContent(new BinaryContent(stream, "image/jpeg"));
                var moderateResult = moderatorService.EvaluateImageAsync(imageContent);
                var actualResult = moderateResult.Result;
                Assert.IsTrue(actualResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));
                Assert.IsTrue(actualResult.AdvancedInfo != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));

                var score =
                    actualResult.AdvancedInfo.First(
                        x => string.Equals(x.Key, "score", StringComparison.OrdinalIgnoreCase));
                double scoreValue = double.Parse(score.Value);
                Assert.IsTrue(scoreValue > 0, "Expected higher than 0 score value for test image, Response: {0}", JsonConvert.SerializeObject(actualResult));
            }
        }
 public MatchImageRequest(ImageModeratableContent content)
     : base(content)
 {
 }
        public void V2ExtractTextTest()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);

            using (Stream stream = new FileStream(TestImageContent, FileMode.Open, FileAccess.Read))
            {
                ImageModeratableContent imageContent =
                    new ImageModeratableContent(new BinaryContent(stream, "image/jpeg"));

                // extract
                var extractResponse = moderatorService.ExtractTextAsync(imageContent, "eng");
                var extractResult = extractResponse.Result;

                Assert.IsTrue(extractResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(extractResult));
                Assert.IsTrue(extractResult.Text == "Windows10 \r\nSatya Nadella \r\n", "Extracted text is not as expected, Response: {0}", JsonConvert.SerializeObject(extractResult));

            }
        }
 public Task<ImageRemoveResult> ImageRemoveAsync(ImageModeratableContent imageContent)
 {
     throw new NotImplementedException();
 }
        private static Service.Results.EvaluateImageResult EvaluateImageContent(IModeratorService moderatorService)
        {
            using (Stream stream = new FileStream(TestImageContent, FileMode.Open, FileAccess.Read))
            {
                ImageModeratableContent imageContent =
                    new ImageModeratableContent(new BinaryContent(stream, "image/jpeg"));
                var moderateResult = moderatorService.EvaluateImageAsync(imageContent);
                return moderateResult.Result;

            }
        }
        public void V2EvaluateImageUrlTest()
        {
            IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);

            ImageModeratableContent imageContent = new ImageModeratableContent(TestImageUrl);
            var moderateResult = moderatorService.EvaluateImageAsync(imageContent);
            var actualResult = moderateResult.Result;
            Assert.IsTrue(actualResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.IsTrue(actualResult.AdultClassificationScore > 0, "Expected higher than 0 Adult Classification Score value for test image, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.IsTrue(actualResult.RacyClassificationScore > 0, "Expected higher than 0 Racy Classification Score value for test image, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.IsTrue(!actualResult.IsImageAdultClassified, "Image should not have been classified as Adult, Response: {0}", JsonConvert.SerializeObject(actualResult));
            Assert.IsTrue(!actualResult.IsImageRacyClassified, "Image should not have been classified as Racy, Response: {0}", JsonConvert.SerializeObject(actualResult));
        }
 public EvaluateImageRequest(ImageModeratableContent content)
     : base(content)
 {
     this.Metadata = new List<KeyValue>();
 }
 private static Service.Results.BaseModeratorResult CacheImageContent(IModeratorServiceV2 moderatorService)
 {
     using (Stream stream = new FileStream(TestImageContent, FileMode.Open, FileAccess.Read))
     {
         ImageModeratableContent imageContent =
             new ImageModeratableContent(TestImageUrl);//(TestCacheImage);
         var moderateResult = moderatorService.CacheImageContent(imageContent);
         return moderateResult.Result;
         //var actualResult = moderateResult.Result;
         //Assert.AreNotEqual(actualResult.CacheID, "");
     }
 }
 private static Service.Results.MatchImageResult ValidateImageContent(IPDNAService pdnaService, bool cacheImage = false)
 {
     ImageModeratableContent imageContent =
         new ImageModeratableContent("https://pdnasampleimages.blob.core.windows.net/matchedimages/img_130.jpg");
     var extractResponse = pdnaService.ValidateImageAsync(imageContent, cacheImage);
     return extractResponse.Result;
 }
        /// <summary>
        /// Call Evaluate Image, to determine whether the image violates any policy
        /// </summary>
        /// <param name="imageContent">Image Content</param>
        /// <returns>Evaluate result</returns>
        public async Task<ExtractTextResult> ExtractTextAsync(ImageModeratableContent imageContent, string language = "eng", bool cacheContent = false)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(this.options.HostUrl);
                //string urlPath = $"{this.options.ImageServicePathV2}{string.Format("/Image/DetectTextEnhanced{0}", cacheContent ? "?cacheImage=true" : string.Empty)}{"&language="}{language}";
                //string urlPath = $"{this.options.ImageServicePathV2}{string.Format("/OCR{0}", cacheContent ? "?cacheImage=true" : string.Empty)}{"&language="}{language}";
                //string urlPath = $"{this.options.ImageServicePathV2}{string.Format("/OCR?language={0}{1}", language, cacheContent ? "&cacheImage=true" : string.Empty)}";//{"&language="}{language}
                string urlPath = string.Format("{0}/OCR?language={1}{2}", this.options.ImageServicePathV2, language,
                    cacheContent ? "&cacheImage=true" : string.Empty);
                HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, urlPath);

                ServiceHelpers.Addkey(message, this.options.ImageServiceKey);
                message.Headers.Add("language", "eng");
                //message.Headers.Add("x-contentsources", "3061");
                ExtractTextRequest request = new ExtractTextRequest(imageContent);

                if (imageContent.BinaryContent == null)
                {
                    message.Content = new StringContent(
                        JsonConvert.SerializeObject(request),
                        Encoding.UTF8,
                        "application/json");
                }
                else
                {
                    message.Content = new StreamContent(imageContent.BinaryContent.Stream);
                    message.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(imageContent.BinaryContent.ContentType);
                }

                return await ServiceHelpers.SendRequest<ExtractTextResult>(client, message);
            }
        }
        /// <summary>
        /// Add an image into the Image list
        /// </summary>
        /// <param name="imageContent">Image Content</param>
        /// <param name="tag">Image policies</param>
        /// <param name="label">Image description</param>
        /// <returns>Immage add result</returns>
        public async Task<ImageAddResult> ImageAddAsync(ImageModeratableContent imageContent, string tag, string label)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(this.options.HostUrl);

                string queryParam = string.IsNullOrWhiteSpace(tag) ? string.Empty : "?tag=" + tag;
                if (string.IsNullOrWhiteSpace(queryParam))
                    queryParam = string.IsNullOrWhiteSpace(label) ? string.Empty : "?label=" + label;
                else
                    queryParam = queryParam + "&label=" + label;

                //string urlPath = $"{this.options.ImageServiceCustomListPathV2}{"/Add"}";
                //string urlPath = $"{this.options.ImageServiceCustomListPathV2}{string.Format("/Image/Add{0}", string.IsNullOrWhiteSpace(queryParam) ? string.Empty : queryParam)}";
                string urlPath = string.Format("{0}/Image/Add{1}", this.options.ImageServiceCustomListPathV2, string.IsNullOrWhiteSpace(queryParam) ? string.Empty : queryParam);
                HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, urlPath);

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

                ImageAddRequest request = new ImageAddRequest(imageContent);
                if (imageContent.BinaryContent == null)
                {
                    message.Content = new StringContent(
                        JsonConvert.SerializeObject(request),
                        Encoding.UTF8,
                        "application/json");
                }
                else
                {
                    message.Content = new StreamContent(imageContent.BinaryContent.Stream);
                    message.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(imageContent.BinaryContent.ContentType);
                }

                return await ServiceHelpers.SendRequest<ImageAddResult>(client, message);
            }
        }
        public void V2EvaluateImageContentTest()
        {
            using (Stream stream = new FileStream(TestImageContent, FileMode.Open, FileAccess.Read))
            {
                IModeratorServiceV2 moderatorService = new ModeratorServiceV2(this.serviceOptions);

                ImageModeratableContent imageContent = new ImageModeratableContent(new BinaryContent(stream, "image/jpeg"));
                var moderateResult = moderatorService.EvaluateImageAsync(imageContent);
                var actualResult = moderateResult.Result;
                Assert.IsTrue(actualResult != null, "Expected valid result, Response: {0}", JsonConvert.SerializeObject(actualResult));
                Assert.IsTrue(actualResult.AdultClassificationScore > 0, "Expected higher than 0 Adult Classification Score value for test image, Response: {0}", JsonConvert.SerializeObject(actualResult));
                Assert.IsTrue(actualResult.RacyClassificationScore > 0, "Expected higher than 0 Racy Classification Score value for test image, Response: {0}", JsonConvert.SerializeObject(actualResult));
                Assert.IsTrue(!actualResult.IsImageAdultClassified, "Image should not have been classified as Adult, Response: {0}", JsonConvert.SerializeObject(actualResult));
                Assert.IsTrue(!actualResult.IsImageRacyClassified, "Image should not have been classified as Racy, Response: {0}", JsonConvert.SerializeObject(actualResult));
            }
        }