Добавлен кэшинг результатов запроса, интерфейс IProduct
This commit is contained in:
parent
f5234e956c
commit
0fe8e038af
9
Assistant/IProduct.cs
Normal file
9
Assistant/IProduct.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
namespace Rehau.Sku.Assist
|
||||||
|
{
|
||||||
|
interface IProduct
|
||||||
|
{
|
||||||
|
string Sku { get; }
|
||||||
|
string Name { get; }
|
||||||
|
string Uri { get; }
|
||||||
|
}
|
||||||
|
}
|
21
Assistant/Product.cs
Normal file
21
Assistant/Product.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
namespace Rehau.Sku.Assist
|
||||||
|
{
|
||||||
|
public class Product : IProduct
|
||||||
|
{
|
||||||
|
public string Sku { get; }
|
||||||
|
public string Name { get; }
|
||||||
|
|
||||||
|
public string Uri => throw new System.NotImplementedException();
|
||||||
|
|
||||||
|
public Product(string sku, string name)
|
||||||
|
{
|
||||||
|
Sku = sku;
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"{this.Name} ({this.Sku})";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
using System.Net.Http;
|
using AngleSharp;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using AngleSharp;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Rehau.Sku.Assist
|
namespace Rehau.Sku.Assist
|
||||||
{
|
{
|
||||||
@ -23,16 +23,14 @@ namespace Rehau.Sku.Assist
|
|||||||
return await context.OpenAsync(req => req.Content(source));
|
return await context.OpenAsync(req => req.Content(source));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetResultFromDocument(AngleSharp.Dom.IDocument document)
|
public static IProduct GetProductFromDocument(AngleSharp.Dom.IDocument document)
|
||||||
{
|
{
|
||||||
var result = document
|
return document
|
||||||
.All
|
.All
|
||||||
.Where(e => e.ClassName == "product-item__desc-top")
|
.Where(e => e.ClassName == "product-item__desc-top")
|
||||||
.Select(e => new { sku = e.Children[0].TextContent, title = e.Children[1].TextContent.Trim(new[] { '\n', ' ' }) })
|
.Select(e => new Product(e.Children[0].TextContent, e.Children[1].TextContent.Trim(new[] { '\n', ' ' })))
|
||||||
.Where(t => !t.sku.Any(c => char.IsLetter(c)))
|
// .Where(product => !product.Sku.Any(c => char.IsLetter(c)))
|
||||||
.FirstOrDefault();
|
.FirstOrDefault();
|
||||||
|
|
||||||
return result == null ? "Не найдено" : $"{result.title} ({result.sku})";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
48
Functions.cs
48
Functions.cs
@ -1,27 +1,53 @@
|
|||||||
using ExcelDna.Integration;
|
using ExcelDna.Integration;
|
||||||
|
using System.Runtime.Caching;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
|
||||||
namespace Rehau.Sku.Assist
|
namespace Rehau.Sku.Assist
|
||||||
{
|
{
|
||||||
public class Functions : IExcelAddIn
|
public class Functions : IExcelAddIn
|
||||||
{
|
{
|
||||||
static readonly HttpClient httpClient = new HttpClient();
|
private static HttpClient _httpClient;
|
||||||
|
private static ObjectCache _resultCache = MemoryCache.Default;
|
||||||
public static object RAUNAME(string request)
|
|
||||||
{
|
|
||||||
return ExcelAsyncUtil.Run("RAUNAME", request, delegate
|
|
||||||
{
|
|
||||||
var document = SkuAssist.GetDocumentAsync(request, httpClient).Result;
|
|
||||||
return SkuAssist.GetResultFromDocument(document);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AutoClose()
|
public void AutoClose()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AutoOpen()
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -38,26 +38,49 @@
|
|||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="ExcelDna.Integration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=f225e9659857edbe, processorArchitecture=MSIL">
|
<Reference Include="ExcelDna.Integration, Version=1.1.0.0, Culture=neutral, PublicKeyToken=f225e9659857edbe, processorArchitecture=MSIL">
|
||||||
<HintPath>packages\ExcelDna.Integration.1.5.0\lib\net452\ExcelDna.Integration.dll</HintPath>
|
<HintPath>packages\ExcelDna.Integration.1.5.0\lib\net452\ExcelDna.Integration.dll</HintPath>
|
||||||
|
<Private>False</Private>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||||
<HintPath>packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
|
<HintPath>packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="System.Configuration" />
|
||||||
|
<Reference Include="System.Configuration.ConfigurationManager, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\System.Configuration.ConfigurationManager.6.0.0\lib\net461\System.Configuration.ConfigurationManager.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
|
<Reference Include="System.Data.OracleClient" />
|
||||||
|
<Reference Include="System.Net" />
|
||||||
|
<Reference Include="System.Runtime.Caching" />
|
||||||
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
<HintPath>packages\System.Runtime.CompilerServices.Unsafe.5.0.0\lib\net45\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
|
<HintPath>packages\System.Runtime.CompilerServices.Unsafe.5.0.0\lib\net45\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="System.Security" />
|
||||||
|
<Reference Include="System.Security.AccessControl, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\System.Security.AccessControl.6.0.0\lib\net461\System.Security.AccessControl.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.Security.Permissions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\System.Security.Permissions.6.0.0\lib\net461\System.Security.Permissions.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.Security.Principal.Windows, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
|
<HintPath>packages\System.Security.Principal.Windows.5.0.0\lib\net461\System.Security.Principal.Windows.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.ServiceProcess" />
|
||||||
<Reference Include="System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
<Reference Include="System.Text.Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
<HintPath>packages\System.Text.Encoding.CodePages.5.0.0\lib\net461\System.Text.Encoding.CodePages.dll</HintPath>
|
<HintPath>packages\System.Text.Encoding.CodePages.5.0.0\lib\net461\System.Text.Encoding.CodePages.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="System.Transactions" />
|
||||||
<Reference Include="System.Xml.Linq" />
|
<Reference Include="System.Xml.Linq" />
|
||||||
<Reference Include="System.Data.DataSetExtensions" />
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
<Reference Include="Microsoft.CSharp" />
|
<Reference Include="Microsoft.CSharp" />
|
||||||
<Reference Include="System.Data" />
|
<Reference Include="System.Data" />
|
||||||
<Reference Include="System.Net.Http" />
|
<Reference Include="System.Net.Http" />
|
||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
|
<Reference Include="WindowsBase" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="Assistant\IProduct.cs" />
|
||||||
|
<Compile Include="Assistant\Product.cs" />
|
||||||
<Compile Include="Functions.cs" />
|
<Compile Include="Functions.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Assistant\SkuAssist.cs" />
|
<Compile Include="Assistant\SkuAssist.cs" />
|
||||||
|
@ -4,6 +4,11 @@
|
|||||||
<package id="ExcelDna.AddIn" version="1.5.0" targetFramework="net48" />
|
<package id="ExcelDna.AddIn" version="1.5.0" targetFramework="net48" />
|
||||||
<package id="ExcelDna.Integration" version="1.5.0" targetFramework="net48" />
|
<package id="ExcelDna.Integration" version="1.5.0" targetFramework="net48" />
|
||||||
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
|
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
|
||||||
|
<package id="System.Configuration.ConfigurationManager" version="6.0.0" targetFramework="net48" />
|
||||||
|
<package id="System.Runtime.Caching" version="6.0.0" targetFramework="net48" />
|
||||||
<package id="System.Runtime.CompilerServices.Unsafe" version="5.0.0" targetFramework="net48" />
|
<package id="System.Runtime.CompilerServices.Unsafe" version="5.0.0" targetFramework="net48" />
|
||||||
|
<package id="System.Security.AccessControl" version="6.0.0" targetFramework="net48" />
|
||||||
|
<package id="System.Security.Permissions" version="6.0.0" targetFramework="net48" />
|
||||||
|
<package id="System.Security.Principal.Windows" version="5.0.0" targetFramework="net48" />
|
||||||
<package id="System.Text.Encoding.CodePages" version="5.0.0" targetFramework="net48" />
|
<package id="System.Text.Encoding.CodePages" version="5.0.0" targetFramework="net48" />
|
||||||
</packages>
|
</packages>
|
Loading…
Reference in New Issue
Block a user