private async Task MakeOCRRequest()

in Hands-on lab/lab-files/src/TollBooth/TollBooth/FindLicensePlateText.cs [39:82]


        private async Task<string> MakeOCRRequest(byte[] imageBytes)
        {
            _log.LogInformation("Making OCR request");
            var licensePlate = string.Empty;
            // Request parameters.
            const string requestParameters = "language=unk&detectOrientation=true";
            // Get the API URL and the API key from settings.
            // TODO 2: Populate the below two variables with the correct AppSettings properties.
            var uriBase = Environment.GetEnvironmentVariable("");
            var apiKey = Environment.GetEnvironmentVariable("");

            var resiliencyStrategy = DefineAndRetrieveResiliencyStrategy();

            // Configure the HttpClient request headers.
            _client.DefaultRequestHeaders.Clear();
            _client.DefaultRequestHeaders.Accept.Clear();
            _client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey);
            _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // Assemble the URI for the REST API Call.
            var uri = uriBase + "?" + requestParameters;

            try
            {
                // Execute the REST API call, implementing our resiliency strategy.
                HttpResponseMessage response = await resiliencyStrategy.ExecuteAsync(() => _client.PostAsync(uri, GetImageHttpContent(imageBytes)));

                // Get the JSON response.
                var result = await response.Content.ReadAsAsync<OCRResult>();
                licensePlate = GetLicensePlateTextFromResult(result);
            }
            catch (BrokenCircuitException bce)
            {
                _log.LogCritical($"Could not contact the Computer Vision API service due to the following error: {bce.Message}");
            }
            catch (Exception e)
            {
                _log.LogCritical($"Critical error: {e.Message}", e);
            }

            _log.LogInformation($"Finished OCR request. Result: {licensePlate}");

            return licensePlate;
        }