1 package inkrecognizer
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 "net/http"
16 )
17
18
19
20 type Client struct {
21 BaseClient
22 }
23
24
25 func NewClient(endpoint string) Client {
26 return Client{New(endpoint)}
27 }
28
29
30
31
32
33
34
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
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
95
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
101
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