From a3e7e2b902c136141ae57792bb76306dd9222868 Mon Sep 17 00:00:00 2001 From: worldofsites Date: Fri, 18 Sep 2020 17:17:43 +0700 Subject: [PATCH] add cancellation token parameter to async methods to be able to cancel api calls --- src/DiadocApi.Async.cs | 662 +++++++++--------- src/DiadocHttpApi.Async.cs | 23 +- src/DiadocHttpApi.AuthAsync.cs | 62 +- src/DiadocHttpApi.CloudCryptAsync.cs | 33 +- src/DiadocHttpApi.CounteragentsAsync.cs | 26 +- src/DiadocHttpApi.Departments.Async.cs | 21 +- ...docHttpApi.DetectCustomPrintForms.Async.cs | 6 +- src/DiadocHttpApi.DocflowAsync.cs | 35 +- src/DiadocHttpApi.DocumentsAsync.cs | 70 +- src/DiadocHttpApi.DraftsAsync.cs | 11 +- src/DiadocHttpApi.Employees.Async.cs | 35 +- src/DiadocHttpApi.EventsAsync.cs | 58 +- src/DiadocHttpApi.ExternalServicesAsync.cs | 7 +- src/DiadocHttpApi.ForwardingAsync.cs | 27 +- src/DiadocHttpApi.InvoicingAsync.cs | 134 ++-- src/DiadocHttpApi.KeyValueStorageAsync.cs | 12 +- src/DiadocHttpApi.Organizations.Async.cs | 5 +- src/DiadocHttpApi.ParseAsync.cs | 58 +- src/DiadocHttpApi.PrintAsync.cs | 29 +- src/DiadocHttpApi.ReceiptsAsync.cs | 9 +- src/DiadocHttpApi.RecognizeAsync.cs | 13 +- src/DiadocHttpApi.ReferencesAsync.cs | 73 +- src/DiadocHttpApi.RegistrationAsync.cs | 13 +- src/DiadocHttpApi.ShelfAsync.cs | 17 +- ...cHttpApi.UniversalTransferDocumentAsync.cs | 22 +- src/DiadocHttpApi.WaitTaskResultAsync.cs | 6 +- src/DiadocHttpApi.ZipAsync.cs | 7 +- src/DocflowApi.Async.cs | 17 +- src/Http/HttpClient.cs | 7 +- src/IDiadocApi.cs | 332 ++++----- src/IDocflowApi.cs | 9 +- src/WebRequestExtensions.cs | 41 ++ tests/DiadocApi-Tests.csproj | 17 + tests/TaskAsyncronousCancel_Test.cs | 71 ++ tests/TaskAsyncronousPattern_Test.cs | 10 +- 35 files changed, 1085 insertions(+), 893 deletions(-) create mode 100644 src/WebRequestExtensions.cs create mode 100644 tests/TaskAsyncronousCancel_Test.cs diff --git a/src/DiadocApi.Async.cs b/src/DiadocApi.Async.cs index 982f877b..00bc6d91 100644 --- a/src/DiadocApi.Async.cs +++ b/src/DiadocApi.Async.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Diadoc.Api.Proto; using Diadoc.Api.Proto.Docflow; @@ -27,423 +28,425 @@ namespace Diadoc.Api { public partial class DiadocApi { - public Task AuthenticateAsync(string login, string password, string key = null, string id = null) + public Task AuthenticateAsync(string login, string password, string key = null, string id = null, CancellationToken ct = default) { - return diadocHttpApi.AuthenticateAsync(login, password, key, id); + return diadocHttpApi.AuthenticateAsync(login, password, key, id, ct: ct); } - public Task AuthenticateByKeyAsync([NotNull] string key, [NotNull] string id) + public Task AuthenticateByKeyAsync([NotNull] string key, [NotNull] string id, CancellationToken ct = default) { if (key == null) throw new ArgumentNullException("key"); if (id == null) throw new ArgumentNullException("id"); - return diadocHttpApi.AuthenticateByKeyAsync(key, id); + return diadocHttpApi.AuthenticateByKeyAsync(key, id, ct: ct); } - public Task AuthenticateBySidAsync([NotNull] string sid) + public Task AuthenticateBySidAsync([NotNull] string sid, CancellationToken ct = default) { if (sid == null) throw new ArgumentNullException("sid"); - return diadocHttpApi.AuthenticateBySidAsync(sid); + return diadocHttpApi.AuthenticateBySidAsync(sid, ct: ct); } - public Task AuthenticateAsync(byte[] certificateBytes, bool useLocalSystemStorage = false) + public Task AuthenticateAsync(byte[] certificateBytes, bool useLocalSystemStorage = false, CancellationToken ct = default) { if (certificateBytes == null) throw new ArgumentNullException("certificateBytes"); - return diadocHttpApi.AuthenticateAsync(certificateBytes, useLocalSystemStorage); + return diadocHttpApi.AuthenticateAsync(certificateBytes, useLocalSystemStorage, ct: ct); } - public Task AuthenticateAsync(string thumbprint, bool useLocalSystemStorage = false) + public Task AuthenticateAsync(string thumbprint, bool useLocalSystemStorage = false, CancellationToken ct = default) { if (thumbprint == null) throw new ArgumentNullException("thumbprint"); - return diadocHttpApi.AuthenticateAsync(thumbprint, useLocalSystemStorage); + return diadocHttpApi.AuthenticateAsync(thumbprint, useLocalSystemStorage, ct: ct); } - public Task AuthenticateWithKeyAsync(string thumbprint, bool useLocalSystemStorage = false, string key = null, string id = null, bool autoConfirm = true) + public Task AuthenticateWithKeyAsync(string thumbprint, bool useLocalSystemStorage = false, string key = null, string id = null, bool autoConfirm = true, CancellationToken ct = default) { if (thumbprint == null) throw new ArgumentNullException("thumbprint"); - return diadocHttpApi.AuthenticateWithKeyAsync(thumbprint, useLocalSystemStorage, key, id, autoConfirm); + return diadocHttpApi.AuthenticateWithKeyAsync(thumbprint, useLocalSystemStorage, key, id, autoConfirm, ct: ct); } - public Task AuthenticateWithKeyAsync(byte[] certificateBytes, bool useLocalSystemStorage = false, string key = null, string id = null, bool autoConfirm = true) + public Task AuthenticateWithKeyAsync(byte[] certificateBytes, bool useLocalSystemStorage = false, string key = null, string id = null, bool autoConfirm = true, CancellationToken ct = default) { if (certificateBytes == null) throw new ArgumentNullException("certificateBytes"); - return diadocHttpApi.AuthenticateWithKeyAsync(certificateBytes, useLocalSystemStorage, key, id, autoConfirm); + return diadocHttpApi.AuthenticateWithKeyAsync(certificateBytes, useLocalSystemStorage, key, id, autoConfirm, ct: ct); } - public Task AuthenticateWithKeyConfirmAsync(byte[] certificateBytes, string token, bool saveBinding = false) + public Task AuthenticateWithKeyConfirmAsync(byte[] certificateBytes, string token, bool saveBinding = false, CancellationToken ct = default) { if (certificateBytes == null) throw new ArgumentNullException("certificateBytes"); if (string.IsNullOrEmpty(token)) throw new ArgumentNullException("token"); - return diadocHttpApi.AuthenticateWithKeyConfirmAsync(certificateBytes, token, saveBinding); + return diadocHttpApi.AuthenticateWithKeyConfirmAsync(certificateBytes, token, saveBinding, ct: ct); } - public Task AuthenticateWithKeyConfirmAsync(string thumbprint, string token, bool saveBinding = false) + public Task AuthenticateWithKeyConfirmAsync(string thumbprint, string token, bool saveBinding = false, CancellationToken ct = default) { if (thumbprint == null) throw new ArgumentNullException("thumbprint"); if (string.IsNullOrEmpty(token)) throw new ArgumentNullException("token"); - return diadocHttpApi.AuthenticateWithKeyConfirmAsync(thumbprint, token, saveBinding); + return diadocHttpApi.AuthenticateWithKeyConfirmAsync(thumbprint, token, saveBinding, ct: ct); } - public Task GetMyPermissionsAsync(string authToken, string orgId) + public Task GetMyPermissionsAsync(string authToken, string orgId, CancellationToken ct = default) { if (authToken == null) throw new ArgumentNullException("authToken"); if (orgId == null) throw new ArgumentNullException("orgId"); - return diadocHttpApi.GetMyPermissionsAsync(authToken, orgId); + return diadocHttpApi.GetMyPermissionsAsync(authToken, orgId, ct: ct); } - public Task GetMyOrganizationsAsync(string authToken, bool autoRegister = true) + public Task GetMyOrganizationsAsync(string authToken, bool autoRegister = true, CancellationToken ct = default) { if (authToken == null) throw new ArgumentNullException("authToken"); - return diadocHttpApi.GetMyOrganizationsAsync(authToken, autoRegister); + return diadocHttpApi.GetMyOrganizationsAsync(authToken, autoRegister, ct: ct); } - public Task GetMyUserAsync(string authToken) + public Task GetMyUserAsync(string authToken, CancellationToken ct = default) { if (authToken == null) throw new ArgumentNullException("authToken"); - return diadocHttpApi.GetMyUserAsync(authToken); + return diadocHttpApi.GetMyUserAsync(authToken, ct: ct); } - public Task GetMyUserV2Async(string authToken) + public Task GetMyUserV2Async(string authToken, CancellationToken ct = default) { if (authToken == null) throw new ArgumentNullException("authToken"); - return diadocHttpApi.GetMyUserV2Async(authToken); + return diadocHttpApi.GetMyUserV2Async(authToken, ct: ct); } - public Task UpdateMyUserAsync(string authToken, UserToUpdate userToUpdate) + public Task UpdateMyUserAsync(string authToken, UserToUpdate userToUpdate, CancellationToken ct = default) { if (authToken == null) throw new ArgumentNullException("authToken"); if (userToUpdate == null) throw new ArgumentNullException("userToUpdate"); - return diadocHttpApi.UpdateMyUserAsync(authToken, userToUpdate); + return diadocHttpApi.UpdateMyUserAsync(authToken, userToUpdate, ct: ct); } - public Task GetMyCertificatesAsync(string authToken, string boxId) + public Task GetMyCertificatesAsync(string authToken, string boxId, CancellationToken ct = default) { if (authToken == null) throw new ArgumentNullException("authToken"); if (boxId == null) throw new ArgumentNullException("boxId"); - return diadocHttpApi.GetMyCertificatesAsync(authToken, boxId); + return diadocHttpApi.GetMyCertificatesAsync(authToken, boxId, ct: ct); } - public Task GetOrganizationsByInnKppAsync(string inn, string kpp, bool includeRelations = false) + public Task GetOrganizationsByInnKppAsync(string inn, string kpp, bool includeRelations = false, CancellationToken ct = default) { if (inn == null) throw new ArgumentNullException("inn"); - return diadocHttpApi.GetOrganizationsByInnKppAsync(inn, kpp, includeRelations); + return diadocHttpApi.GetOrganizationsByInnKppAsync(inn, kpp, includeRelations, ct: ct); } - public Task GetOrganizationByIdAsync(string orgId) + public Task GetOrganizationByIdAsync(string orgId, CancellationToken ct = default) { if (orgId == null) throw new ArgumentNullException("orgId"); - return diadocHttpApi.GetOrganizationByIdAsync(orgId); + return diadocHttpApi.GetOrganizationByIdAsync(orgId, ct: ct); } - public Task GetOrganizationByBoxIdAsync(string boxId) + public Task GetOrganizationByBoxIdAsync(string boxId, CancellationToken ct = default) { if (boxId == null) throw new ArgumentNullException("boxId"); - return diadocHttpApi.GetOrganizationByBoxIdAsync(boxId); + return diadocHttpApi.GetOrganizationByBoxIdAsync(boxId, ct: ct); } - public Task GetOrganizationByFnsParticipantIdAsync(string fnsParticipantId) + public Task GetOrganizationByFnsParticipantIdAsync(string fnsParticipantId, CancellationToken ct = default) { if (fnsParticipantId == null) throw new ArgumentException("fnsParticipantId"); - return diadocHttpApi.GetOrganizationByFnsParticipantIdAsync(fnsParticipantId); + return diadocHttpApi.GetOrganizationByFnsParticipantIdAsync(fnsParticipantId, ct: ct); } - public Task GetOrganizationByInnKppAsync(string inn, string kpp) + public Task GetOrganizationByInnKppAsync(string inn, string kpp, CancellationToken ct = default) { if (inn == null) throw new ArgumentException("inn"); - return diadocHttpApi.GetOrganizationByInnKppAsync(inn, kpp); + return diadocHttpApi.GetOrganizationByInnKppAsync(inn, kpp, ct: ct); } - public Task GetBoxAsync(string boxId) + public Task GetBoxAsync(string boxId, CancellationToken ct = default) { if (boxId == null) throw new ArgumentNullException("boxId"); - return diadocHttpApi.GetBoxAsync(boxId); + return diadocHttpApi.GetBoxAsync(boxId, ct: ct); } - public Task GetDepartmentAsync(string authToken, string orgId, string departmentId) + public Task GetDepartmentAsync(string authToken, string orgId, string departmentId, CancellationToken ct = default) { if (authToken == null) throw new ArgumentNullException("authToken"); if (orgId == null) throw new ArgumentNullException("orgId"); if (departmentId == null) throw new ArgumentNullException("departmentId"); - return diadocHttpApi.GetDepartmentAsync(authToken, orgId, departmentId); + return diadocHttpApi.GetDepartmentAsync(authToken, orgId, departmentId, ct: ct); } - public Task UpdateOrganizationPropertiesAsync(string authToken, OrganizationPropertiesToUpdate orgProps) + public Task UpdateOrganizationPropertiesAsync(string authToken, OrganizationPropertiesToUpdate orgProps, CancellationToken ct = default) { if (authToken == null) throw new ArgumentNullException("authToken"); if (orgProps == null) throw new ArgumentNullException("orgProps"); - return diadocHttpApi.UpdateOrganizationPropertiesAsync(authToken, orgProps); + return diadocHttpApi.UpdateOrganizationPropertiesAsync(authToken, orgProps, ct: ct); } - public Task GetOrganizationFeaturesAsync(string authToken, string boxId) + public Task GetOrganizationFeaturesAsync(string authToken, string boxId, CancellationToken ct = default) { if (authToken == null) throw new ArgumentNullException("authToken"); if (boxId == null) throw new ArgumentNullException("boxId"); - return diadocHttpApi.GetOrganizationFeaturesAsync(authToken, boxId); + return diadocHttpApi.GetOrganizationFeaturesAsync(authToken, boxId, ct: ct); } - public Task GetNewEventsAsync(string authToken, string boxId, string afterEventId = null) + public Task GetNewEventsAsync(string authToken, string boxId, string afterEventId = null, CancellationToken ct = default) { if (authToken == null) throw new ArgumentNullException("authToken"); if (boxId == null) throw new ArgumentNullException("boxId"); - return diadocHttpApi.GetNewEventsAsync(authToken, boxId, afterEventId); + return diadocHttpApi.GetNewEventsAsync(authToken, boxId, afterEventId, ct: ct); } - public Task GetEventAsync(string authToken, string boxId, string eventId) + public Task GetEventAsync(string authToken, string boxId, string eventId, CancellationToken ct = default) { if (authToken == null) throw new ArgumentNullException("authToken"); if (boxId == null) throw new ArgumentNullException("boxId"); if (eventId == null) throw new ArgumentNullException("eventId"); - return diadocHttpApi.GetEventAsync(authToken, boxId, eventId); + return diadocHttpApi.GetEventAsync(authToken, boxId, eventId, ct: ct); } - public Task PostMessageAsync(string authToken, MessageToPost msg, string operationId = null) + public Task PostMessageAsync(string authToken, MessageToPost msg, string operationId = null, CancellationToken ct = default) { if (authToken == null) throw new ArgumentNullException("authToken"); if (msg == null) throw new ArgumentNullException("msg"); - return diadocHttpApi.PostMessageAsync(authToken, msg, operationId); + return diadocHttpApi.PostMessageAsync(authToken, msg, operationId, ct: ct); } - public Task