< Summary

Class:Microsoft.Azure.CognitiveServices.ContentModerator.TextModeration
Assembly:Microsoft.Azure.CognitiveServices.Vision.ContentModerator
File(s):C:\Git\azure-sdk-for-net\sdk\cognitiveservices\Vision.ContentModerator\src\Generated\TextModeration.cs
Covered lines:4
Uncovered lines:176
Coverable lines:180
Total lines:442
Line coverage:2.2% (4 of 180)
Covered branches:1
Total branches:98
Branch coverage:1% (1 of 98)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-80%50%
get_Client()-0%100%
ScreenTextWithHttpMessagesAsync()-0%0%
DetectLanguageWithHttpMessagesAsync()-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\cognitiveservices\Vision.ContentModerator\src\Generated\TextModeration.cs

#LineLine coverage
 1// <auto-generated>
 2// Code generated by Microsoft (R) AutoRest Code Generator.
 3// Changes may cause incorrect behavior and will be lost if the code is
 4// regenerated.
 5// </auto-generated>
 6
 7namespace Microsoft.Azure.CognitiveServices.ContentModerator
 8{
 9    using Microsoft.Rest;
 10    using Models;
 11    using Newtonsoft.Json;
 12    using System.Collections;
 13    using System.Collections.Generic;
 14    using System.IO;
 15    using System.Linq;
 16    using System.Net;
 17    using System.Net.Http;
 18    using System.Threading;
 19    using System.Threading.Tasks;
 20
 21    /// <summary>
 22    /// TextModeration operations.
 23    /// </summary>
 24    public partial class TextModeration : IServiceOperations<ContentModeratorClient>, ITextModeration
 25    {
 26        /// <summary>
 27        /// Initializes a new instance of the TextModeration class.
 28        /// </summary>
 29        /// <param name='client'>
 30        /// Reference to the service client.
 31        /// </param>
 32        /// <exception cref="System.ArgumentNullException">
 33        /// Thrown when a required parameter is null
 34        /// </exception>
 535        public TextModeration(ContentModeratorClient client)
 36        {
 537            if (client == null)
 38            {
 039                throw new System.ArgumentNullException("client");
 40            }
 541            Client = client;
 542        }
 43
 44        /// <summary>
 45        /// Gets a reference to the ContentModeratorClient
 46        /// </summary>
 047        public ContentModeratorClient Client { get; private set; }
 48
 49        /// <summary>
 50        /// Detect profanity and match against custom and shared blacklists
 51        /// </summary>
 52        /// <remarks>
 53        /// Detects profanity in more than 100 languages and match against custom and
 54        /// shared blacklists.
 55        /// </remarks>
 56        /// <param name='textContentType'>
 57        /// The content type. Possible values include: 'text/plain', 'text/html',
 58        /// 'text/xml', 'text/markdown'
 59        /// </param>
 60        /// <param name='textContent'>
 61        /// Content to screen.
 62        /// </param>
 63        /// <param name='language'>
 64        /// Language of the text.
 65        /// </param>
 66        /// <param name='autocorrect'>
 67        /// Autocorrect text.
 68        /// </param>
 69        /// <param name='pII'>
 70        /// Detect personal identifiable information.
 71        /// </param>
 72        /// <param name='listId'>
 73        /// The list Id.
 74        /// </param>
 75        /// <param name='classify'>
 76        /// Classify input.
 77        /// </param>
 78        /// <param name='customHeaders'>
 79        /// Headers that will be added to request.
 80        /// </param>
 81        /// <param name='cancellationToken'>
 82        /// The cancellation token.
 83        /// </param>
 84        /// <exception cref="APIErrorException">
 85        /// Thrown when the operation returned an invalid status code
 86        /// </exception>
 87        /// <exception cref="SerializationException">
 88        /// Thrown when unable to deserialize the response
 89        /// </exception>
 90        /// <exception cref="ValidationException">
 91        /// Thrown when a required parameter is null
 92        /// </exception>
 93        /// <exception cref="System.ArgumentNullException">
 94        /// Thrown when a required parameter is null
 95        /// </exception>
 96        /// <return>
 97        /// A response object containing the response body and response headers.
 98        /// </return>
 99        public async Task<HttpOperationResponse<Screen>> ScreenTextWithHttpMessagesAsync(string textContentType, Stream 
 100        {
 0101            if (Client.Endpoint == null)
 102            {
 0103                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint");
 104            }
 0105            if (textContentType == null)
 106            {
 0107                throw new ValidationException(ValidationRules.CannotBeNull, "textContentType");
 108            }
 0109            if (textContent == null)
 110            {
 0111                throw new ValidationException(ValidationRules.CannotBeNull, "textContent");
 112            }
 113            // Tracing
 0114            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 0115            string _invocationId = null;
 0116            if (_shouldTrace)
 117            {
 0118                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0119                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0120                tracingParameters.Add("language", language);
 0121                tracingParameters.Add("autocorrect", autocorrect);
 0122                tracingParameters.Add("pII", pII);
 0123                tracingParameters.Add("listId", listId);
 0124                tracingParameters.Add("classify", classify);
 0125                tracingParameters.Add("textContentType", textContentType);
 0126                tracingParameters.Add("textContent", textContent);
 0127                tracingParameters.Add("cancellationToken", cancellationToken);
 0128                ServiceClientTracing.Enter(_invocationId, this, "ScreenText", tracingParameters);
 129            }
 130            // Construct URL
 0131            var _baseUrl = Client.BaseUri;
 0132            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "contentmoderator/moderate/v1.0/ProcessText/Scre
 0133            _url = _url.Replace("{Endpoint}", Client.Endpoint);
 0134            List<string> _queryParameters = new List<string>();
 0135            if (language != null)
 136            {
 0137                _queryParameters.Add(string.Format("language={0}", System.Uri.EscapeDataString(language)));
 138            }
 0139            if (autocorrect != null)
 140            {
 0141                _queryParameters.Add(string.Format("autocorrect={0}", System.Uri.EscapeDataString(Rest.Serialization.Saf
 142            }
 0143            if (pII != null)
 144            {
 0145                _queryParameters.Add(string.Format("PII={0}", System.Uri.EscapeDataString(Rest.Serialization.SafeJsonCon
 146            }
 0147            if (listId != null)
 148            {
 0149                _queryParameters.Add(string.Format("listId={0}", System.Uri.EscapeDataString(listId)));
 150            }
 0151            if (classify != null)
 152            {
 0153                _queryParameters.Add(string.Format("classify={0}", System.Uri.EscapeDataString(Rest.Serialization.SafeJs
 154            }
 0155            if (_queryParameters.Count > 0)
 156            {
 0157                _url += "?" + string.Join("&", _queryParameters);
 158            }
 159            // Create HTTP transport objects
 0160            var _httpRequest = new HttpRequestMessage();
 0161            HttpResponseMessage _httpResponse = null;
 0162            _httpRequest.Method = new HttpMethod("POST");
 0163            _httpRequest.RequestUri = new System.Uri(_url);
 164            // Set Headers
 165
 166
 0167            if (customHeaders != null)
 168            {
 0169                foreach(var _header in customHeaders)
 170                {
 0171                    if (_httpRequest.Headers.Contains(_header.Key))
 172                    {
 0173                        _httpRequest.Headers.Remove(_header.Key);
 174                    }
 0175                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 176                }
 177            }
 178
 179            // Serialize Request
 0180            string _requestContent = null;
 0181            if(textContent == null)
 182            {
 0183              throw new System.ArgumentNullException("textContent");
 184            }
 0185            if (textContent != null && textContent != Stream.Null)
 186            {
 0187                _httpRequest.Content = new StreamContent(textContent);
 0188                _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse(textContent
 189            }
 190            // Set Credentials
 0191            if (Client.Credentials != null)
 192            {
 0193                cancellationToken.ThrowIfCancellationRequested();
 0194                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 195            }
 196            // Send Request
 0197            if (_shouldTrace)
 198            {
 0199                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 200            }
 0201            cancellationToken.ThrowIfCancellationRequested();
 0202            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 0203            if (_shouldTrace)
 204            {
 0205                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 206            }
 0207            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 0208            cancellationToken.ThrowIfCancellationRequested();
 0209            string _responseContent = null;
 0210            if ((int)_statusCode != 200)
 211            {
 0212                var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC
 213                try
 214                {
 0215                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 0216                    APIError _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte
 0217                    if (_errorBody != null)
 218                    {
 0219                        ex.Body = _errorBody;
 220                    }
 0221                }
 0222                catch (JsonException)
 223                {
 224                    // Ignore the exception
 0225                }
 0226                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 0227                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 0228                if (_shouldTrace)
 229                {
 0230                    ServiceClientTracing.Error(_invocationId, ex);
 231                }
 0232                _httpRequest.Dispose();
 0233                if (_httpResponse != null)
 234                {
 0235                    _httpResponse.Dispose();
 236                }
 0237                throw ex;
 238            }
 239            // Create Result
 0240            var _result = new HttpOperationResponse<Screen>();
 0241            _result.Request = _httpRequest;
 0242            _result.Response = _httpResponse;
 243            // Deserialize Response
 0244            if ((int)_statusCode == 200)
 245            {
 0246                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 247                try
 248                {
 0249                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<Screen>(_responseContent, Client
 0250                }
 0251                catch (JsonException ex)
 252                {
 0253                    _httpRequest.Dispose();
 0254                    if (_httpResponse != null)
 255                    {
 0256                        _httpResponse.Dispose();
 257                    }
 0258                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 259                }
 260            }
 0261            if (_shouldTrace)
 262            {
 0263                ServiceClientTracing.Exit(_invocationId, _result);
 264            }
 0265            return _result;
 0266        }
 267
 268        /// <summary>
 269        /// This operation will detect the language of given input content. Returns the
 270        /// &lt;a href="http://www-01.sil.org/iso639-3/codes.asp"&gt;ISO 639-3
 271        /// code&lt;/a&gt; for the predominant language comprising the submitted text.
 272        /// Over 110 languages supported.
 273        /// </summary>
 274        /// <param name='textContentType'>
 275        /// The content type. Possible values include: 'text/plain', 'text/html',
 276        /// 'text/xml', 'text/markdown'
 277        /// </param>
 278        /// <param name='textContent'>
 279        /// Content to screen.
 280        /// </param>
 281        /// <param name='customHeaders'>
 282        /// Headers that will be added to request.
 283        /// </param>
 284        /// <param name='cancellationToken'>
 285        /// The cancellation token.
 286        /// </param>
 287        /// <exception cref="APIErrorException">
 288        /// Thrown when the operation returned an invalid status code
 289        /// </exception>
 290        /// <exception cref="SerializationException">
 291        /// Thrown when unable to deserialize the response
 292        /// </exception>
 293        /// <exception cref="ValidationException">
 294        /// Thrown when a required parameter is null
 295        /// </exception>
 296        /// <exception cref="System.ArgumentNullException">
 297        /// Thrown when a required parameter is null
 298        /// </exception>
 299        /// <return>
 300        /// A response object containing the response body and response headers.
 301        /// </return>
 302        public async Task<HttpOperationResponse<DetectedLanguage>> DetectLanguageWithHttpMessagesAsync(string textConten
 303        {
 0304            if (Client.Endpoint == null)
 305            {
 0306                throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint");
 307            }
 0308            if (textContentType == null)
 309            {
 0310                throw new ValidationException(ValidationRules.CannotBeNull, "textContentType");
 311            }
 0312            if (textContent == null)
 313            {
 0314                throw new ValidationException(ValidationRules.CannotBeNull, "textContent");
 315            }
 316            // Tracing
 0317            bool _shouldTrace = ServiceClientTracing.IsEnabled;
 0318            string _invocationId = null;
 0319            if (_shouldTrace)
 320            {
 0321                _invocationId = ServiceClientTracing.NextInvocationId.ToString();
 0322                Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
 0323                tracingParameters.Add("textContentType", textContentType);
 0324                tracingParameters.Add("textContent", textContent);
 0325                tracingParameters.Add("cancellationToken", cancellationToken);
 0326                ServiceClientTracing.Enter(_invocationId, this, "DetectLanguage", tracingParameters);
 327            }
 328            // Construct URL
 0329            var _baseUrl = Client.BaseUri;
 0330            var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "contentmoderator/moderate/v1.0/ProcessText/Dete
 0331            _url = _url.Replace("{Endpoint}", Client.Endpoint);
 332            // Create HTTP transport objects
 0333            var _httpRequest = new HttpRequestMessage();
 0334            HttpResponseMessage _httpResponse = null;
 0335            _httpRequest.Method = new HttpMethod("POST");
 0336            _httpRequest.RequestUri = new System.Uri(_url);
 337            // Set Headers
 338
 339
 0340            if (customHeaders != null)
 341            {
 0342                foreach(var _header in customHeaders)
 343                {
 0344                    if (_httpRequest.Headers.Contains(_header.Key))
 345                    {
 0346                        _httpRequest.Headers.Remove(_header.Key);
 347                    }
 0348                    _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
 349                }
 350            }
 351
 352            // Serialize Request
 0353            string _requestContent = null;
 0354            if(textContent == null)
 355            {
 0356              throw new System.ArgumentNullException("textContent");
 357            }
 0358            if (textContent != null && textContent != Stream.Null)
 359            {
 0360                _httpRequest.Content = new StreamContent(textContent);
 0361                _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse(textContent
 362            }
 363            // Set Credentials
 0364            if (Client.Credentials != null)
 365            {
 0366                cancellationToken.ThrowIfCancellationRequested();
 0367                await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 368            }
 369            // Send Request
 0370            if (_shouldTrace)
 371            {
 0372                ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
 373            }
 0374            cancellationToken.ThrowIfCancellationRequested();
 0375            _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
 0376            if (_shouldTrace)
 377            {
 0378                ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
 379            }
 0380            HttpStatusCode _statusCode = _httpResponse.StatusCode;
 0381            cancellationToken.ThrowIfCancellationRequested();
 0382            string _responseContent = null;
 0383            if ((int)_statusCode != 200)
 384            {
 0385                var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC
 386                try
 387                {
 0388                    _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 0389                    APIError _errorBody =  Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte
 0390                    if (_errorBody != null)
 391                    {
 0392                        ex.Body = _errorBody;
 393                    }
 0394                }
 0395                catch (JsonException)
 396                {
 397                    // Ignore the exception
 0398                }
 0399                ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
 0400                ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
 0401                if (_shouldTrace)
 402                {
 0403                    ServiceClientTracing.Error(_invocationId, ex);
 404                }
 0405                _httpRequest.Dispose();
 0406                if (_httpResponse != null)
 407                {
 0408                    _httpResponse.Dispose();
 409                }
 0410                throw ex;
 411            }
 412            // Create Result
 0413            var _result = new HttpOperationResponse<DetectedLanguage>();
 0414            _result.Request = _httpRequest;
 0415            _result.Response = _httpResponse;
 416            // Deserialize Response
 0417            if ((int)_statusCode == 200)
 418            {
 0419                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
 420                try
 421                {
 0422                    _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<DetectedLanguage>(_responseConte
 0423                }
 0424                catch (JsonException ex)
 425                {
 0426                    _httpRequest.Dispose();
 0427                    if (_httpResponse != null)
 428                    {
 0429                        _httpResponse.Dispose();
 430                    }
 0431                    throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
 432                }
 433            }
 0434            if (_shouldTrace)
 435            {
 0436                ServiceClientTracing.Exit(_invocationId, _result);
 437            }
 0438            return _result;
 0439        }
 440
 441    }
 442}