Edit ASync method. Delete unnecessary classes

This commit is contained in:
Sergey Chebotar 2021-11-14 12:27:49 +03:00
parent 2290f1b340
commit f5234e956c
6 changed files with 9 additions and 217 deletions

View File

@ -8,25 +8,14 @@ namespace Rehau.Sku.Assist
{
static class SkuAssist
{
static private HttpClient _httpClient;
public static void EnsureHttpInitialized()
public async static Task<AngleSharp.Dom.IDocument> GetDocumentAsync(string request, HttpClient httpClient)
{
if (_httpClient == null)
{
_httpClient = new HttpClient();
}
}
public async static Task<AngleSharp.Dom.IDocument> GetDocumentAsync(string request)
{
string url = "https://shop-rehau.ru/catalogsearch/result/?q=" + request;
string uri = "https://shop-rehau.ru/catalogsearch/result/?q=" + request;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
HttpResponseMessage response = await _httpClient.GetAsync(url);
HttpResponseMessage response = await httpClient.GetAsync(uri);
response.EnsureSuccessStatusCode();
IConfiguration config = Configuration.Default;
IBrowsingContext context = BrowsingContext.New(config);

View File

@ -1,38 +0,0 @@
using System;
using System.Threading;
namespace Rehau.Sku.Assist
{
sealed class CancellationDisposable : IDisposable
{
readonly CancellationTokenSource cts;
public CancellationDisposable(CancellationTokenSource cts)
{
if (cts == null)
{
throw new ArgumentNullException("cts");
}
this.cts = cts;
}
public CancellationDisposable()
: this(new CancellationTokenSource())
{
}
public CancellationToken Token
{
get { return cts.Token; }
}
public void Dispose()
{
cts.Cancel();
}
}
}

View File

@ -1,21 +0,0 @@
using System;
namespace Rehau.Sku.Assist
{
sealed class DefaultDisposable : IDisposable
{
public static readonly DefaultDisposable Instance = new DefaultDisposable();
DefaultDisposable()
{
}
public void Dispose()
{
}
}
}

View File

@ -1,133 +0,0 @@
using ExcelDna.Integration;
using System.Threading.Tasks;
using System;
using System.Threading;
namespace Rehau.Sku.Assist
{
internal static class ExcelTaskUtil
{
public static object Run<TResult>(string callerFunctionName, object callerParameters, Func<CancellationToken, Task<TResult>> taskSource)
{
return ExcelAsyncUtil.Observe(callerFunctionName, callerParameters, delegate
{
var cts = new CancellationTokenSource();
var task = taskSource(cts.Token);
return new ExcelTaskObservable<TResult>(task, cts);
});
}
public static object Run<TResult>(string callerFunctionName, object callerParameters, Func<Task<TResult>> taskSource)
{
return ExcelAsyncUtil.Observe(callerFunctionName, callerParameters, delegate
{
var task = taskSource();
return new ExcelTaskObservable<TResult>(task);
});
}
class ExcelTaskObservable<TResult> : IExcelObservable
{
readonly Task<TResult> _task;
readonly CancellationTokenSource _cts;
public ExcelTaskObservable(Task<TResult> task)
{
_task = task;
}
public ExcelTaskObservable(Task<TResult> task, CancellationTokenSource cts)
: this(task)
{
_cts = cts;
}
public IDisposable Subscribe(IExcelObserver observer)
{
switch (_task.Status)
{
case TaskStatus.RanToCompletion:
observer.OnNext(_task.Result);
observer.OnCompleted();
break;
case TaskStatus.Faulted:
observer.OnError(_task.Exception.InnerException);
break;
case TaskStatus.Canceled:
observer.OnError(new TaskCanceledException(_task));
break;
default:
_task.ContinueWith(t =>
{
switch (t.Status)
{
case TaskStatus.RanToCompletion:
observer.OnNext(t.Result);
observer.OnCompleted();
break;
case TaskStatus.Faulted:
observer.OnError(t.Exception.InnerException);
break;
case TaskStatus.Canceled:
observer.OnError(new TaskCanceledException(t));
break;
}
});
break;
}
if (_cts != null)
{
return new CancellationDisposable(_cts);
}
return DefaultDisposable.Instance;
}
}
sealed class DefaultDisposable : IDisposable
{
public static readonly DefaultDisposable Instance = new DefaultDisposable();
DefaultDisposable()
{
}
public void Dispose()
{
}
}
sealed class CancellationDisposable : IDisposable
{
readonly CancellationTokenSource cts;
public CancellationDisposable(CancellationTokenSource cts)
{
if (cts == null)
{
throw new ArgumentNullException("cts");
}
this.cts = cts;
}
public CancellationDisposable()
: this(new CancellationTokenSource())
{
}
public CancellationToken Token
{
get { return cts.Token; }
}
public void Dispose()
{
cts.Cancel();
}
}
}
}

View File

@ -1,25 +1,23 @@
using ExcelDna.Integration;
using System.Net.Http;
namespace Rehau.Sku.Assist
{
public class Functions : IExcelAddIn
{
[ExcelFunction(description: "Получение наименования и артикула позиции")]
static readonly HttpClient httpClient = new HttpClient();
public static object RAUNAME(string request)
{
SkuAssist.EnsureHttpInitialized();
return ExcelTaskUtil.Run("RAUNAME ASYNC", request, async token =>
return ExcelAsyncUtil.Run("RAUNAME", request, delegate
{
var document = await SkuAssist.GetDocumentAsync(request);
var document = SkuAssist.GetDocumentAsync(request, httpClient).Result;
return SkuAssist.GetResultFromDocument(document);
});
}
public void AutoClose()
{
ExcelIntegration.RegisterUnhandledExceptionHandler(
delegate (object ex) { return string.Format("!!!ERROR: {0}", ex.ToString()); });
}
public void AutoOpen()

View File

@ -58,12 +58,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CancellationDisposable.cs" />
<Compile Include="DefaultDisposable.cs" />
<Compile Include="ExcelTaskUtil.cs" />
<Compile Include="Functions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SkuAssist.cs" />
<Compile Include="Assistant\SkuAssist.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Rehau.Sku.Assist-AddIn.dna" />