Importers need access to Account

This commit is contained in:
Robert McRackan 2020-08-20 16:09:07 -04:00
parent 57ee150d3c
commit c67972a327
10 changed files with 35 additions and 20 deletions

View File

@ -13,13 +13,15 @@ namespace ApplicationServices
{ {
try try
{ {
var items = await AudibleApiActions.GetAllLibraryItemsAsync(callback); var account = AudibleApiStorage.TEST_GetFirstAccount();
var items = await AudibleApiActions.GetAllLibraryItemsAsync(account, callback);
var totalCount = items.Count; var totalCount = items.Count;
Serilog.Log.Logger.Information($"GetAllLibraryItems: Total count {totalCount}"); Serilog.Log.Logger.Information($"GetAllLibraryItems: Total count {totalCount}");
using var context = DbContexts.GetContext(); using var context = DbContexts.GetContext();
var libImporter = new LibraryImporter(context); var libraryImporter = new LibraryImporter(context, account);
var newCount = await Task.Run(() => libImporter.Import(items)); var newCount = await Task.Run(() => libraryImporter.Import(items));
context.SaveChanges(); context.SaveChanges();
Serilog.Log.Logger.Information($"Import: New count {newCount}"); Serilog.Log.Logger.Information($"Import: New count {newCount}");

View File

@ -9,16 +9,16 @@ namespace DtoImporterService
{ {
public class BookImporter : ItemsImporterBase public class BookImporter : ItemsImporterBase
{ {
public BookImporter(LibationContext context) : base(context) { } public BookImporter(LibationContext context, Account account) : base(context, account) { }
public override IEnumerable<Exception> Validate(IEnumerable<Item> items) => new BookValidator().Validate(items); public override IEnumerable<Exception> Validate(IEnumerable<Item> items) => new BookValidator().Validate(items);
protected override int DoImport(IEnumerable<Item> items) protected override int DoImport(IEnumerable<Item> items)
{ {
// pre-req.s // pre-req.s
new ContributorImporter(DbContext).Import(items); new ContributorImporter(DbContext, Account).Import(items);
new SeriesImporter(DbContext).Import(items); new SeriesImporter(DbContext, Account).Import(items);
new CategoryImporter(DbContext).Import(items); new CategoryImporter(DbContext, Account).Import(items);
// get distinct // get distinct
var productIds = items.Select(i => i.ProductId).ToList(); var productIds = items.Select(i => i.ProductId).ToList();

View File

@ -9,7 +9,7 @@ namespace DtoImporterService
{ {
public class CategoryImporter : ItemsImporterBase public class CategoryImporter : ItemsImporterBase
{ {
public CategoryImporter(LibationContext context) : base(context) { } public CategoryImporter(LibationContext context, Account account) : base(context, account) { }
public override IEnumerable<Exception> Validate(IEnumerable<Item> items) => new CategoryValidator().Validate(items); public override IEnumerable<Exception> Validate(IEnumerable<Item> items) => new CategoryValidator().Validate(items);

View File

@ -9,7 +9,7 @@ namespace DtoImporterService
{ {
public class ContributorImporter : ItemsImporterBase public class ContributorImporter : ItemsImporterBase
{ {
public ContributorImporter(LibationContext context) : base(context) { } public ContributorImporter(LibationContext context, Account account) : base(context, account) { }
public override IEnumerable<Exception> Validate(IEnumerable<Item> items) => new ContributorValidator().Validate(items); public override IEnumerable<Exception> Validate(IEnumerable<Item> items) => new ContributorValidator().Validate(items);

View File

@ -4,17 +4,22 @@ using System.Linq;
using AudibleApiDTOs; using AudibleApiDTOs;
using DataLayer; using DataLayer;
using Dinah.Core; using Dinah.Core;
using InternalUtilities;
namespace DtoImporterService namespace DtoImporterService
{ {
public abstract class ImporterBase<T> public abstract class ImporterBase<T>
{ {
protected LibationContext DbContext { get; } 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(context, nameof(context));
ArgumentValidator.EnsureNotNull(account, nameof(account));
DbContext = context; DbContext = context;
Account = account;
} }
/// <summary>LONG RUNNING. call with await Task.Run</summary> /// <summary>LONG RUNNING. call with await Task.Run</summary>
@ -52,6 +57,6 @@ namespace DtoImporterService
public abstract class ItemsImporterBase : ImporterBase<IEnumerable<Item>> public abstract class ItemsImporterBase : ImporterBase<IEnumerable<Item>>
{ {
public ItemsImporterBase(LibationContext context) : base(context) { } protected ItemsImporterBase(LibationContext context, Account account) : base(context, account) { }
} }
} }

View File

@ -9,13 +9,13 @@ namespace DtoImporterService
{ {
public class LibraryImporter : ItemsImporterBase public class LibraryImporter : ItemsImporterBase
{ {
public LibraryImporter(LibationContext context) : base(context) { } public LibraryImporter(LibationContext context, Account account) : base(context, account) { }
public override IEnumerable<Exception> Validate(IEnumerable<Item> items) => new LibraryValidator().Validate(items); public override IEnumerable<Exception> Validate(IEnumerable<Item> items) => new LibraryValidator().Validate(items);
protected override int DoImport(IEnumerable<Item> items) protected override int DoImport(IEnumerable<Item> items)
{ {
new BookImporter(DbContext).Import(items); new BookImporter(DbContext, Account).Import(items);
var qtyNew = upsertLibraryBooks(items); var qtyNew = upsertLibraryBooks(items);
return qtyNew; return qtyNew;

View File

@ -9,7 +9,7 @@ namespace DtoImporterService
{ {
public class SeriesImporter : ItemsImporterBase public class SeriesImporter : ItemsImporterBase
{ {
public SeriesImporter(LibationContext context) : base(context) { } public SeriesImporter(LibationContext context, Account account) : base(context, account) { }
public override IEnumerable<Exception> Validate(IEnumerable<Item> items) => new SeriesValidator().Validate(items); public override IEnumerable<Exception> Validate(IEnumerable<Item> items) => new SeriesValidator().Validate(items);

View File

@ -20,22 +20,30 @@ namespace InternalUtilities
return await EzApiCreator.GetApiAsync(AudibleApiStorage.AccountsSettingsFile, AudibleApiStorage.TEST_GetFirstIdentityTokensJsonPath(), loginCallback); return await EzApiCreator.GetApiAsync(AudibleApiStorage.AccountsSettingsFile, AudibleApiStorage.TEST_GetFirstIdentityTokensJsonPath(), loginCallback);
} }
/// <summary>USE THIS from within Libation. It wraps the call with correct JSONPath</summary>
public static async Task<Api> 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; } private static AsyncRetryPolicy policy { get; }
= Policy.Handle<Exception>() = Policy.Handle<Exception>()
// 2 retries == 3 total // 2 retries == 3 total
.RetryAsync(2); .RetryAsync(2);
public static Task<List<Item>> GetAllLibraryItemsAsync(ILoginCallback callback) public static Task<List<Item>> 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 // 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: // 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 }); // 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 // 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<List<Item>> getItemsAsync(ILoginCallback callback) private static async Task<List<Item>> getItemsAsync(Account account, ILoginCallback callback)
{ {
var api = await GetApiAsync(callback); var api = await GetApiAsync(account, callback);
var items = await api.GetAllLibraryItemsAsync(); var items = await api.GetAllLibraryItemsAsync();
// remove episode parents // remove episode parents

View File

@ -13,7 +13,7 @@
<!-- <PublishSingleFile>true</PublishSingleFile> --> <!-- <PublishSingleFile>true</PublishSingleFile> -->
<RuntimeIdentifier>win-x64</RuntimeIdentifier> <RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Version>3.1.12.136</Version> <Version>3.1.12.139</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -22,7 +22,7 @@ namespace LibationWinForms.Dialogs
{ {
(TotalBooksProcessed, NewBooksAdded) = await LibraryCommands.ImportLibraryAsync(new WinformResponder()); (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"; 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); MessageBox.Show(msg, "Error importing library", MessageBoxButtons.OK, MessageBoxIcon.Error);