aboutsummaryrefslogtreecommitdiff
path: root/Functions.cs
blob: 9d3eb28e6c744a0d6f644f5cc05684b9c62c3b81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using ExcelDna.Integration;
using System.Runtime.Caching;
using System.Net.Http;

namespace Rehau.Sku.Assist
{
    public class Functions : IExcelAddIn
    {
        private static HttpClient _httpClient;
        private static ObjectCache _resultCache = MemoryCache.Default;

        public void AutoClose()
        {
        }
        public void AutoOpen()
        {
            _httpClient = new HttpClient();
        }

        [ExcelFunction]
        public static object 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();
                }
            }
        }
    }
}