...

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

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

     1  package inkrecognizer
     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/autorest/validation"
    14  	"github.com/Azure/go-autorest/tracing"
    15  	"net/http"
    16  )
    17  
    18  // Client is the the service is used to perform ink layout and recognition of written words and shapes. Ink strokes
    19  // passed to the service are recognized and organized into recognition results in the response
    20  type Client struct {
    21  	BaseClient
    22  }
    23  
    24  // NewClient creates an instance of the Client client.
    25  func NewClient(endpoint string) Client {
    26  	return Client{New(endpoint)}
    27  }
    28  
    29  // Recognize ink Recognition operation is used to perform ink layout and recognition of written words and shapes. It
    30  // allows passing the ink strokes to the service to get the recognition results in the response.
    31  // Parameters:
    32  // body - the collection of stroke objects to send for analysis
    33  // xMsClientRequestID - the request id used to uniquely identify each request during troubleshooting. This is
    34  // an optional parameter useful for correlating logs and other artifacts.
    35  func (client Client) Recognize(ctx context.Context, body AnalysisRequest, xMsClientRequestID string) (result AnalysisResponse, err error) {
    36  	if tracing.IsEnabled() {
    37  		ctx = tracing.StartSpan(ctx, fqdn+"/Client.Recognize")
    38  		defer func() {
    39  			sc := -1
    40  			if result.Response.Response != nil {
    41  				sc = result.Response.Response.StatusCode
    42  			}
    43  			tracing.EndSpan(ctx, sc, err)
    44  		}()
    45  	}
    46  	if err := validation.Validate([]validation.Validation{
    47  		{TargetValue: body,
    48  			Constraints: []validation.Constraint{{Target: "body.Language", Name: validation.Null, Rule: true, Chain: nil},
    49  				{Target: "body.Strokes", Name: validation.Null, Rule: true, Chain: nil}}}}); err != nil {
    50  		return result, validation.NewError("inkrecognizer.Client", "Recognize", err.Error())
    51  	}
    52  
    53  	req, err := client.RecognizePreparer(ctx, body, xMsClientRequestID)
    54  	if err != nil {
    55  		err = autorest.NewErrorWithError(err, "inkrecognizer.Client", "Recognize", nil, "Failure preparing request")
    56  		return
    57  	}
    58  
    59  	resp, err := client.RecognizeSender(req)
    60  	if err != nil {
    61  		result.Response = autorest.Response{Response: resp}
    62  		err = autorest.NewErrorWithError(err, "inkrecognizer.Client", "Recognize", resp, "Failure sending request")
    63  		return
    64  	}
    65  
    66  	result, err = client.RecognizeResponder(resp)
    67  	if err != nil {
    68  		err = autorest.NewErrorWithError(err, "inkrecognizer.Client", "Recognize", resp, "Failure responding to request")
    69  		return
    70  	}
    71  
    72  	return
    73  }
    74  
    75  // RecognizePreparer prepares the Recognize request.
    76  func (client Client) RecognizePreparer(ctx context.Context, body AnalysisRequest, xMsClientRequestID string) (*http.Request, error) {
    77  	urlParameters := map[string]interface{}{
    78  		"Endpoint": client.Endpoint,
    79  	}
    80  
    81  	preparer := autorest.CreatePreparer(
    82  		autorest.AsContentType("application/json; charset=utf-8"),
    83  		autorest.AsPut(),
    84  		autorest.WithCustomBaseURL("https://{Endpoint}/inkrecognizer/v1.0-preview", urlParameters),
    85  		autorest.WithPath("/recognize"),
    86  		autorest.WithJSON(body))
    87  	if len(xMsClientRequestID) > 0 {
    88  		preparer = autorest.DecoratePreparer(preparer,
    89  			autorest.WithHeader("x-ms-client-request-id", autorest.String(xMsClientRequestID)))
    90  	}
    91  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
    92  }
    93  
    94  // RecognizeSender sends the Recognize request. The method will close the
    95  // http.Response Body if it receives an error.
    96  func (client Client) RecognizeSender(req *http.Request) (*http.Response, error) {
    97  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
    98  }
    99  
   100  // RecognizeResponder handles the response to the Recognize request. The method always
   101  // closes the http.Response Body.
   102  func (client Client) RecognizeResponder(resp *http.Response) (result AnalysisResponse, err error) {
   103  	err = autorest.Respond(
   104  		resp,
   105  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   106  		autorest.ByUnmarshallingJSON(&result),
   107  		autorest.ByClosing())
   108  	result.Response = autorest.Response{Response: resp}
   109  	return
   110  }
   111  

View as plain text