From 3eb118d6db672eb8e27f2b2478b0688141782654 Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Mon, 29 Nov 2021 15:50:24 +0300 Subject: [PATCH] Async/await ready solution init commit --- AddIn.cs | 24 ++++++++++++++ Assistant/SkuAssist.cs | 65 ++++++++++++++++++++------------------ Functions.cs | 49 ++++------------------------ Rehau.Sku.Assist-AddIn.dna | 2 +- Rehau.Sku.Assist.csproj | 31 ++++-------------- app.config | 15 +++++++++ packages.config | 14 ++------ 7 files changed, 91 insertions(+), 109 deletions(-) create mode 100644 AddIn.cs create mode 100644 app.config diff --git a/AddIn.cs b/AddIn.cs new file mode 100644 index 0000000..dd99667 --- /dev/null +++ b/AddIn.cs @@ -0,0 +1,24 @@ +using ExcelDna.Integration; +using ExcelDna.Registration; + +namespace Rehau.Sku.Assist +{ + public class AddIn : IExcelAddIn + { + public void AutoOpen() + { + RegisterFunctions(); + } + + public void AutoClose() + { + } + + void RegisterFunctions() + { + ExcelRegistration.GetExcelFunctions() + .ProcessAsyncRegistrations(nativeAsyncIfAvailable: false) + .RegisterFunctions(); + } + } +} diff --git a/Assistant/SkuAssist.cs b/Assistant/SkuAssist.cs index 9eb3328..d390a1d 100644 --- a/Assistant/SkuAssist.cs +++ b/Assistant/SkuAssist.cs @@ -1,38 +1,41 @@ -using AngleSharp; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Threading.Tasks; +//using System; +//using AngleSharp; +//using AngleSharp.Dom; +//using System.Linq; +//using System.Net; +//using System.Net.Http; +//using System.Threading.Tasks; -namespace Rehau.Sku.Assist -{ - static class SkuAssist - { - public async static Task GetDocumentAsync(string request, HttpClient httpClient) - { - string uri = "https://shop-rehau.ru/catalogsearch/result/?q=" + request; +//namespace Rehau.Sku.Assist +//{ +// static class SkuAssist +// { +// public async static Task GetContent(string request, HttpClient httpClient) +// { +// string uri = "https://shop-rehau.ru/catalogsearch/result/?q=" + request; +// ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; - ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; - HttpResponseMessage response = await httpClient.GetAsync(uri); - response.EnsureSuccessStatusCode(); +// return await httpClient.GetStringAsync(uri); +// } - IConfiguration config = Configuration.Default; - IBrowsingContext context = BrowsingContext.New(config); +// public async static Task GetDocument(string source) +// { +// IConfiguration config = Configuration.Default; +// IBrowsingContext context = BrowsingContext.New(config); - string source = await response.Content.ReadAsStringAsync(); - return await context.OpenAsync(req => req.Content(source)); - } +// return await context.OpenAsync(req => req.Content(source)); +// } - public static IProduct GetProductFromDocument(AngleSharp.Dom.IDocument document) - { - return document - .All - .Where(e => e.ClassName == "product-item__desc-top") - .Select(e => new Product(e.Children[0].TextContent, e.Children[1].TextContent.Trim(new[] { '\n', ' ' }))) - // .Where(product => !product.Sku.Any(c => char.IsLetter(c))) - .FirstOrDefault(); - } - } -} +// public static IProduct GetProductFromDocument(IDocument document) +// { +// return document +// .All +// .Where(e => e.ClassName == "product-item__desc-top") +// .Select(e => new Product(e.Children[0].TextContent, e.Children[1].TextContent.Trim(new[] { '\n', ' ' }))) +// // .Where(product => !product.Sku.Any(c => char.IsLetter(c))) +// .FirstOrDefault(); +// } +// } +//} diff --git a/Functions.cs b/Functions.cs index 9d3eb28..ab6cedf 100644 --- a/Functions.cs +++ b/Functions.cs @@ -1,53 +1,18 @@ -using ExcelDna.Integration; -using System.Runtime.Caching; +using System; +using ExcelDna.Integration; using System.Net.Http; +using System.Threading.Tasks; namespace Rehau.Sku.Assist { - public class Functions : IExcelAddIn + public class Functions { - private static HttpClient _httpClient; - private static ObjectCache _resultCache = MemoryCache.Default; - - public void AutoClose() - { - } - public void AutoOpen() - { - _httpClient = new HttpClient(); - } + private static HttpClient httpClient = new HttpClient(); [ExcelFunction] - public static object RAUNAME(string request) + public static async Task RAUNAME(string request) { - string cachedResult = _resultCache[request] as string; - - if (cachedResult != null) - { - return cachedResult; - } - - else - { - object result = ExcelAsyncUtil.Run("RAUNAME", null, - delegate - { - var document = SkuAssist.GetDocumentAsync(request, _httpClient).Result; - var product = SkuAssist.GetProductFromDocument(document); - return product.ToString(); - }); - - if (result.Equals(ExcelError.ExcelErrorNA)) - { - return "Загрузка..."; - } - - else - { - _resultCache.Add(request, result, System.DateTime.Now.AddMinutes(20)); - return result.ToString(); - } - } + throw new NotImplementedException(); } } } \ No newline at end of file diff --git a/Rehau.Sku.Assist-AddIn.dna b/Rehau.Sku.Assist-AddIn.dna index b7322e9..bdf8847 100644 --- a/Rehau.Sku.Assist-AddIn.dna +++ b/Rehau.Sku.Assist-AddIn.dna @@ -1,6 +1,6 @@ - +