From b3e90c7a7fafc5c692b05b4e91cbd8a490deca29 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Fri, 29 Jul 2016 16:50:35 +0100 Subject: [PATCH] Async async async improvements. --- PlexRequests.Api/RetryHandler.cs | 12 +- .../NotificationMessageResolverTests.cs | 5 +- .../JsonIssuesModelRequestService.cs | 10 +- .../JsonRequestModelRequestService.cs | 22 +- .../{ => Notification}/NotificationMessage.cs | 2 +- .../NotificationMessageContent.cs | 2 +- .../NotificationMessageCurlys.cs | 3 +- .../NotificationMessageResolver.cs | 72 ++- PlexRequests.Core/PlexRequests.Core.csproj | 9 +- PlexRequests.Core/Properties/AssemblyInfo.cs | 74 +-- .../SettingModels/NotificationSettings.cs | 13 +- PlexRequests.Core/SettingsServiceV2.cs | 6 +- PlexRequests.Core/StatusChecker.cs | 164 +++---- PlexRequests.Helpers/CookieHelper.cs | 2 +- .../Repository/BaseGenericRepository.cs | 13 +- .../Repository/GenericRepository.cs | 180 ++++--- .../Repository/RequestJsonRepository.cs | 459 +++++++++--------- .../Repository/SettingsJsonRepository.cs | 334 ++++++------- PlexRequests.Store/UserRepository.cs | 158 +++--- 19 files changed, 785 insertions(+), 755 deletions(-) rename PlexRequests.Core/{ => Notification}/NotificationMessage.cs (97%) rename PlexRequests.Core/{ => Notification}/NotificationMessageContent.cs (97%) rename PlexRequests.Core/{ => Notification}/NotificationMessageCurlys.cs (98%) rename PlexRequests.Core/{ => Notification}/NotificationMessageResolver.cs (67%) diff --git a/PlexRequests.Api/RetryHandler.cs b/PlexRequests.Api/RetryHandler.cs index 898c2370c..d3ffb1912 100644 --- a/PlexRequests.Api/RetryHandler.cs +++ b/PlexRequests.Api/RetryHandler.cs @@ -33,13 +33,13 @@ namespace PlexRequests.Api { public static class RetryHandler { - private static readonly TimeSpan[] DefaultTime = { TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10) }; + private static readonly TimeSpan[] DefaultRetryTime = { TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10) }; public static T Execute(Func action, TimeSpan[] timeSpan = null) { if (timeSpan == null) { - timeSpan = DefaultTime; + timeSpan = DefaultRetryTime; } var policy = RetryAndWaitPolicy(timeSpan); @@ -50,7 +50,7 @@ namespace PlexRequests.Api { if (timeSpan == null) { - timeSpan = DefaultTime; + timeSpan = DefaultRetryTime; } var policy = RetryAndWaitPolicy(action, timeSpan); @@ -61,7 +61,7 @@ namespace PlexRequests.Api { if (timeSpan == null) { - timeSpan = DefaultTime; + timeSpan = DefaultRetryTime; } var policy = Policy.Handle().WaitAndRetry(timeSpan, (e, ts) => action()); @@ -72,7 +72,7 @@ namespace PlexRequests.Api { if (timeSpan == null) { - timeSpan = DefaultTime; + timeSpan = DefaultRetryTime; } var policy = Policy.Handle().WaitAndRetry(timeSpan); @@ -83,7 +83,7 @@ namespace PlexRequests.Api { if (timeSpan == null) { - timeSpan = DefaultTime; + timeSpan = DefaultRetryTime; } var policy = Policy.Handle().WaitAndRetry(timeSpan, action); diff --git a/PlexRequests.Core.Tests/NotificationMessageResolverTests.cs b/PlexRequests.Core.Tests/NotificationMessageResolverTests.cs index d4ccd4fed..58cfb06e5 100644 --- a/PlexRequests.Core.Tests/NotificationMessageResolverTests.cs +++ b/PlexRequests.Core.Tests/NotificationMessageResolverTests.cs @@ -30,6 +30,7 @@ using System.Collections.Generic; using NUnit.Framework; using PlexRequests.Core.Models; +using PlexRequests.Core.Notification; using PlexRequests.Core.SettingModels; namespace PlexRequests.Core.Tests @@ -43,7 +44,7 @@ namespace PlexRequests.Core.Tests var n = new NotificationMessageResolver(); var s = new NotificationSettings { - Message = new List { new NotificationMessage { NotificationType = NotificationType.NewRequest, Body = body } } + Message = new List { new Notification.NotificationMessage { NotificationType = NotificationType.NewRequest, Body = body } } }; var result = n.ParseMessage(s, NotificationType.NewRequest, param); @@ -56,7 +57,7 @@ namespace PlexRequests.Core.Tests var n = new NotificationMessageResolver(); var s = new NotificationSettings { - Message = new List { new NotificationMessage { NotificationType = NotificationType.NewRequest, Subject = subject }} + Message = new List { new Notification.NotificationMessage { NotificationType = NotificationType.NewRequest, Subject = subject }} }; var result = n.ParseMessage(s, NotificationType.NewRequest, param); diff --git a/PlexRequests.Core/JsonIssuesModelRequestService.cs b/PlexRequests.Core/JsonIssuesModelRequestService.cs index dd6a8f9a7..10aa7a0b5 100644 --- a/PlexRequests.Core/JsonIssuesModelRequestService.cs +++ b/PlexRequests.Core/JsonIssuesModelRequestService.cs @@ -53,7 +53,7 @@ namespace PlexRequests.Core model.Id = id; entity = new IssueBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), RequestId = model.RequestId, Id = id }; - var result = await Repo.UpdateAsync(entity); + var result = await Repo.UpdateAsync(entity).ConfigureAwait(false); return result ? id : -1; } @@ -61,21 +61,21 @@ namespace PlexRequests.Core public async Task UpdateIssueAsync(IssuesModel model) { var entity = new IssueBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), RequestId = model.RequestId, Id = model.Id }; - return await Repo.UpdateAsync(entity); + return await Repo.UpdateAsync(entity).ConfigureAwait(false); } public async Task DeleteIssueAsync(int id) { var entity = await Repo.GetAsync(id); - await Repo.DeleteAsync(entity); + await Repo.DeleteAsync(entity).ConfigureAwait(false); } public async Task DeleteIssueAsync(IssuesModel model) { var entity = new IssueBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), RequestId = model.RequestId, Id = model.Id }; - await Repo.DeleteAsync(entity); + await Repo.DeleteAsync(entity).ConfigureAwait(false); } /// @@ -101,7 +101,7 @@ namespace PlexRequests.Core /// public async Task> GetAllAsync() { - var blobs = await Repo.GetAllAsync(); + var blobs = await Repo.GetAllAsync().ConfigureAwait(false); if (blobs == null) { diff --git a/PlexRequests.Core/JsonRequestModelRequestService.cs b/PlexRequests.Core/JsonRequestModelRequestService.cs index 795f797aa..12aaa7467 100644 --- a/PlexRequests.Core/JsonRequestModelRequestService.cs +++ b/PlexRequests.Core/JsonRequestModelRequestService.cs @@ -61,12 +61,12 @@ namespace PlexRequests.Core public async Task AddRequestAsync(RequestedModel model) { var entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId }; - var id = await Repo.InsertAsync(entity); + var id = await Repo.InsertAsync(entity).ConfigureAwait(false); model.Id = id; entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId, Id = id, MusicId = model.MusicBrainzId }; - var result = await Repo.UpdateAsync(entity); + var result = await Repo.UpdateAsync(entity).ConfigureAwait(false); return result ? id : -1; } @@ -80,7 +80,7 @@ namespace PlexRequests.Core public async Task CheckRequestAsync(int providerId) { - var blobs = await Repo.GetAllAsync(); + var blobs = await Repo.GetAllAsync().ConfigureAwait(false); var blob = blobs.FirstOrDefault(x => x.ProviderId == providerId); return blob != null ? ByteConverterHelper.ReturnObject(blob.Content) : null; } @@ -94,7 +94,7 @@ namespace PlexRequests.Core public async Task CheckRequestAsync(string musicId) { - var blobs = await Repo.GetAllAsync(); + var blobs = await Repo.GetAllAsync().ConfigureAwait(false); var blob = blobs.FirstOrDefault(x => x.MusicId == musicId); return blob != null ? ByteConverterHelper.ReturnObject(blob.Content) : null; } @@ -107,8 +107,8 @@ namespace PlexRequests.Core public async Task DeleteRequestAsync(RequestedModel request) { - var blob = await Repo.GetAsync(request.Id); - await Repo.DeleteAsync(blob); + var blob = await Repo.GetAsync(request.Id).ConfigureAwait(false); + await Repo.DeleteAsync(blob).ConfigureAwait(false); } public bool UpdateRequest(RequestedModel model) @@ -120,7 +120,7 @@ namespace PlexRequests.Core public async Task UpdateRequestAsync(RequestedModel model) { var entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId, Id = model.Id }; - return await Repo.UpdateAsync(entity); + return await Repo.UpdateAsync(entity).ConfigureAwait(false); } public RequestedModel Get(int id) @@ -136,7 +136,7 @@ namespace PlexRequests.Core public async Task GetAsync(int id) { - var blob = await Repo.GetAsync(id); + var blob = await Repo.GetAsync(id).ConfigureAwait(false); if (blob == null) { return new RequestedModel(); @@ -155,7 +155,7 @@ namespace PlexRequests.Core public async Task> GetAllAsync() { - var blobs = await Repo.GetAllAsync(); + var blobs = await Repo.GetAllAsync().ConfigureAwait(false); return blobs.Select(b => Encoding.UTF8.GetString(b.Content)) .Select(JsonConvert.DeserializeObject) .ToList(); @@ -169,7 +169,7 @@ namespace PlexRequests.Core public async Task BatchUpdateAsync(IEnumerable model) { var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ByteConverterHelper.ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList(); - return await Repo.UpdateAllAsync(entities); + return await Repo.UpdateAllAsync(entities).ConfigureAwait(false); } public bool BatchDelete(IEnumerable model) { @@ -180,7 +180,7 @@ namespace PlexRequests.Core public async Task BatchDeleteAsync(IEnumerable model) { var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ByteConverterHelper.ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList(); - return await Repo.DeleteAllAsync(entities); + return await Repo.DeleteAllAsync(entities).ConfigureAwait(false); } } } \ No newline at end of file diff --git a/PlexRequests.Core/NotificationMessage.cs b/PlexRequests.Core/Notification/NotificationMessage.cs similarity index 97% rename from PlexRequests.Core/NotificationMessage.cs rename to PlexRequests.Core/Notification/NotificationMessage.cs index 5b897323c..168211efd 100644 --- a/PlexRequests.Core/NotificationMessage.cs +++ b/PlexRequests.Core/Notification/NotificationMessage.cs @@ -26,7 +26,7 @@ #endregion using PlexRequests.Core.Models; -namespace PlexRequests.Core +namespace PlexRequests.Core.Notification { public class NotificationMessage { diff --git a/PlexRequests.Core/NotificationMessageContent.cs b/PlexRequests.Core/Notification/NotificationMessageContent.cs similarity index 97% rename from PlexRequests.Core/NotificationMessageContent.cs rename to PlexRequests.Core/Notification/NotificationMessageContent.cs index 3a89eda6c..4c0ad0bd4 100644 --- a/PlexRequests.Core/NotificationMessageContent.cs +++ b/PlexRequests.Core/Notification/NotificationMessageContent.cs @@ -24,7 +24,7 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ************************************************************************/ #endregion -namespace PlexRequests.Core +namespace PlexRequests.Core.Notification { public class NotificationMessageContent { diff --git a/PlexRequests.Core/NotificationMessageCurlys.cs b/PlexRequests.Core/Notification/NotificationMessageCurlys.cs similarity index 98% rename from PlexRequests.Core/NotificationMessageCurlys.cs rename to PlexRequests.Core/Notification/NotificationMessageCurlys.cs index ea10494a4..28edd08a3 100644 --- a/PlexRequests.Core/NotificationMessageCurlys.cs +++ b/PlexRequests.Core/Notification/NotificationMessageCurlys.cs @@ -24,10 +24,9 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ************************************************************************/ #endregion -using System; using System.Collections.Generic; -namespace PlexRequests.Core +namespace PlexRequests.Core.Notification { public class NotificationMessageCurlys { diff --git a/PlexRequests.Core/NotificationMessageResolver.cs b/PlexRequests.Core/Notification/NotificationMessageResolver.cs similarity index 67% rename from PlexRequests.Core/NotificationMessageResolver.cs rename to PlexRequests.Core/Notification/NotificationMessageResolver.cs index 9271c7683..ed90bbf12 100644 --- a/PlexRequests.Core/NotificationMessageResolver.cs +++ b/PlexRequests.Core/Notification/NotificationMessageResolver.cs @@ -30,12 +30,27 @@ using System.Linq; using PlexRequests.Core.Models; using PlexRequests.Core.SettingModels; -namespace PlexRequests.Core +namespace PlexRequests.Core.Notification { public class NotificationMessageResolver { + /// + /// The start character '{' + /// private const char StartChar = (char)123; + /// + /// The end character '}' + /// private const char EndChar = (char)125; + + /// + /// Parses the message. + /// + /// + /// The notification. + /// The type. + /// The c. + /// public NotificationMessageContent ParseMessage(T notification, NotificationType type, NotificationMessageCurlys c) where T : NotificationSettings { var content = notification.Message.FirstOrDefault(x => x.NotificationType == type); @@ -47,33 +62,30 @@ namespace PlexRequests.Core return Resolve(content.Body, content.Subject, c.Curlys); } - private NotificationMessageContent Resolve(string body, string subject, Dictionary paramaters) + /// + /// Resolves the specified message curly fields. + /// + /// The body. + /// The subject. + /// The parameters. + /// + private NotificationMessageContent Resolve(string body, string subject, IReadOnlyDictionary parameters) { - + // Find the fields var bodyFields = FindCurlyFields(body); var subjectFields = FindCurlyFields(subject); - foreach (var f in bodyFields) - { - string outString; - if (paramaters.TryGetValue(f, out outString)) - { - body = body.Replace($"{{{f}}}", outString); - } - } - - foreach (var s in subjectFields) - { - string outString; - if (paramaters.TryGetValue(s, out outString)) - { - subject = subject.Replace($"{{{s}}}", outString); - } - } + body = ReplaceFields(bodyFields, parameters, body); + subject = ReplaceFields(subjectFields, parameters, subject); return new NotificationMessageContent { Body = body ?? string.Empty, Subject = subject ?? string.Empty }; } + /// + /// Finds the curly fields. + /// + /// The message. + /// private IEnumerable FindCurlyFields(string message) { if (string.IsNullOrEmpty(message)) @@ -116,5 +128,25 @@ namespace PlexRequests.Core return fields; } + + /// + /// Replaces the fields. + /// + /// The fields. + /// The parameters. + /// The main text. + /// + private string ReplaceFields(IEnumerable fields, IReadOnlyDictionary parameters, string mainText) + { + foreach (var field in fields) + { + string outString; + if (parameters.TryGetValue(field, out outString)) + { + mainText = mainText.Replace($"{{{field}}}", outString); + } + } + return mainText; + } } } \ No newline at end of file diff --git a/PlexRequests.Core/PlexRequests.Core.csproj b/PlexRequests.Core/PlexRequests.Core.csproj index 44cabceeb..d1036148c 100644 --- a/PlexRequests.Core/PlexRequests.Core.csproj +++ b/PlexRequests.Core/PlexRequests.Core.csproj @@ -67,10 +67,10 @@ - - - - + + + + @@ -130,6 +130,7 @@ PlexRequests.Store +