RhDatabase Client extract class
This commit is contained in:
parent
e427d63e77
commit
bfada8d605
@ -1,27 +1,10 @@
|
||||
using ExcelDna.Integration;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RhSolutions
|
||||
{
|
||||
public class Functions
|
||||
{
|
||||
[ExcelFunction(Description = "Получение корректного артикула из строки")]
|
||||
public static object GETRAUSKU([ExcelArgument(Name = "\"Строка\"", Description = "строка, содержащая актикул")] string line)
|
||||
{
|
||||
if (RauSku.TryParse(line, out RauSku rausku))
|
||||
{
|
||||
return rausku.ToString();
|
||||
}
|
||||
|
||||
else return ExcelError.ExcelErrorNA;
|
||||
}
|
||||
|
||||
[ExcelFunction(Description = "Запрос в удаленную базу данных")]
|
||||
public static object RHSOLUTIONS([ExcelArgument(Name = "Запрос")] string line)
|
||||
{
|
||||
@ -45,40 +28,4 @@ namespace RhSolutions
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public static class RhDatabaseClient
|
||||
{
|
||||
private static HttpClient httpClient = AddIn.httpClient;
|
||||
|
||||
public static async Task<object> GetProduct(string line)
|
||||
{
|
||||
string request = @"https://rh.cebotari.ru/api/search?query=" + line;
|
||||
|
||||
ServicePointManager.SecurityProtocol =
|
||||
SecurityProtocolType.Tls12 |
|
||||
SecurityProtocolType.Tls11 |
|
||||
SecurityProtocolType.Tls;
|
||||
|
||||
string response = await httpClient.GetStringAsync(request);
|
||||
|
||||
var products = JsonConvert.DeserializeObject<IEnumerable<DbProduct>>(response);
|
||||
|
||||
var product = products.FirstOrDefault();
|
||||
|
||||
if (product == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $"{product.productSku} {product.name}";
|
||||
}
|
||||
}
|
||||
|
||||
private class DbProduct
|
||||
{
|
||||
public string productSku { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
45
src/AddIn/RhDatabaseClient.cs
Normal file
45
src/AddIn/RhDatabaseClient.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RhSolutions
|
||||
{
|
||||
public static class RhDatabaseClient
|
||||
{
|
||||
private static HttpClient httpClient = AddIn.httpClient;
|
||||
|
||||
public static async Task<object> GetProduct(string line)
|
||||
{
|
||||
string request = @"https://rh.cebotari.ru/api/search?query=" + line;
|
||||
|
||||
ServicePointManager.SecurityProtocol =
|
||||
SecurityProtocolType.Tls12 |
|
||||
SecurityProtocolType.Tls11 |
|
||||
SecurityProtocolType.Tls;
|
||||
|
||||
string response = await httpClient.GetStringAsync(request);
|
||||
|
||||
var products = JsonConvert.DeserializeObject<IEnumerable<DbProduct>>(response);
|
||||
|
||||
var product = products.FirstOrDefault();
|
||||
|
||||
if (product == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $"{product.productSku} {product.name}";
|
||||
}
|
||||
}
|
||||
|
||||
private class DbProduct
|
||||
{
|
||||
public string productSku { get; set; }
|
||||
public string name { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user