diff options
author | Serghei Cebotari <51533848+schebotar@users.noreply.github.com> | 2021-11-29 16:36:58 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-29 16:36:58 +0300 |
commit | 15995027bfbc535e4b9ffa159607caaffdecc478 (patch) | |
tree | 06c5c8ab66889c6e3dac0819280ef1b1bffcf3ac /Functions.cs | |
parent | 0fe8e038af7f33eae824bba263e7c54dea829679 (diff) | |
parent | e9ec1df01b752b423b66cb30dc2025a396f769f5 (diff) |
Merge pull request #1 from schebotar/experimental
Experimental
Diffstat (limited to 'Functions.cs')
-rw-r--r-- | Functions.cs | 52 |
1 files changed, 10 insertions, 42 deletions
diff --git a/Functions.cs b/Functions.cs index 9d3eb28..9112822 100644 --- a/Functions.cs +++ b/Functions.cs @@ -1,53 +1,21 @@ -using ExcelDna.Integration; -using System.Runtime.Caching; +using AngleSharp.Dom; +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<string> 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(); - } - } + Task<string> contentTask = Task.Run(() => SkuAssist.GetContent(request, httpClient)); + Task<IDocument> documentTask = await contentTask.ContinueWith(content => SkuAssist.GetDocument(content)); + IProduct product = await documentTask.ContinueWith(doc => SkuAssist.GetProductFromDocument(doc.Result)); + return product.ToString(); } } }
\ No newline at end of file |