1 package runtime
2
3
4
5
6
7
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
20 type PredictionClient struct {
21 BaseClient
22 }
23
24
25 func NewPredictionClient(endpoint string) PredictionClient {
26 return PredictionClient{New(endpoint)}
27 }
28
29
30
31
32
33
34
35
36
37
38
39
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
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
120
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
126
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