refactor file migration

This commit is contained in:
Robert McRackan 2020-08-18 07:14:58 -04:00
parent b768362eae
commit 09dbc67914
3 changed files with 25 additions and 29 deletions

View File

@ -12,13 +12,6 @@ namespace InternalUtilities
{ {
public static class AudibleApiActions public static class AudibleApiActions
{ {
public static async Task<Api> GetApiAsyncLegacy30Async()
{
Localization.SetLocale(Configuration.Instance.LocaleCountryCode);
return await EzApiCreator.GetApiAsync(AudibleApiStorage.AccountsSettingsFileLegacy30);
}
/// <summary>USE THIS from within Libation. It wraps the call with correct JSONPath</summary> /// <summary>USE THIS from within Libation. It wraps the call with correct JSONPath</summary>
public static async Task<Api> GetApiAsync(ILoginCallback loginCallback = null) public static async Task<Api> GetApiAsync(ILoginCallback loginCallback = null)
{ {

View File

@ -10,8 +10,6 @@ namespace InternalUtilities
{ {
public static class AudibleApiStorage public static class AudibleApiStorage
{ {
public static string AccountsSettingsFileLegacy30 => Path.Combine(Configuration.Instance.LibationFiles, "IdentityTokens.json");
public static string AccountsSettingsFile => Path.Combine(Configuration.Instance.LibationFiles, "AccountsSettings.json"); public static string AccountsSettingsFile => Path.Combine(Configuration.Instance.LibationFiles, "AccountsSettings.json");
public static void EnsureAccountsSettingsFileExists() public static void EnsureAccountsSettingsFileExists()

View File

@ -25,7 +25,8 @@ namespace LibationLauncher
createSettings(); createSettings();
AudibleApiStorage.EnsureAccountsSettingsFileExists(); AudibleApiStorage.EnsureAccountsSettingsFileExists();
migrateIdentityFile();
migrate_v3_to_v4();
ensureLoggingConfig(); ensureLoggingConfig();
ensureSerilogConfig(); ensureSerilogConfig();
@ -81,12 +82,14 @@ namespace LibationLauncher
Environment.Exit(0); Environment.Exit(0);
} }
private static void migrateIdentityFile() #region v3 => v4 migration
{ static string AccountsSettingsFileLegacy30 => Path.Combine(Configuration.Instance.LibationFiles, "IdentityTokens.json");
if (!File.Exists(AudibleApiStorage.AccountsSettingsFileLegacy30))
return;
// in here: don't rely on applicable POCOs. some is legacy and must be: json file => JObject private static void migrate_v3_to_v4()
{
if (File.Exists(AccountsSettingsFileLegacy30))
{
// don't always rely on applicable POCOs. some is legacy and must be: json file => JObject
try try
{ {
updateLegacyFileWithLocale(); updateLegacyFileWithLocale();
@ -99,15 +102,15 @@ namespace LibationLauncher
catch { } catch { }
// delete legacy token file // delete legacy token file
File.Delete(AudibleApiStorage.AccountsSettingsFileLegacy30); File.Delete(AccountsSettingsFileLegacy30);
}
// in reality, only need to run this when migating v3 => v4. put inside here so already upgraded versions won't make this check
updateSettingsFile(); updateSettingsFile();
} }
private static void updateLegacyFileWithLocale() private static void updateLegacyFileWithLocale()
{ {
var legacyContents = File.ReadAllText(AudibleApiStorage.AccountsSettingsFileLegacy30); var legacyContents = File.ReadAllText(AccountsSettingsFileLegacy30);
var legacyJObj = JObject.Parse(legacyContents); var legacyJObj = JObject.Parse(legacyContents);
// attempt to update legacy token file with locale from settings // attempt to update legacy token file with locale from settings
@ -122,19 +125,20 @@ namespace LibationLauncher
// save // save
var newContents = legacyJObj.ToString(Formatting.Indented); var newContents = legacyJObj.ToString(Formatting.Indented);
File.WriteAllText(AudibleApiStorage.AccountsSettingsFileLegacy30, newContents); File.WriteAllText(AccountsSettingsFileLegacy30, newContents);
} }
} }
} }
private static Account addAccountToNewAccountFile() private static Account addAccountToNewAccountFile()
{ {
var api = AudibleApiActions.GetApiAsyncLegacy30Async().GetAwaiter().GetResult(); AudibleApi.Localization.SetLocale(Configuration.Instance.LocaleCountryCode);
var api = AudibleApi.EzApiCreator.GetApiAsync(AccountsSettingsFileLegacy30).GetAwaiter().GetResult();
var email = api.GetEmailAsync().GetAwaiter().GetResult(); var email = api.GetEmailAsync().GetAwaiter().GetResult();
var locale = api.GetLocaleAsync(AudibleApi.CustomerOptions.All).GetAwaiter().GetResult(); var locale = api.GetLocaleAsync(AudibleApi.CustomerOptions.All).GetAwaiter().GetResult();
// identity has likely been updated above. re-get contents // identity has likely been updated above. re-get contents
var legacyContents = File.ReadAllText(AudibleApiStorage.AccountsSettingsFileLegacy30); var legacyContents = File.ReadAllText(AccountsSettingsFileLegacy30);
var identity = AudibleApi.Authorization.Identity.FromJson(legacyContents); var identity = AudibleApi.Authorization.Identity.FromJson(legacyContents);
@ -194,6 +198,7 @@ throw new NotImplementedException();
File.WriteAllText(Configuration.Instance.SettingsFilePath, newContents); File.WriteAllText(Configuration.Instance.SettingsFilePath, newContents);
} }
} }
#endregion
private static string defaultLoggingLevel { get; } = "Information"; private static string defaultLoggingLevel { get; } = "Information";
private static void ensureLoggingConfig() private static void ensureLoggingConfig()