| | 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 | |
|
| | 7 | | namespace 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> |
| 5 | 35 | | public TextModeration(ContentModeratorClient client) |
| | 36 | | { |
| 5 | 37 | | if (client == null) |
| | 38 | | { |
| 0 | 39 | | throw new System.ArgumentNullException("client"); |
| | 40 | | } |
| 5 | 41 | | Client = client; |
| 5 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Gets a reference to the ContentModeratorClient |
| | 46 | | /// </summary> |
| 0 | 47 | | 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 | | { |
| 0 | 101 | | if (Client.Endpoint == null) |
| | 102 | | { |
| 0 | 103 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 104 | | } |
| 0 | 105 | | if (textContentType == null) |
| | 106 | | { |
| 0 | 107 | | throw new ValidationException(ValidationRules.CannotBeNull, "textContentType"); |
| | 108 | | } |
| 0 | 109 | | if (textContent == null) |
| | 110 | | { |
| 0 | 111 | | throw new ValidationException(ValidationRules.CannotBeNull, "textContent"); |
| | 112 | | } |
| | 113 | | // Tracing |
| 0 | 114 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 115 | | string _invocationId = null; |
| 0 | 116 | | if (_shouldTrace) |
| | 117 | | { |
| 0 | 118 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 119 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 120 | | tracingParameters.Add("language", language); |
| 0 | 121 | | tracingParameters.Add("autocorrect", autocorrect); |
| 0 | 122 | | tracingParameters.Add("pII", pII); |
| 0 | 123 | | tracingParameters.Add("listId", listId); |
| 0 | 124 | | tracingParameters.Add("classify", classify); |
| 0 | 125 | | tracingParameters.Add("textContentType", textContentType); |
| 0 | 126 | | tracingParameters.Add("textContent", textContent); |
| 0 | 127 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 128 | | ServiceClientTracing.Enter(_invocationId, this, "ScreenText", tracingParameters); |
| | 129 | | } |
| | 130 | | // Construct URL |
| 0 | 131 | | var _baseUrl = Client.BaseUri; |
| 0 | 132 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "contentmoderator/moderate/v1.0/ProcessText/Scre |
| 0 | 133 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| 0 | 134 | | List<string> _queryParameters = new List<string>(); |
| 0 | 135 | | if (language != null) |
| | 136 | | { |
| 0 | 137 | | _queryParameters.Add(string.Format("language={0}", System.Uri.EscapeDataString(language))); |
| | 138 | | } |
| 0 | 139 | | if (autocorrect != null) |
| | 140 | | { |
| 0 | 141 | | _queryParameters.Add(string.Format("autocorrect={0}", System.Uri.EscapeDataString(Rest.Serialization.Saf |
| | 142 | | } |
| 0 | 143 | | if (pII != null) |
| | 144 | | { |
| 0 | 145 | | _queryParameters.Add(string.Format("PII={0}", System.Uri.EscapeDataString(Rest.Serialization.SafeJsonCon |
| | 146 | | } |
| 0 | 147 | | if (listId != null) |
| | 148 | | { |
| 0 | 149 | | _queryParameters.Add(string.Format("listId={0}", System.Uri.EscapeDataString(listId))); |
| | 150 | | } |
| 0 | 151 | | if (classify != null) |
| | 152 | | { |
| 0 | 153 | | _queryParameters.Add(string.Format("classify={0}", System.Uri.EscapeDataString(Rest.Serialization.SafeJs |
| | 154 | | } |
| 0 | 155 | | if (_queryParameters.Count > 0) |
| | 156 | | { |
| 0 | 157 | | _url += "?" + string.Join("&", _queryParameters); |
| | 158 | | } |
| | 159 | | // Create HTTP transport objects |
| 0 | 160 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 161 | | HttpResponseMessage _httpResponse = null; |
| 0 | 162 | | _httpRequest.Method = new HttpMethod("POST"); |
| 0 | 163 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 164 | | // Set Headers |
| | 165 | |
|
| | 166 | |
|
| 0 | 167 | | if (customHeaders != null) |
| | 168 | | { |
| 0 | 169 | | foreach(var _header in customHeaders) |
| | 170 | | { |
| 0 | 171 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 172 | | { |
| 0 | 173 | | _httpRequest.Headers.Remove(_header.Key); |
| | 174 | | } |
| 0 | 175 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 176 | | } |
| | 177 | | } |
| | 178 | |
|
| | 179 | | // Serialize Request |
| 0 | 180 | | string _requestContent = null; |
| 0 | 181 | | if(textContent == null) |
| | 182 | | { |
| 0 | 183 | | throw new System.ArgumentNullException("textContent"); |
| | 184 | | } |
| 0 | 185 | | if (textContent != null && textContent != Stream.Null) |
| | 186 | | { |
| 0 | 187 | | _httpRequest.Content = new StreamContent(textContent); |
| 0 | 188 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse(textContent |
| | 189 | | } |
| | 190 | | // Set Credentials |
| 0 | 191 | | if (Client.Credentials != null) |
| | 192 | | { |
| 0 | 193 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 194 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 195 | | } |
| | 196 | | // Send Request |
| 0 | 197 | | if (_shouldTrace) |
| | 198 | | { |
| 0 | 199 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 200 | | } |
| 0 | 201 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 202 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 203 | | if (_shouldTrace) |
| | 204 | | { |
| 0 | 205 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 206 | | } |
| 0 | 207 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 208 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 209 | | string _responseContent = null; |
| 0 | 210 | | if ((int)_statusCode != 200) |
| | 211 | | { |
| 0 | 212 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | 213 | | try |
| | 214 | | { |
| 0 | 215 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 216 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| 0 | 217 | | if (_errorBody != null) |
| | 218 | | { |
| 0 | 219 | | ex.Body = _errorBody; |
| | 220 | | } |
| 0 | 221 | | } |
| 0 | 222 | | catch (JsonException) |
| | 223 | | { |
| | 224 | | // Ignore the exception |
| 0 | 225 | | } |
| 0 | 226 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 227 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 228 | | if (_shouldTrace) |
| | 229 | | { |
| 0 | 230 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 231 | | } |
| 0 | 232 | | _httpRequest.Dispose(); |
| 0 | 233 | | if (_httpResponse != null) |
| | 234 | | { |
| 0 | 235 | | _httpResponse.Dispose(); |
| | 236 | | } |
| 0 | 237 | | throw ex; |
| | 238 | | } |
| | 239 | | // Create Result |
| 0 | 240 | | var _result = new HttpOperationResponse<Screen>(); |
| 0 | 241 | | _result.Request = _httpRequest; |
| 0 | 242 | | _result.Response = _httpResponse; |
| | 243 | | // Deserialize Response |
| 0 | 244 | | if ((int)_statusCode == 200) |
| | 245 | | { |
| 0 | 246 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 247 | | try |
| | 248 | | { |
| 0 | 249 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<Screen>(_responseContent, Client |
| 0 | 250 | | } |
| 0 | 251 | | catch (JsonException ex) |
| | 252 | | { |
| 0 | 253 | | _httpRequest.Dispose(); |
| 0 | 254 | | if (_httpResponse != null) |
| | 255 | | { |
| 0 | 256 | | _httpResponse.Dispose(); |
| | 257 | | } |
| 0 | 258 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 259 | | } |
| | 260 | | } |
| 0 | 261 | | if (_shouldTrace) |
| | 262 | | { |
| 0 | 263 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 264 | | } |
| 0 | 265 | | return _result; |
| 0 | 266 | | } |
| | 267 | |
|
| | 268 | | /// <summary> |
| | 269 | | /// This operation will detect the language of given input content. Returns the |
| | 270 | | /// <a href="http://www-01.sil.org/iso639-3/codes.asp">ISO 639-3 |
| | 271 | | /// code</a> 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 | | { |
| 0 | 304 | | if (Client.Endpoint == null) |
| | 305 | | { |
| 0 | 306 | | throw new ValidationException(ValidationRules.CannotBeNull, "this.Client.Endpoint"); |
| | 307 | | } |
| 0 | 308 | | if (textContentType == null) |
| | 309 | | { |
| 0 | 310 | | throw new ValidationException(ValidationRules.CannotBeNull, "textContentType"); |
| | 311 | | } |
| 0 | 312 | | if (textContent == null) |
| | 313 | | { |
| 0 | 314 | | throw new ValidationException(ValidationRules.CannotBeNull, "textContent"); |
| | 315 | | } |
| | 316 | | // Tracing |
| 0 | 317 | | bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 0 | 318 | | string _invocationId = null; |
| 0 | 319 | | if (_shouldTrace) |
| | 320 | | { |
| 0 | 321 | | _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 0 | 322 | | Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 0 | 323 | | tracingParameters.Add("textContentType", textContentType); |
| 0 | 324 | | tracingParameters.Add("textContent", textContent); |
| 0 | 325 | | tracingParameters.Add("cancellationToken", cancellationToken); |
| 0 | 326 | | ServiceClientTracing.Enter(_invocationId, this, "DetectLanguage", tracingParameters); |
| | 327 | | } |
| | 328 | | // Construct URL |
| 0 | 329 | | var _baseUrl = Client.BaseUri; |
| 0 | 330 | | var _url = _baseUrl + (_baseUrl.EndsWith("/") ? "" : "/") + "contentmoderator/moderate/v1.0/ProcessText/Dete |
| 0 | 331 | | _url = _url.Replace("{Endpoint}", Client.Endpoint); |
| | 332 | | // Create HTTP transport objects |
| 0 | 333 | | var _httpRequest = new HttpRequestMessage(); |
| 0 | 334 | | HttpResponseMessage _httpResponse = null; |
| 0 | 335 | | _httpRequest.Method = new HttpMethod("POST"); |
| 0 | 336 | | _httpRequest.RequestUri = new System.Uri(_url); |
| | 337 | | // Set Headers |
| | 338 | |
|
| | 339 | |
|
| 0 | 340 | | if (customHeaders != null) |
| | 341 | | { |
| 0 | 342 | | foreach(var _header in customHeaders) |
| | 343 | | { |
| 0 | 344 | | if (_httpRequest.Headers.Contains(_header.Key)) |
| | 345 | | { |
| 0 | 346 | | _httpRequest.Headers.Remove(_header.Key); |
| | 347 | | } |
| 0 | 348 | | _httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value); |
| | 349 | | } |
| | 350 | | } |
| | 351 | |
|
| | 352 | | // Serialize Request |
| 0 | 353 | | string _requestContent = null; |
| 0 | 354 | | if(textContent == null) |
| | 355 | | { |
| 0 | 356 | | throw new System.ArgumentNullException("textContent"); |
| | 357 | | } |
| 0 | 358 | | if (textContent != null && textContent != Stream.Null) |
| | 359 | | { |
| 0 | 360 | | _httpRequest.Content = new StreamContent(textContent); |
| 0 | 361 | | _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse(textContent |
| | 362 | | } |
| | 363 | | // Set Credentials |
| 0 | 364 | | if (Client.Credentials != null) |
| | 365 | | { |
| 0 | 366 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 367 | | await Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| | 368 | | } |
| | 369 | | // Send Request |
| 0 | 370 | | if (_shouldTrace) |
| | 371 | | { |
| 0 | 372 | | ServiceClientTracing.SendRequest(_invocationId, _httpRequest); |
| | 373 | | } |
| 0 | 374 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 375 | | _httpResponse = await Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false); |
| 0 | 376 | | if (_shouldTrace) |
| | 377 | | { |
| 0 | 378 | | ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse); |
| | 379 | | } |
| 0 | 380 | | HttpStatusCode _statusCode = _httpResponse.StatusCode; |
| 0 | 381 | | cancellationToken.ThrowIfCancellationRequested(); |
| 0 | 382 | | string _responseContent = null; |
| 0 | 383 | | if ((int)_statusCode != 200) |
| | 384 | | { |
| 0 | 385 | | var ex = new APIErrorException(string.Format("Operation returned an invalid status code '{0}'", _statusC |
| | 386 | | try |
| | 387 | | { |
| 0 | 388 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 0 | 389 | | APIError _errorBody = Rest.Serialization.SafeJsonConvert.DeserializeObject<APIError>(_responseConte |
| 0 | 390 | | if (_errorBody != null) |
| | 391 | | { |
| 0 | 392 | | ex.Body = _errorBody; |
| | 393 | | } |
| 0 | 394 | | } |
| 0 | 395 | | catch (JsonException) |
| | 396 | | { |
| | 397 | | // Ignore the exception |
| 0 | 398 | | } |
| 0 | 399 | | ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent); |
| 0 | 400 | | ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent); |
| 0 | 401 | | if (_shouldTrace) |
| | 402 | | { |
| 0 | 403 | | ServiceClientTracing.Error(_invocationId, ex); |
| | 404 | | } |
| 0 | 405 | | _httpRequest.Dispose(); |
| 0 | 406 | | if (_httpResponse != null) |
| | 407 | | { |
| 0 | 408 | | _httpResponse.Dispose(); |
| | 409 | | } |
| 0 | 410 | | throw ex; |
| | 411 | | } |
| | 412 | | // Create Result |
| 0 | 413 | | var _result = new HttpOperationResponse<DetectedLanguage>(); |
| 0 | 414 | | _result.Request = _httpRequest; |
| 0 | 415 | | _result.Response = _httpResponse; |
| | 416 | | // Deserialize Response |
| 0 | 417 | | if ((int)_statusCode == 200) |
| | 418 | | { |
| 0 | 419 | | _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | 420 | | try |
| | 421 | | { |
| 0 | 422 | | _result.Body = Rest.Serialization.SafeJsonConvert.DeserializeObject<DetectedLanguage>(_responseConte |
| 0 | 423 | | } |
| 0 | 424 | | catch (JsonException ex) |
| | 425 | | { |
| 0 | 426 | | _httpRequest.Dispose(); |
| 0 | 427 | | if (_httpResponse != null) |
| | 428 | | { |
| 0 | 429 | | _httpResponse.Dispose(); |
| | 430 | | } |
| 0 | 431 | | throw new SerializationException("Unable to deserialize the response.", _responseContent, ex); |
| | 432 | | } |
| | 433 | | } |
| 0 | 434 | | if (_shouldTrace) |
| | 435 | | { |
| 0 | 436 | | ServiceClientTracing.Exit(_invocationId, _result); |
| | 437 | | } |
| 0 | 438 | | return _result; |
| 0 | 439 | | } |
| | 440 | |
|
| | 441 | | } |
| | 442 | | } |