From c67972a3276036d65266c1a6b93ec01a7341764c Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Thu, 20 Aug 2020 16:09:07 -0400 Subject: [PATCH] Importers need access to Account --- ApplicationServices/UNTESTED/LibraryCommands.cs | 8 +++++--- DtoImporterService/UNTESTED/BookImporter.cs | 8 ++++---- DtoImporterService/UNTESTED/CategoryImporter.cs | 2 +- .../UNTESTED/ContributorImporter.cs | 2 +- DtoImporterService/UNTESTED/ImporterBase.cs | 9 +++++++-- DtoImporterService/UNTESTED/LibraryImporter.cs | 4 ++-- DtoImporterService/UNTESTED/SeriesImporter.cs | 2 +- InternalUtilities/UNTESTED/AudibleApiActions.cs | 16 ++++++++++++---- LibationLauncher/LibationLauncher.csproj | 2 +- .../UNTESTED/Dialogs/IndexLibraryDialog.cs | 2 +- 10 files changed, 35 insertions(+), 20 deletions(-) diff --git a/ApplicationServices/UNTESTED/LibraryCommands.cs b/ApplicationServices/UNTESTED/LibraryCommands.cs index c7306c9a..96817369 100644 --- a/ApplicationServices/UNTESTED/LibraryCommands.cs +++ b/ApplicationServices/UNTESTED/LibraryCommands.cs @@ -13,13 +13,15 @@ namespace ApplicationServices { try { - var items = await AudibleApiActions.GetAllLibraryItemsAsync(callback); + var account = AudibleApiStorage.TEST_GetFirstAccount(); + + var items = await AudibleApiActions.GetAllLibraryItemsAsync(account, callback); var totalCount = items.Count; Serilog.Log.Logger.Information($"GetAllLibraryItems: Total count {totalCount}"); using var context = DbContexts.GetContext(); - var libImporter = new LibraryImporter(context); - var newCount = await Task.Run(() => libImporter.Import(items)); + var libraryImporter = new LibraryImporter(context, account); + var newCount = await Task.Run(() => libraryImporter.Import(items)); context.SaveChanges(); Serilog.Log.Logger.Information($"Import: New count {newCount}"); diff --git a/DtoImporterService/UNTESTED/BookImporter.cs b/DtoImporterService/UNTESTED/BookImporter.cs index a064cfd3..518387c2 100644 --- a/DtoImporterService/UNTESTED/BookImporter.cs +++ b/DtoImporterService/UNTESTED/BookImporter.cs @@ -9,16 +9,16 @@ namespace DtoImporterService { public class BookImporter : ItemsImporterBase { - public BookImporter(LibationContext context) : base(context) { } + public BookImporter(LibationContext context, Account account) : base(context, account) { } public override IEnumerable Validate(IEnumerable items) => new BookValidator().Validate(items); protected override int DoImport(IEnumerable items) { // pre-req.s - new ContributorImporter(DbContext).Import(items); - new SeriesImporter(DbContext).Import(items); - new CategoryImporter(DbContext).Import(items); + new ContributorImporter(DbContext, Account).Import(items); + new SeriesImporter(DbContext, Account).Import(items); + new CategoryImporter(DbContext, Account).Import(items); // get distinct var productIds = items.Select(i => i.ProductId).ToList(); diff --git a/DtoImporterService/UNTESTED/CategoryImporter.cs b/DtoImporterService/UNTESTED/CategoryImporter.cs index a3789049..19e75c4b 100644 --- a/DtoImporterService/UNTESTED/CategoryImporter.cs +++ b/DtoImporterService/UNTESTED/CategoryImporter.cs @@ -9,7 +9,7 @@ namespace DtoImporterService { public class CategoryImporter : ItemsImporterBase { - public CategoryImporter(LibationContext context) : base(context) { } + public CategoryImporter(LibationContext context, Account account) : base(context, account) { } public override IEnumerable Validate(IEnumerable items) => new CategoryValidator().Validate(items); diff --git a/DtoImporterService/UNTESTED/ContributorImporter.cs b/DtoImporterService/UNTESTED/ContributorImporter.cs index f6940160..2bf067d4 100644 --- a/DtoImporterService/UNTESTED/ContributorImporter.cs +++ b/DtoImporterService/UNTESTED/ContributorImporter.cs @@ -9,7 +9,7 @@ namespace DtoImporterService { public class ContributorImporter : ItemsImporterBase { - public ContributorImporter(LibationContext context) : base(context) { } + public ContributorImporter(LibationContext context, Account account) : base(context, account) { } public override IEnumerable Validate(IEnumerable items) => new ContributorValidator().Validate(items); diff --git a/DtoImporterService/UNTESTED/ImporterBase.cs b/DtoImporterService/UNTESTED/ImporterBase.cs index 49c5234f..c7cb6f3c 100644 --- a/DtoImporterService/UNTESTED/ImporterBase.cs +++ b/DtoImporterService/UNTESTED/ImporterBase.cs @@ -4,17 +4,22 @@ using System.Linq; using AudibleApiDTOs; using DataLayer; using Dinah.Core; +using InternalUtilities; namespace DtoImporterService { public abstract class ImporterBase { protected LibationContext DbContext { get; } + protected Account Account { get; } - public ImporterBase(LibationContext context) + protected ImporterBase(LibationContext context, Account account) { ArgumentValidator.EnsureNotNull(context, nameof(context)); + ArgumentValidator.EnsureNotNull(account, nameof(account)); + DbContext = context; + Account = account; } /// LONG RUNNING. call with await Task.Run @@ -52,6 +57,6 @@ namespace DtoImporterService public abstract class ItemsImporterBase : ImporterBase> { - public ItemsImporterBase(LibationContext context) : base(context) { } + protected ItemsImporterBase(LibationContext context, Account account) : base(context, account) { } } } diff --git a/DtoImporterService/UNTESTED/LibraryImporter.cs b/DtoImporterService/UNTESTED/LibraryImporter.cs index 710f03c3..3e9184e7 100644 --- a/DtoImporterService/UNTESTED/LibraryImporter.cs +++ b/DtoImporterService/UNTESTED/LibraryImporter.cs @@ -9,13 +9,13 @@ namespace DtoImporterService { public class LibraryImporter : ItemsImporterBase { - public LibraryImporter(LibationContext context) : base(context) { } + public LibraryImporter(LibationContext context, Account account) : base(context, account) { } public override IEnumerable Validate(IEnumerable items) => new LibraryValidator().Validate(items); protected override int DoImport(IEnumerable items) { - new BookImporter(DbContext).Import(items); + new BookImporter(DbContext, Account).Import(items); var qtyNew = upsertLibraryBooks(items); return qtyNew; diff --git a/DtoImporterService/UNTESTED/SeriesImporter.cs b/DtoImporterService/UNTESTED/SeriesImporter.cs index 9df17de9..7c2e0358 100644 --- a/DtoImporterService/UNTESTED/SeriesImporter.cs +++ b/DtoImporterService/UNTESTED/SeriesImporter.cs @@ -9,7 +9,7 @@ namespace DtoImporterService { public class SeriesImporter : ItemsImporterBase { - public SeriesImporter(LibationContext context) : base(context) { } + public SeriesImporter(LibationContext context, Account account) : base(context, account) { } public override IEnumerable Validate(IEnumerable items) => new SeriesValidator().Validate(items); diff --git a/InternalUtilities/UNTESTED/AudibleApiActions.cs b/InternalUtilities/UNTESTED/AudibleApiActions.cs index 8c873482..78200864 100644 --- a/InternalUtilities/UNTESTED/AudibleApiActions.cs +++ b/InternalUtilities/UNTESTED/AudibleApiActions.cs @@ -20,22 +20,30 @@ namespace InternalUtilities return await EzApiCreator.GetApiAsync(AudibleApiStorage.AccountsSettingsFile, AudibleApiStorage.TEST_GetFirstIdentityTokensJsonPath(), loginCallback); } + /// USE THIS from within Libation. It wraps the call with correct JSONPath + public static async Task GetApiAsync(Account account, ILoginCallback loginCallback = null) + { + Localization.SetLocale(Configuration.Instance.LocaleCountryCode); + + return await EzApiCreator.GetApiAsync(AudibleApiStorage.AccountsSettingsFile, account.GetIdentityTokensJsonPath(), loginCallback); + } + private static AsyncRetryPolicy policy { get; } = Policy.Handle() // 2 retries == 3 total .RetryAsync(2); - public static Task> GetAllLibraryItemsAsync(ILoginCallback callback) + public static Task> GetAllLibraryItemsAsync(Account account, ILoginCallback callback) { // bug on audible's side. the 1st time after a long absence, a query to get library will return without titles or authors. a subsequent identical query will be successful. this is true whether or tokens are refreshed // worse, this 1st dummy call doesn't seem to help: // var page = await api.GetLibraryAsync(new AudibleApi.LibraryOptions { NumberOfResultPerPage = 1, PageNumber = 1, PurchasedAfter = DateTime.Now.AddYears(-20), ResponseGroups = AudibleApi.LibraryOptions.ResponseGroupOptions.ALL_OPTIONS }); // i don't want to incur the cost of making a full dummy call every time because it fails sometimes - return policy.ExecuteAsync(() => getItemsAsync(callback)); + return policy.ExecuteAsync(() => getItemsAsync(account, callback)); } - private static async Task> getItemsAsync(ILoginCallback callback) + private static async Task> getItemsAsync(Account account, ILoginCallback callback) { - var api = await GetApiAsync(callback); + var api = await GetApiAsync(account, callback); var items = await api.GetAllLibraryItemsAsync(); // remove episode parents diff --git a/LibationLauncher/LibationLauncher.csproj b/LibationLauncher/LibationLauncher.csproj index ca737a9c..3ceec6a4 100644 --- a/LibationLauncher/LibationLauncher.csproj +++ b/LibationLauncher/LibationLauncher.csproj @@ -13,7 +13,7 @@ win-x64 - 3.1.12.136 + 3.1.12.139 diff --git a/LibationWinForms/UNTESTED/Dialogs/IndexLibraryDialog.cs b/LibationWinForms/UNTESTED/Dialogs/IndexLibraryDialog.cs index 860a2a38..aca3a34f 100644 --- a/LibationWinForms/UNTESTED/Dialogs/IndexLibraryDialog.cs +++ b/LibationWinForms/UNTESTED/Dialogs/IndexLibraryDialog.cs @@ -22,7 +22,7 @@ namespace LibationWinForms.Dialogs { (TotalBooksProcessed, NewBooksAdded) = await LibraryCommands.ImportLibraryAsync(new WinformResponder()); } - catch (Exception ex) + catch { var msg = "Error importing library. Please try again. If this still happens after 2 or 3 tries, stop and contact administrator"; MessageBox.Show(msg, "Error importing library", MessageBoxButtons.OK, MessageBoxIcon.Error);