RhSolutions-AddIn/Source/Assistant/HttpClientUtil.cs
2021-12-05 15:52:11 +03:00

75 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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(".", " ")
.Replace("Ø", " ")
.ToString();
}
}
}