diff options
author | Sergey Chebotar <s.chebotar@gmail.com> | 2023-04-07 07:27:42 +0300 |
---|---|---|
committer | Sergey Chebotar <s.chebotar@gmail.com> | 2023-04-07 07:27:42 +0300 |
commit | c86f0b8143965de9fc617d6135ea17ff055e4fd0 (patch) | |
tree | 9cde5154643f6a128a29c6af886b1e12bbd3ba09 | |
parent | 75cb3c4f5d82ca475757d7ff3ac86d04dc0be6d9 (diff) |
Refactoring
-rw-r--r-- | RhSolutions.AddIn/Services/RhDatabaseClient.cs | 78 |
1 files 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<IEnumerable<Product>> 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<IEnumerable<Product>> 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<HttpClient>(); - var response = await client.GetAsync(request); - - try - { - response.EnsureSuccessStatusCode(); - string json = await response.Content.ReadAsStringAsync(); - return JsonConvert.DeserializeObject<IEnumerable<Product>>(json) ?? Enumerable.Empty<Product>(); - } - - catch - { - StatusCode = response.StatusCode; - } - - return Enumerable.Empty<Product>(); + request = @"https://rh.cebotari.ru/api/search?query=" + line; } + + var client = serviceProvider.GetRequiredService<HttpClient>(); + var response = await client.GetAsync(request); + + try + { + response.EnsureSuccessStatusCode(); + string json = await response.Content.ReadAsStringAsync(); + return JsonConvert.DeserializeObject<IEnumerable<Product>>(json) ?? Enumerable.Empty<Product>(); + } + + catch + { + StatusCode = response.StatusCode; + } + + return Enumerable.Empty<Product>(); } }
\ No newline at end of file |