2022-12-20 12:03:05 +03:00
|
|
|
|
using Newtonsoft.Json;
|
2022-12-20 12:41:46 +03:00
|
|
|
|
using RhSolutions.AddIn;
|
2022-12-20 13:00:16 +03:00
|
|
|
|
using RhSolutions.Models;
|
2022-12-21 08:02:42 +03:00
|
|
|
|
using System;
|
2022-12-20 12:03:05 +03:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2022-12-20 12:27:47 +03:00
|
|
|
|
namespace RhSolutions.Services
|
2022-12-20 12:03:05 +03:00
|
|
|
|
{
|
|
|
|
|
public static class RhDatabaseClient
|
|
|
|
|
{
|
2022-12-20 12:27:47 +03:00
|
|
|
|
private static HttpClient httpClient = RhSolutionsAddIn.httpClient;
|
2022-12-20 12:03:05 +03:00
|
|
|
|
|
|
|
|
|
public static async Task<object> GetProduct(string line)
|
|
|
|
|
{
|
2022-12-21 08:02:42 +03:00
|
|
|
|
string request = string.Empty;
|
2022-12-20 12:03:05 +03:00
|
|
|
|
|
2022-12-22 16:00:38 +03:00
|
|
|
|
if (Sku.TryParse(line, out var skus))
|
2022-12-21 08:02:42 +03:00
|
|
|
|
{
|
2022-12-22 16:00:38 +03:00
|
|
|
|
request = @"https://rh.cebotari.ru/api/products/" + skus.FirstOrDefault().ToString();
|
2022-12-21 08:02:42 +03:00
|
|
|
|
}
|
2022-12-20 12:03:05 +03:00
|
|
|
|
|
2022-12-21 08:02:42 +03:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
request = @"https://rh.cebotari.ru/api/search?query=" + line;
|
|
|
|
|
}
|
2022-12-20 12:03:05 +03:00
|
|
|
|
|
2022-12-21 08:02:42 +03:00
|
|
|
|
var response = await httpClient.GetAsync(request);
|
2022-12-20 12:03:05 +03:00
|
|
|
|
|
2022-12-21 08:02:42 +03:00
|
|
|
|
try
|
2022-12-20 12:03:05 +03:00
|
|
|
|
{
|
2022-12-22 16:00:38 +03:00
|
|
|
|
response.EnsureSuccessStatusCode();
|
2022-12-21 08:02:42 +03:00
|
|
|
|
string json = await response.Content.ReadAsStringAsync();
|
|
|
|
|
var product = JsonConvert.DeserializeObject<IEnumerable<Product>>(json)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (product == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-12-22 16:00:38 +03:00
|
|
|
|
return $"{product.ProductSku} {product.Name}";
|
2022-12-21 08:02:42 +03:00
|
|
|
|
}
|
2022-12-20 12:03:05 +03:00
|
|
|
|
}
|
2022-12-21 08:02:42 +03:00
|
|
|
|
catch
|
2022-12-20 12:03:05 +03:00
|
|
|
|
{
|
2022-12-21 08:02:42 +03:00
|
|
|
|
return $"Ошибка сервера {response.StatusCode}";
|
2022-12-20 12:03:05 +03:00
|
|
|
|
}
|
2022-12-21 08:02:42 +03:00
|
|
|
|
|
2022-12-20 12:03:05 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|