using Newtonsoft.Json; using RhSolutions.AddIn; using RhSolutions.Models; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; namespace RhSolutions.Services { public static class RhDatabaseClient { private static HttpClient httpClient = RhSolutionsAddIn.HttpClient; public static async Task GetProduct(string line) { string request = string.Empty; 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 response = await httpClient.GetAsync(request); try { response.EnsureSuccessStatusCode(); string json = await response.Content.ReadAsStringAsync(); var product = JsonConvert.DeserializeObject>(json) .FirstOrDefault(); if (product == null) { return null; } else { return $"{product.ProductSku} {product.Name}"; } } catch { return $"Ошибка сервера {response.StatusCode}"; } } } }