From e1289aebbc2cd3b2a125e910f48a8cb695997d24 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Thu, 14 Nov 2024 00:23:38 +0300 Subject: [PATCH] Inject IAddinConfiguration to OcrClient --- RhSolutions.AddIn/Services/IOcrClient.cs | 2 +- RhSolutions.AddIn/Services/YandexOcrClient.cs | 12 ++++++++---- RhSolutions.AddIn/Tools/OcrTool.cs | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/RhSolutions.AddIn/Services/IOcrClient.cs b/RhSolutions.AddIn/Services/IOcrClient.cs index 0233d14..e22a49e 100644 --- a/RhSolutions.AddIn/Services/IOcrClient.cs +++ b/RhSolutions.AddIn/Services/IOcrClient.cs @@ -4,5 +4,5 @@ namespace RhSolutions.Services; public interface IOcrClient { - public Task> ProcessImage(string base64Image, string xFolderId, string apiKey); + public Task> ProcessImage(string base64Image); } diff --git a/RhSolutions.AddIn/Services/YandexOcrClient.cs b/RhSolutions.AddIn/Services/YandexOcrClient.cs index c79dda4..960d2f7 100644 --- a/RhSolutions.AddIn/Services/YandexOcrClient.cs +++ b/RhSolutions.AddIn/Services/YandexOcrClient.cs @@ -9,13 +9,17 @@ namespace RhSolutions.Services; public class YandexOcrClient : IOcrClient { private readonly HttpClient _httpClient; - public YandexOcrClient(HttpClient httpClient) + private readonly string _apiKey; + private readonly string _xFolderId; + public YandexOcrClient(HttpClient httpClient, IAddInConfiguration configuration) { _httpClient = httpClient; _httpClient.BaseAddress = new Uri("https://ocr.api.cloud.yandex.net/ocr/v1/"); + _apiKey = configuration["apiKey"]; + _xFolderId = configuration["x-folder-id"]; } - public async Task> ProcessImage(string base64Image, string xFolderId, string apiKey) + public async Task> ProcessImage(string base64Image) { using StringContent jsonContent = new( JsonConvert.SerializeObject(new @@ -27,8 +31,8 @@ public class YandexOcrClient : IOcrClient }), Encoding.UTF8, "application/json"); - _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Api-Key", apiKey); - _httpClient.DefaultRequestHeaders.Add("x-folder-id", xFolderId); + _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Api-Key", _apiKey); + _httpClient.DefaultRequestHeaders.Add("x-folder-id", _xFolderId); _httpClient.DefaultRequestHeaders.Add("x-data-logging-enable", "true"); using HttpResponseMessage response = await _httpClient.PostAsync("recognizeText", jsonContent); diff --git a/RhSolutions.AddIn/Tools/OcrTool.cs b/RhSolutions.AddIn/Tools/OcrTool.cs index 2869537..96ec4ca 100644 --- a/RhSolutions.AddIn/Tools/OcrTool.cs +++ b/RhSolutions.AddIn/Tools/OcrTool.cs @@ -27,7 +27,7 @@ internal class OcrTool : ITool if (shot != null) { - IEnumerable tables = await client.ProcessImage(shot, xFolderId, apiKey); + IEnumerable tables = await client.ProcessImage(shot); if (tables != null) { foreach (var table in tables)