refactor file migration
This commit is contained in:
parent
b768362eae
commit
09dbc67914
@ -12,13 +12,6 @@ namespace InternalUtilities
|
||||
{
|
||||
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>
|
||||
public static async Task<Api> GetApiAsync(ILoginCallback loginCallback = null)
|
||||
{
|
||||
|
||||
@ -10,8 +10,6 @@ namespace InternalUtilities
|
||||
{
|
||||
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 void EnsureAccountsSettingsFileExists()
|
||||
|
||||
@ -25,7 +25,8 @@ namespace LibationLauncher
|
||||
createSettings();
|
||||
|
||||
AudibleApiStorage.EnsureAccountsSettingsFileExists();
|
||||
migrateIdentityFile();
|
||||
|
||||
migrate_v3_to_v4();
|
||||
|
||||
ensureLoggingConfig();
|
||||
ensureSerilogConfig();
|
||||
@ -81,12 +82,14 @@ namespace LibationLauncher
|
||||
Environment.Exit(0);
|
||||
}
|
||||
|
||||
private static void migrateIdentityFile()
|
||||
{
|
||||
if (!File.Exists(AudibleApiStorage.AccountsSettingsFileLegacy30))
|
||||
return;
|
||||
#region v3 => v4 migration
|
||||
static string AccountsSettingsFileLegacy30 => Path.Combine(Configuration.Instance.LibationFiles, "IdentityTokens.json");
|
||||
|
||||
// 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
|
||||
{
|
||||
updateLegacyFileWithLocale();
|
||||
@ -99,15 +102,15 @@ namespace LibationLauncher
|
||||
catch { }
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
private static void updateLegacyFileWithLocale()
|
||||
{
|
||||
var legacyContents = File.ReadAllText(AudibleApiStorage.AccountsSettingsFileLegacy30);
|
||||
var legacyContents = File.ReadAllText(AccountsSettingsFileLegacy30);
|
||||
var legacyJObj = JObject.Parse(legacyContents);
|
||||
|
||||
// attempt to update legacy token file with locale from settings
|
||||
@ -122,19 +125,20 @@ namespace LibationLauncher
|
||||
|
||||
// save
|
||||
var newContents = legacyJObj.ToString(Formatting.Indented);
|
||||
File.WriteAllText(AudibleApiStorage.AccountsSettingsFileLegacy30, newContents);
|
||||
File.WriteAllText(AccountsSettingsFileLegacy30, newContents);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 locale = api.GetLocaleAsync(AudibleApi.CustomerOptions.All).GetAwaiter().GetResult();
|
||||
|
||||
// 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);
|
||||
|
||||
@ -194,6 +198,7 @@ throw new NotImplementedException();
|
||||
File.WriteAllText(Configuration.Instance.SettingsFilePath, newContents);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
private static string defaultLoggingLevel { get; } = "Information";
|
||||
private static void ensureLoggingConfig()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user