RhSolutions-AddIn/RhSolutions.AddIn/Services/RhDatabaseClient.cs
2023-03-22 08:36:13 +03:00

52 lines
1.4 KiB
C#

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<object> 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<IEnumerable<Product>>(json)
.FirstOrDefault();
if (product == null)
{
return null;
}
else
{
return $"{product.ProductSku} {product.Name}";
}
}
catch
{
return $"Ошибка сервера {response.StatusCode}";
}
}
}
}