...

Source file src/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v2.0/luis/runtime/prediction.go

Documentation: github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v2.0/luis/runtime

     1  package runtime
     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  	"github.com/gofrs/uuid"
    16  	"net/http"
    17  )
    18  
    19  // PredictionClient is the client for the Prediction methods of the Runtime service.
    20  type PredictionClient struct {
    21  	BaseClient
    22  }
    23  
    24  // NewPredictionClient creates an instance of the PredictionClient client.
    25  func NewPredictionClient(endpoint string) PredictionClient {
    26  	return PredictionClient{New(endpoint)}
    27  }
    28  
    29  // Resolve gets predictions for a given utterance, in the form of intents and entities. The current maximum query size
    30  // is 500 characters.
    31  // Parameters:
    32  // appID - the LUIS application ID (Guid).
    33  // query - the utterance to predict.
    34  // timezoneOffset - the timezone offset for the location of the request.
    35  // verbose - if true, return all intents instead of just the top scoring intent.
    36  // staging - use the staging endpoint slot.
    37  // spellCheck - enable spell checking.
    38  // bingSpellCheckSubscriptionKey - the subscription key to use when enabling Bing spell check
    39  // logParameter - log query (default is true)
    40  func (client PredictionClient) Resolve(ctx context.Context, appID uuid.UUID, query string, timezoneOffset *float64, verbose *bool, staging *bool, spellCheck *bool, bingSpellCheckSubscriptionKey string, logParameter *bool) (result LuisResult, err error) {
    41  	if tracing.IsEnabled() {
    42  		ctx = tracing.StartSpan(ctx, fqdn+"/PredictionClient.Resolve")
    43  		defer func() {
    44  			sc := -1
    45  			if result.Response.Response != nil {
    46  				sc = result.Response.Response.StatusCode
    47  			}
    48  			tracing.EndSpan(ctx, sc, err)
    49  		}()
    50  	}
    51  	if err := validation.Validate([]validation.Validation{
    52  		{TargetValue: query,
    53  			Constraints: []validation.Constraint{{Target: "query", Name: validation.MaxLength, Rule: 500, Chain: nil}}}}); err != nil {
    54  		return result, validation.NewError("runtime.PredictionClient", "Resolve", err.Error())
    55  	}
    56  
    57  	req, err := client.ResolvePreparer(ctx, appID, query, timezoneOffset, verbose, staging, spellCheck, bingSpellCheckSubscriptionKey, logParameter)
    58  	if err != nil {
    59  		err = autorest.NewErrorWithError(err, "runtime.PredictionClient", "Resolve", nil, "Failure preparing request")
    60  		return
    61  	}
    62  
    63  	resp, err := client.ResolveSender(req)
    64  	if err != nil {
    65  		result.Response = autorest.Response{Response: resp}
    66  		err = autorest.NewErrorWithError(err, "runtime.PredictionClient", "Resolve", resp, "Failure sending request")
    67  		return
    68  	}
    69  
    70  	result, err = client.ResolveResponder(resp)
    71  	if err != nil {
    72  		err = autorest.NewErrorWithError(err, "runtime.PredictionClient", "Resolve", resp, "Failure responding to request")
    73  		return
    74  	}
    75  
    76  	return
    77  }
    78  
    79  // ResolvePreparer prepares the Resolve request.
    80  func (client PredictionClient) ResolvePreparer(ctx context.Context, appID uuid.UUID, query string, timezoneOffset *float64, verbose *bool, staging *bool, spellCheck *bool, bingSpellCheckSubscriptionKey string, logParameter *bool) (*http.Request, error) {
    81  	urlParameters := map[string]interface{}{
    82  		"Endpoint": client.Endpoint,
    83  	}
    84  
    85  	pathParameters := map[string]interface{}{
    86  		"appId": autorest.Encode("path", appID),
    87  	}
    88  
    89  	queryParameters := map[string]interface{}{}
    90  	if timezoneOffset != nil {
    91  		queryParameters["timezoneOffset"] = autorest.Encode("query", *timezoneOffset)
    92  	}
    93  	if verbose != nil {
    94  		queryParameters["verbose"] = autorest.Encode("query", *verbose)
    95  	}
    96  	if staging != nil {
    97  		queryParameters["staging"] = autorest.Encode("query", *staging)
    98  	}
    99  	if spellCheck != nil {
   100  		queryParameters["spellCheck"] = autorest.Encode("query", *spellCheck)
   101  	}
   102  	if len(bingSpellCheckSubscriptionKey) > 0 {
   103  		queryParameters["bing-spell-check-subscription-key"] = autorest.Encode("query", bingSpellCheckSubscriptionKey)
   104  	}
   105  	if logParameter != nil {
   106  		queryParameters["log"] = autorest.Encode("query", *logParameter)
   107  	}
   108  
   109  	preparer := autorest.CreatePreparer(
   110  		autorest.AsContentType("application/json; charset=utf-8"),
   111  		autorest.AsPost(),
   112  		autorest.WithCustomBaseURL("{Endpoint}/luis/v2.0", urlParameters),
   113  		autorest.WithPathParameters("/apps/{appId}", pathParameters),
   114  		autorest.WithJSON(query),
   115  		autorest.WithQueryParameters(queryParameters))
   116  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   117  }
   118  
   119  // ResolveSender sends the Resolve request. The method will close the
   120  // http.Response Body if it receives an error.
   121  func (client PredictionClient) ResolveSender(req *http.Request) (*http.Response, error) {
   122  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   123  }
   124  
   125  // ResolveResponder handles the response to the Resolve request. The method always
   126  // closes the http.Response Body.
   127  func (client PredictionClient) ResolveResponder(resp *http.Response) (result LuisResult, err error) {
   128  	err = autorest.Respond(
   129  		resp,
   130  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   131  		autorest.ByUnmarshallingJSON(&result),
   132  		autorest.ByClosing())
   133  	result.Response = autorest.Response{Response: resp}
   134  	return
   135  }
   136  

View as plain text