aboutsummaryrefslogtreecommitdiff
path: root/src/AddIn
diff options
context:
space:
mode:
authorSergey Chebotar <s.chebotar@gmail.com>2022-12-19 09:13:14 +0300
committerSergey Chebotar <s.chebotar@gmail.com>2022-12-19 09:13:14 +0300
commit012ec9d01016faa0a6ee0cbbae169311b959897d (patch)
treebf79e8d036542f9672ecc0f50cca260f816d2bd8 /src/AddIn
parent75e7f9aae48724fd7eea042642e8e17304c03de4 (diff)
Basic remote database implementation
Diffstat (limited to 'src/AddIn')
-rw-r--r--src/AddIn/AddIn.cs2
-rw-r--r--src/AddIn/Functions.cs66
2 files changed, 68 insertions, 0 deletions
diff --git a/src/AddIn/AddIn.cs b/src/AddIn/AddIn.cs
index 1606624..a60fc9e 100644
--- a/src/AddIn/AddIn.cs
+++ b/src/AddIn/AddIn.cs
@@ -10,10 +10,12 @@ namespace RehauSku
class AddIn : IExcelAddIn
{
public static Application Excel;
+ public static HttpClient httpClient;
public void AutoOpen()
{
Excel = (Application)ExcelDnaUtil.Application;
+ httpClient = new HttpClient();
RegisterFunctions();
IntelliSenseServer.Install();
RegistryUtil.Initialize();
diff --git a/src/AddIn/Functions.cs b/src/AddIn/Functions.cs
index c202a3b..54d50a5 100644
--- a/src/AddIn/Functions.cs
+++ b/src/AddIn/Functions.cs
@@ -1,4 +1,11 @@
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 RehauSku
{
@@ -14,5 +21,64 @@ namespace RehauSku
else return ExcelError.ExcelErrorNA;
}
+
+ [ExcelFunction(Description = "Запрос в удаленную базу данных")]
+ public static object RHSOLUTIONS([ExcelArgument(Name = "Запрос")] string line)
+ {
+ object result;
+
+ result = ExcelAsyncUtil.Run("Database request", line, delegate
+ {
+ return RhDatabaseClient.GetProduct(line).GetAwaiter().GetResult();
+ });
+
+ if (result == null)
+ {
+ return ExcelError.ExcelErrorNA;
+ }
+
+ if (result.Equals(ExcelError.ExcelErrorNA))
+ {
+ return "Загрузка...";
+ }
+
+ 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; }
+ }
}
} \ No newline at end of file