Add Caching and replace HttpClientUtil class to external file

This commit is contained in:
Sergey Chebotar 2021-12-03 12:57:22 +03:00
parent 3ea18ae25e
commit b5c7135070
9 changed files with 169 additions and 81 deletions

Binary file not shown.

View File

@ -85,6 +85,7 @@
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Source\Assistant\HttpClientUtil.cs" />
<Compile Include="Source\ExcelDNA\AddIn.cs" /> <Compile Include="Source\ExcelDNA\AddIn.cs" />
<Compile Include="Source\Assistant\IProduct.cs" /> <Compile Include="Source\Assistant\IProduct.cs" />
<Compile Include="Source\Assistant\Product.cs" /> <Compile Include="Source\Assistant\Product.cs" />

View File

@ -0,0 +1,74 @@
using AngleSharp;
using AngleSharp.Dom;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Text;
namespace Rehau.Sku.Assist
{
static class HttpClientUtil
{
private static HttpClient _httpClient = AddIn.httpClient;
public async static Task<string> GetContentByUriAsync(Uri uri)
{
ServicePointManager.SecurityProtocol =
SecurityProtocolType.Tls12 |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls;
return await _httpClient.GetStringAsync(uri);
}
public async static Task<IDocument> ContentToDocAsync(Task<string> content)
{
IConfiguration config = Configuration.Default;
IBrowsingContext context = BrowsingContext.New(config);
return await context.OpenAsync(req => req.Content(content.Result));
}
public static Uri ConvertToUri(this string request, ResponseOrder order)
{
UriBuilder baseUri = new UriBuilder("https", "shop-rehau.ru");
baseUri.Path = "/catalogsearch/result/index/";
string cleanedRequest = request._CleanRequest();
switch (order)
{
case ResponseOrder.Relevance:
baseUri.Query = "dir=asc&order=relevance&q=" + cleanedRequest;
break;
case ResponseOrder.Name:
baseUri.Query = "dir=asc&order=name&q=" + cleanedRequest;
break;
case ResponseOrder.Price:
baseUri.Query = "dir=asc&order=price&q=" + cleanedRequest;
break;
case ResponseOrder.Series:
baseUri.Query = "dir=asc&order=sch_product_series&q=" + cleanedRequest;
break;
case ResponseOrder.NoSettings:
baseUri.Query = "q=" + cleanedRequest;
break;
default:
throw new ArgumentException();
}
return baseUri.Uri;
}
private static string _CleanRequest(this string input)
{
return new StringBuilder(input)
.Replace("+", " plus ")
.Replace("РХ", "")
.Replace("º", " ")
.Replace(".", " ")
.ToString();
}
}
}

View File

@ -1,9 +1,11 @@
namespace Rehau.Sku.Assist using System;
namespace Rehau.Sku.Assist
{ {
interface IProduct interface IProduct
{ {
string Sku { get; } string Sku { get; }
string Name { get; } string Name { get; }
string Uri { get; } Uri Uri { get; }
} }
} }

View File

@ -1,11 +1,12 @@
namespace Rehau.Sku.Assist using System;
namespace Rehau.Sku.Assist
{ {
public class Product : IProduct public class Product : IProduct
{ {
public string Sku { get; } public string Sku { get; }
public string Name { get; } public string Name { get; }
public Uri Uri { get; }
public string Uri => throw new System.NotImplementedException();
public Product(string sku, string name) public Product(string sku, string name)
{ {
@ -13,6 +14,13 @@
Name = name; Name = name;
} }
public Product(string sku, string name, string uri)
{
Sku = sku;
Name = name;
Uri = new Uri(uri);
}
public override string ToString() public override string ToString()
{ {
return $"{this.Name} ({this.Sku})"; return $"{this.Name} ({this.Sku})";

View File

@ -1,17 +1,13 @@
using AngleSharp; using AngleSharp.Dom;
using AngleSharp.Dom; using AngleSharp.Html.Dom;
using System; using System;
using System.Linq; using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace Rehau.Sku.Assist namespace Rehau.Sku.Assist
{ {
static class SkuAssist public enum ResponseOrder
{
private static HttpClient _httpClient;
private enum ResponseOrder
{ {
NoSettings, NoSettings,
Relevance, Relevance,
@ -19,62 +15,48 @@ namespace Rehau.Sku.Assist
Price, Price,
Series Series
} }
private static void _EnsureHttpClientRunning()
static class SkuAssist
{ {
if (_httpClient == null) public static async Task<IProduct> GetProduct(string request)
_httpClient = new HttpClient(); {
Uri uri = request.ConvertToUri(ResponseOrder.NoSettings);
Task<string> contentTask = Task.Run(() => HttpClientUtil.GetContentByUriAsync(uri));
Task<IDocument> documentTask = await contentTask.ContinueWith(content => HttpClientUtil.ContentToDocAsync(content));
IProduct product = await documentTask.ContinueWith(doc => SkuAssist.GetFirstProduct(doc.Result));
return product;
} }
public async static Task<string> GetContent(string request) public static IProduct GetFirstProduct(IDocument doc)
{ {
Uri uri = _ConvertToUri(request, ResponseOrder.NoSettings); return doc
_EnsureHttpClientRunning();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
return await _httpClient.GetStringAsync(uri);
}
public async static Task<IDocument> GetDocument(Task<string> source)
{
IConfiguration config = Configuration.Default;
IBrowsingContext context = BrowsingContext.New(config);
return await context.OpenAsync(req => req.Content(source.Result));
}
public static IProduct GetProductFromDocument(IDocument document)
{
return document
.All .All
.Where(e => e.ClassName == "product-item__desc-top") .Where(e => e.ClassName == "product-item__desc-top")
.Select(e => new Product(e.Children[0].TextContent, e.Children[1].TextContent.Trim(new[] { '\n', ' ' }))) .Where(e => Regex.IsMatch(e.Children[0].TextContent, @"\d{11}", RegexOptions.None))
.Select(e =>
new Product(e.Children[0].TextContent,
e.Children[1].TextContent.Trim(new[] { '\n', ' ' })))
.FirstOrDefault(); .FirstOrDefault();
} }
private static Uri _ConvertToUri(this string request, ResponseOrder order) public static Uri GetFirstResultLink(IDocument doc)
{ {
string cleanedRequest = request._CleanRequest(); var link = new Uri(doc
switch (order) .Links
{ .Where(e => e.ClassName == "product-item__title-link js-name")
case ResponseOrder.Relevance: .Select(l => ((IHtmlAnchorElement)l).Href)
return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=relevance&q=" + cleanedRequest); .FirstOrDefault());
case ResponseOrder.Name: return link;
return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=name&q=" + cleanedRequest);
case ResponseOrder.Price:
return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=price&q=" + cleanedRequest);
case ResponseOrder.Series:
return new Uri("https://shop-rehau.ru/catalogsearch/result/index/?dir=asc&order=sch_product_series&q=" + cleanedRequest);
case ResponseOrder.NoSettings:
return new Uri("https://shop-rehau.ru/catalogsearch/result/?q=" + cleanedRequest);
default:
throw new ArgumentException();
}
}
private static string _CleanRequest(this string input)
{
return input.Replace("+", " plus ");
}
}
} }
public static string GetFistResultImageLink(IDocument doc)
{
var imageSource = doc.Images
.Where(x => x.ClassName == "product-item__image")
.FirstOrDefault();
return imageSource != null ? imageSource.Source : "Нет ссылки";
}
}
}

View File

@ -1,13 +1,17 @@
using ExcelDna.Integration; using ExcelDna.Integration;
using ExcelDna.Registration; using ExcelDna.Registration;
using System.Net.Http;
namespace Rehau.Sku.Assist namespace Rehau.Sku.Assist
{ {
public class AddIn : IExcelAddIn public class AddIn : IExcelAddIn
{ {
public static HttpClient httpClient;
public void AutoOpen() public void AutoOpen()
{ {
RegisterFunctions(); RegisterFunctions();
httpClient = new HttpClient();
} }
public void AutoClose() public void AutoClose()

View File

@ -1,19 +1,36 @@
using AngleSharp.Dom; using ExcelDna.Integration;
using ExcelDna.Integration;
using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Runtime.Caching;
namespace Rehau.Sku.Assist namespace Rehau.Sku.Assist
{ {
public class Functions public class Functions
{ {
[ExcelFunction] [ExcelFunction]
public static async Task<string> RAUNAME(string request) public static object RAUNAME(string request)
{ {
Task<string> contentTask = Task.Run(() => SkuAssist.GetContent(request)); if (MemoryCache.Default.Contains(request))
Task<IDocument> documentTask = await contentTask.ContinueWith(content => SkuAssist.GetDocument(content)); return MemoryCache.Default[request].ToString();
IProduct product = await documentTask.ContinueWith(doc => SkuAssist.GetProductFromDocument(doc.Result));
return product != null ? product.ToString() : "Не найдено"; else
{
object result = ExcelAsyncUtil.Run("Rauname", new[] { request },
delegate
{
Task<IProduct> product = Task.Run(() => SkuAssist.GetProduct(request));
return product.Result;
});
if (Equals(result, ExcelError.ExcelErrorNA))
{
return "Загрузка...";
}
else
{
MemoryCache.Default.Add(request, result, System.DateTime.Now.AddMinutes(10));
return result == null ? "Не найдено" : result.ToString();
}
}
} }
} }
} }

View File

@ -2,14 +2,14 @@
namespace Rehau.Sku.Assist.Tests namespace Rehau.Sku.Assist.Tests
{ {
[TestFixture] //[TestFixture]
public class SkuAssistTests //public class SkuAssistTests
{ //{
[Test] // [Test]
public async void BaseTest() // public async void BaseTest()
{ // {
var result = await Functions.RAUNAME("160001"); // var result = await Functions.RAUNAME("160001");
Assert.AreEqual("Надвижная гильза REHAU RAUTITAN РХ (11600011001)", result); // Assert.AreEqual("Надвижная гильза REHAU RAUTITAN РХ (11600011001)", result);
} // }
} //}
} }