From c86f0b8143965de9fc617d6135ea17ff055e4fd0 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Fri, 7 Apr 2023 07:27:42 +0300 Subject: [PATCH] Refactoring --- .../Services/RhDatabaseClient.cs | 78 +++++++++---------- 1 file changed, 35 insertions(+), 43 deletions(-) diff --git a/RhSolutions.AddIn/Services/RhDatabaseClient.cs b/RhSolutions.AddIn/Services/RhDatabaseClient.cs index 65dfe0e..8edbafc 100644 --- a/RhSolutions.AddIn/Services/RhDatabaseClient.cs +++ b/RhSolutions.AddIn/Services/RhDatabaseClient.cs @@ -1,57 +1,49 @@ -using Microsoft.Extensions.DependencyInjection; -using Newtonsoft.Json; -using RhSolutions; -using RhSolutions.AddIn; -using RhSolutions.Models; -using System; -using System.Collections.Generic; -using System.Linq; +using Newtonsoft.Json; using System.Net; using System.Net.Http; using System.Threading.Tasks; -namespace RhSolutions.Services +namespace RhSolutions.Services; + +public class RhDatabaseClient : IDatabaseClient { - public class RhDatabaseClient : IDatabaseClient + private readonly IServiceProvider serviceProvider; + public HttpStatusCode StatusCode { get; private set; } + + public RhDatabaseClient(IServiceProvider provider) { - private IServiceProvider serviceProvider; - public HttpStatusCode StatusCode { get; private set; } + this.serviceProvider = provider; + } - public RhDatabaseClient(IServiceProvider provider) + public async Task> GetProducts(string line) + { + string request; + + if (Sku.TryParse(line, out var skus)) { - this.serviceProvider = provider; + request = @"https://rh.cebotari.ru/api/products/" + skus.FirstOrDefault().ToString(); } - public async Task> GetProducts(string line) + else { - string request; - - if (Sku.TryParse(line, out var skus)) - { - request = @"https://rh.cebotari.ru/api/products/" + skus.FirstOrDefault().ToString(); - } - - else - { - request = @"https://rh.cebotari.ru/api/search?query=" + line; - } - - var client = serviceProvider.GetRequiredService(); - var response = await client.GetAsync(request); - - try - { - response.EnsureSuccessStatusCode(); - string json = await response.Content.ReadAsStringAsync(); - return JsonConvert.DeserializeObject>(json) ?? Enumerable.Empty(); - } - - catch - { - StatusCode = response.StatusCode; - } - - return Enumerable.Empty(); + request = @"https://rh.cebotari.ru/api/search?query=" + line; } + + var client = serviceProvider.GetRequiredService(); + var response = await client.GetAsync(request); + + try + { + response.EnsureSuccessStatusCode(); + string json = await response.Content.ReadAsStringAsync(); + return JsonConvert.DeserializeObject>(json) ?? Enumerable.Empty(); + } + + catch + { + StatusCode = response.StatusCode; + } + + return Enumerable.Empty(); } } \ No newline at end of file