...

Source file src/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.0/contentmoderator/textmoderation.go

Documentation: github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.0/contentmoderator

     1  package contentmoderator
     2  
     3  // Copyright (c) Microsoft Corporation. All rights reserved.
     4  // Licensed under the MIT License. See License.txt in the project root for license information.
     5  //
     6  // Code generated by Microsoft (R) AutoRest Code Generator.
     7  // Changes may cause incorrect behavior and will be lost if the code is regenerated.
     8  
     9  import (
    10  	"context"
    11  	"github.com/Azure/go-autorest/autorest"
    12  	"github.com/Azure/go-autorest/autorest/azure"
    13  	"github.com/Azure/go-autorest/tracing"
    14  	"io"
    15  	"net/http"
    16  )
    17  
    18  // TextModerationClient is the you use the API to scan your content as it is generated. Content Moderator then
    19  // processes your content and sends the results along with relevant information either back to your systems or to the
    20  // built-in review tool. You can use this information to take decisions e.g. take it down, send to human judge, etc.
    21  //
    22  // When using the API, images need to have a minimum of 128 pixels and a maximum file size of 4MB.
    23  // Text can be at most 1024 characters long.
    24  // If the content passed to the text API or the image API exceeds the size limits, the API will return an error code
    25  // that informs about the issue.
    26  type TextModerationClient struct {
    27  	BaseClient
    28  }
    29  
    30  // NewTextModerationClient creates an instance of the TextModerationClient client.
    31  func NewTextModerationClient(endpoint string) TextModerationClient {
    32  	return TextModerationClient{New(endpoint)}
    33  }
    34  
    35  // DetectLanguage this operation will detect the language of given input content. Returns the <a
    36  // href="http://www-01.sil.org/iso639-3/codes.asp">ISO 639-3 code</a> for the predominant language comprising the
    37  // submitted text. Over 110 languages supported.
    38  // Parameters:
    39  // textContentType - the content type.
    40  // textContent - content to screen.
    41  func (client TextModerationClient) DetectLanguage(ctx context.Context, textContentType string, textContent io.ReadCloser) (result DetectedLanguage, err error) {
    42  	if tracing.IsEnabled() {
    43  		ctx = tracing.StartSpan(ctx, fqdn+"/TextModerationClient.DetectLanguage")
    44  		defer func() {
    45  			sc := -1
    46  			if result.Response.Response != nil {
    47  				sc = result.Response.Response.StatusCode
    48  			}
    49  			tracing.EndSpan(ctx, sc, err)
    50  		}()
    51  	}
    52  	req, err := client.DetectLanguagePreparer(ctx, textContentType, textContent)
    53  	if err != nil {
    54  		err = autorest.NewErrorWithError(err, "contentmoderator.TextModerationClient", "DetectLanguage", nil, "Failure preparing request")
    55  		return
    56  	}
    57  
    58  	resp, err := client.DetectLanguageSender(req)
    59  	if err != nil {
    60  		result.Response = autorest.Response{Response: resp}
    61  		err = autorest.NewErrorWithError(err, "contentmoderator.TextModerationClient", "DetectLanguage", resp, "Failure sending request")
    62  		return
    63  	}
    64  
    65  	result, err = client.DetectLanguageResponder(resp)
    66  	if err != nil {
    67  		err = autorest.NewErrorWithError(err, "contentmoderator.TextModerationClient", "DetectLanguage", resp, "Failure responding to request")
    68  		return
    69  	}
    70  
    71  	return
    72  }
    73  
    74  // DetectLanguagePreparer prepares the DetectLanguage request.
    75  func (client TextModerationClient) DetectLanguagePreparer(ctx context.Context, textContentType string, textContent io.ReadCloser) (*http.Request, error) {
    76  	urlParameters := map[string]interface{}{
    77  		"Endpoint": client.Endpoint,
    78  	}
    79  
    80  	preparer := autorest.CreatePreparer(
    81  		autorest.AsContentType("text/plain"),
    82  		autorest.AsPost(),
    83  		autorest.WithCustomBaseURL("{Endpoint}", urlParameters),
    84  		autorest.WithPath("/contentmoderator/moderate/v1.0/ProcessText/DetectLanguage"),
    85  		autorest.WithFile(textContent),
    86  		autorest.WithHeader("Content-Type", autorest.String(textContentType)))
    87  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
    88  }
    89  
    90  // DetectLanguageSender sends the DetectLanguage request. The method will close the
    91  // http.Response Body if it receives an error.
    92  func (client TextModerationClient) DetectLanguageSender(req *http.Request) (*http.Response, error) {
    93  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
    94  }
    95  
    96  // DetectLanguageResponder handles the response to the DetectLanguage request. The method always
    97  // closes the http.Response Body.
    98  func (client TextModerationClient) DetectLanguageResponder(resp *http.Response) (result DetectedLanguage, err error) {
    99  	err = autorest.Respond(
   100  		resp,
   101  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   102  		autorest.ByUnmarshallingJSON(&result),
   103  		autorest.ByClosing())
   104  	result.Response = autorest.Response{Response: resp}
   105  	return
   106  }
   107  
   108  // ScreenText detects profanity in more than 100 languages and match against custom and shared blacklists.
   109  // Parameters:
   110  // textContentType - the content type.
   111  // textContent - content to screen.
   112  // language - language of the text.
   113  // autocorrect - autocorrect text.
   114  // pii - detect personal identifiable information.
   115  // listID - the list Id.
   116  // classify - classify input.
   117  func (client TextModerationClient) ScreenText(ctx context.Context, textContentType string, textContent io.ReadCloser, language string, autocorrect *bool, pii *bool, listID string, classify *bool) (result Screen, err error) {
   118  	if tracing.IsEnabled() {
   119  		ctx = tracing.StartSpan(ctx, fqdn+"/TextModerationClient.ScreenText")
   120  		defer func() {
   121  			sc := -1
   122  			if result.Response.Response != nil {
   123  				sc = result.Response.Response.StatusCode
   124  			}
   125  			tracing.EndSpan(ctx, sc, err)
   126  		}()
   127  	}
   128  	req, err := client.ScreenTextPreparer(ctx, textContentType, textContent, language, autocorrect, pii, listID, classify)
   129  	if err != nil {
   130  		err = autorest.NewErrorWithError(err, "contentmoderator.TextModerationClient", "ScreenText", nil, "Failure preparing request")
   131  		return
   132  	}
   133  
   134  	resp, err := client.ScreenTextSender(req)
   135  	if err != nil {
   136  		result.Response = autorest.Response{Response: resp}
   137  		err = autorest.NewErrorWithError(err, "contentmoderator.TextModerationClient", "ScreenText", resp, "Failure sending request")
   138  		return
   139  	}
   140  
   141  	result, err = client.ScreenTextResponder(resp)
   142  	if err != nil {
   143  		err = autorest.NewErrorWithError(err, "contentmoderator.TextModerationClient", "ScreenText", resp, "Failure responding to request")
   144  		return
   145  	}
   146  
   147  	return
   148  }
   149  
   150  // ScreenTextPreparer prepares the ScreenText request.
   151  func (client TextModerationClient) ScreenTextPreparer(ctx context.Context, textContentType string, textContent io.ReadCloser, language string, autocorrect *bool, pii *bool, listID string, classify *bool) (*http.Request, error) {
   152  	urlParameters := map[string]interface{}{
   153  		"Endpoint": client.Endpoint,
   154  	}
   155  
   156  	queryParameters := map[string]interface{}{}
   157  	if len(language) > 0 {
   158  		queryParameters["language"] = autorest.Encode("query", language)
   159  	}
   160  	if autocorrect != nil {
   161  		queryParameters["autocorrect"] = autorest.Encode("query", *autocorrect)
   162  	} else {
   163  		queryParameters["autocorrect"] = autorest.Encode("query", false)
   164  	}
   165  	if pii != nil {
   166  		queryParameters["PII"] = autorest.Encode("query", *pii)
   167  	} else {
   168  		queryParameters["PII"] = autorest.Encode("query", false)
   169  	}
   170  	if len(listID) > 0 {
   171  		queryParameters["listId"] = autorest.Encode("query", listID)
   172  	}
   173  	if classify != nil {
   174  		queryParameters["classify"] = autorest.Encode("query", *classify)
   175  	} else {
   176  		queryParameters["classify"] = autorest.Encode("query", false)
   177  	}
   178  
   179  	preparer := autorest.CreatePreparer(
   180  		autorest.AsContentType("text/plain"),
   181  		autorest.AsPost(),
   182  		autorest.WithCustomBaseURL("{Endpoint}", urlParameters),
   183  		autorest.WithPath("/contentmoderator/moderate/v1.0/ProcessText/Screen/"),
   184  		autorest.WithFile(textContent),
   185  		autorest.WithQueryParameters(queryParameters),
   186  		autorest.WithHeader("Content-Type", autorest.String(textContentType)))
   187  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   188  }
   189  
   190  // ScreenTextSender sends the ScreenText request. The method will close the
   191  // http.Response Body if it receives an error.
   192  func (client TextModerationClient) ScreenTextSender(req *http.Request) (*http.Response, error) {
   193  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   194  }
   195  
   196  // ScreenTextResponder handles the response to the ScreenText request. The method always
   197  // closes the http.Response Body.
   198  func (client TextModerationClient) ScreenTextResponder(resp *http.Response) (result Screen, err error) {
   199  	err = autorest.Respond(
   200  		resp,
   201  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   202  		autorest.ByUnmarshallingJSON(&result),
   203  		autorest.ByClosing())
   204  	result.Response = autorest.Response{Response: resp}
   205  	return
   206  }
   207  

View as plain text