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 { public static async Task GetProduct(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 response = await RhSolutionsAddIn.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}"; } } } }