Inject IAddinConfiguration to OcrClient

This commit is contained in:
Serghei Cebotari 2024-11-14 00:23:38 +03:00
parent 2026e2a4e2
commit e1289aebbc
3 changed files with 10 additions and 6 deletions

View File

@ -4,5 +4,5 @@ namespace RhSolutions.Services;
public interface IOcrClient
{
public Task<IEnumerable<object[,]>> ProcessImage(string base64Image, string xFolderId, string apiKey);
public Task<IEnumerable<object[,]>> ProcessImage(string base64Image);
}

View File

@ -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<IEnumerable<object[,]>> ProcessImage(string base64Image, string xFolderId, string apiKey)
public async Task<IEnumerable<object[,]>> 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);

View File

@ -27,7 +27,7 @@ internal class OcrTool : ITool
if (shot != null)
{
IEnumerable<object[,]> tables = await client.ProcessImage(shot, xFolderId, apiKey);
IEnumerable<object[,]> tables = await client.ProcessImage(shot);
if (tables != null)
{
foreach (var table in tables)