RhSolutions-AddIn/RhSolutions.AddIn/Services/RhDatabaseClient.cs

52 lines
1.4 KiB
C#
Raw Normal View History

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
{
public static async Task<object> GetProduct(string line)
{
2022-12-26 14:46:37 +03:00
string request;
2022-12-20 12:03:05 +03:00
if (Sku.TryParse(line, out var skus))
2022-12-21 08:02:42 +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-26 14:46:37 +03:00
var response = await RhSolutionsAddIn.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
{
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
{
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
}
}
}
}