aboutsummaryrefslogtreecommitdiff
path: root/Source/Assistant/HttpClientUtil.cs
blob: f9c144b6817fe8ee3a684bf5e7604baad77bfd4e (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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();
        }
    }
}