private Request CreateInkRecognitionRequest(HttpPipeline pipeline, IEnumerable <InkStroke> strokes, ApplicationKind applicationKind, string language, InkPointUnit inkPointUnit, float unitMultiple) { Request request = pipeline.CreateRequest(); // add content var inkRecognitionRequest = new InkRecognitionRequest(strokes, applicationKind, language, inkPointUnit, unitMultiple); var content = new MemoryStream(Encoding.UTF8.GetBytes(inkRecognitionRequest.ToJson())); request.Content = RequestContent.Create(content); // specify HTTP request line request.Method = RequestMethod.Put; var requestUri = new RequestUriBuilder(); requestUri.Reset(_endpoint); request.Uri.Scheme = requestUri.Scheme; request.Uri.Host = requestUri.Host; request.Uri.Port = requestUri.Port; request.Uri.Path = requestUri.Path; request.Uri.Query = requestUri.Query; // add headers for authentication _credential.SetRequestCredentials(request); return(request); }
internal InkRecognitionRequest(IEnumerable <InkStroke> strokes, ApplicationKind applicationType, string language, InkPointUnit inkPointUnit, float unitMultiple) { _strokes = strokes; ApplicationType = applicationType; Language = language; InkPointUnit = inkPointUnit; UnitMultiple = unitMultiple; }
/// <summary> /// Synchronously sends data to the service and generates a tree structure containing the model results. /// </summary> /// <param name="strokes">The list of ink strokes to recognize.</param> /// <param name="unit"> The physical unit for the points in the stroke.</param> /// <param name="unitMultiple"> A multiplier applied to the unit value to indicate the true unit being used. /// This allows the caller to specify values in a fraction or multiple of a unit.</param> /// <param name="language"> IETF BCP 47 language code (for ex. en-US, en-GB, hi-IN etc.) for the strokes. /// This is only needed when the language is different from the default set when the client was instantiated.</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> /// <returns> RecognitionRoot containing the model results in a hierarchy. </returns> /// <exception cref="RequestFailedException"></exception> /// <exception cref="ArgumentNullException"></exception> public virtual Response <RecognitionRoot> RecognizeInk( IEnumerable <InkStroke> strokes, InkPointUnit unit, float unitMultiple, string language, CancellationToken cancellationToken = default(CancellationToken)) { if (strokes == null) { throw new ArgumentNullException(nameof(strokes)); } try { // var policies = new HttpPipelinePolicy[] { _options.TelemetryPolicy, _options.LoggingPolicy, _options.RetryPolicy }; //var pipeline = HttpPipelineBuilder.Build(_options, true, policies); var pipeline = HttpPipelineBuilder.Build(_options, null); var request = CreateInkRecognitionRequest(pipeline, strokes, _options.ApplicationKind, language, unit, unitMultiple); var response = pipeline.SendRequest(request, cancellationToken); var reader = new StreamReader(response.ContentStream); string responseText = reader.ReadToEnd(); if (response.Status == 200) { var root = InkRecognitionResponse.Parse(responseText); return(Response.FromValue <RecognitionRoot>(root, response)); } // For bad requests and internal server errors else if (response.Status >= 400 && response.Status < 600) { var serverError = new HttpErrorDetails(responseText); throw new RequestFailedException(serverError.ToString()); } else { throw new RequestFailedException(responseText); } } catch (Exception) { throw; } }