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 System.Net; using System.Net.Http; using System.Threading.Tasks; namespace RhSolutions.Services { public class RhDatabaseClient : IDatabaseClient { private IServiceProvider serviceProvider; public HttpStatusCode StatusCode { get; private set; } public RhDatabaseClient(IServiceProvider provider) { this.serviceProvider = provider; } public async Task> GetProducts(string line) { 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(); } } }