Preparations for cache changes.

Log fix
This commit is contained in:
Jeddunk 2021-01-10 17:28:57 +01:00
parent 1540e6cb7a
commit d384ab0cc0
2 changed files with 44 additions and 23 deletions

View File

@ -28,14 +28,33 @@ namespace GoldbergGUI.Core.Services
} }
// ReSharper disable once UnusedType.Global // ReSharper disable once UnusedType.Global
// ReSharper disable once ClassNeverInstantiated.Global
public class SteamService : ISteamService public class SteamService : ISteamService
{ {
private const string CachePath1 = "steamapps.json"; // ReSharper disable StringLiteralTypo
//private const string CachePath1 = "steamapps_games.json"; private readonly List<(string filename, string uri, string type)> _caches = new List<(string, string, string)>
//private const string CachePath2 = "steamapps_dlc.json"; {
private const string SteamUri1 = "https://api.steampowered.com/ISteamApps/GetAppList/v2/"; (
//private const string SteamUri1 = "https://api.steampowered.com/IStoreService/GetAppList/v1/?include_games=1&key="; "steamapps.json",
//private const string SteamUri2 = "https://api.steampowered.com/IStoreService/GetAppList/v1/?include_games=0&include_dlc=1&key="; "https://api.steampowered.com/ISteamApps/GetAppList/v2/",
null
),
(
"steamapps_games.json",
"https://api.steampowered.com/IStoreService/GetAppList/v1/?include_games=1&key=" +
Secrets.SteamWebApiKey(),
AppType.Game
),
(
"steamapps_dlc.json",
"https://api.steampowered.com/IStoreService/GetAppList/v1/?include_games=0&include_dlc=1&key=" +
Secrets.SteamWebApiKey(),
AppType.DLC
)
};
// ReSharper enable StringLiteralTypo
private static readonly Secrets Secrets = new Secrets();
private const string UserAgent = private const string UserAgent =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " +
@ -46,11 +65,12 @@ namespace GoldbergGUI.Core.Services
public async Task Initialize(IMvxLog log) public async Task Initialize(IMvxLog log)
{ {
var secrets = new Secrets(); var path = _caches.First().filename;
var uri = _caches.First().uri;
_log = log; _log = log;
_log.Info("Updating cache..."); _log.Info("Updating cache...");
var updateNeeded = DateTime.Now.Subtract(File.GetLastWriteTimeUtc(CachePath1)).TotalDays >= 1; var updateNeeded = DateTime.Now.Subtract(File.GetLastWriteTimeUtc(path)).TotalDays >= 1;
var cacheString = await GetCache(updateNeeded, SteamUri1, CachePath1).ConfigureAwait(false); var cacheString = await GetCache(updateNeeded, uri, path).ConfigureAwait(false);
SteamApps steamApps; SteamApps steamApps;
try try
{ {
@ -58,30 +78,29 @@ namespace GoldbergGUI.Core.Services
} }
catch (JsonException) catch (JsonException)
{ {
cacheString = await GetCache(true, SteamUri1, CachePath1).ConfigureAwait(false); cacheString = await GetCache(true, uri, path).ConfigureAwait(false);
steamApps = JsonSerializer.Deserialize<SteamApps>(cacheString); steamApps = JsonSerializer.Deserialize<SteamApps>(cacheString);
} }
_cache = new HashSet<SteamApp>(steamApps.AppList.Apps); _cache = new HashSet<SteamApp>(steamApps.AppList.Apps);
_log.Info("Loaded cache into memory!"); _log.Info("Loaded cache into memory!");
} }
private async Task<string> GetCache(bool updateNeeded, string steamUri, string cachePath) private async Task<string> GetCache(bool updateNeeded, string steamUri, string cachePath)
{ {
var secrets = new Secrets();
string cacheString; string cacheString;
if (updateNeeded) if (updateNeeded)
{ {
_log.Info("Getting content from API..."); _log.Info("Getting content from API...");
var client = new HttpClient(); var client = new HttpClient();
var httpCall = client.GetAsync(steamUri + secrets.SteamWebApiKey()); var response = await client.GetAsync(steamUri).ConfigureAwait(false);
var response = await httpCall.ConfigureAwait(false); var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var readAsStringAsync = response.Content.ReadAsStringAsync();
var responseBody = await readAsStringAsync.ConfigureAwait(false);
_log.Info("Got content from API successfully. Writing to file...");
_log.Info("Got content from API successfully. Writing to file...");
await File.WriteAllTextAsync(cachePath, responseBody, Encoding.UTF8).ConfigureAwait(false); await File.WriteAllTextAsync(cachePath, responseBody, Encoding.UTF8).ConfigureAwait(false);
cacheString = responseBody;
_log.Info("Cache written to file successfully."); _log.Info("Cache written to file successfully.");
cacheString = responseBody;
} }
else else
{ {

View File

@ -38,13 +38,15 @@ namespace GoldbergGUI.Core.ViewModels
private bool _goldbergApplied; private bool _goldbergApplied;
private ObservableCollection<string> _steamLanguages; private ObservableCollection<string> _steamLanguages;
private string _selectedLanguage; private string _selectedLanguage;
private readonly IMvxLogProvider _logProvider;
public MainViewModel(ISteamService steam, IGoldbergService goldberg, IMvxLogProvider logProvider, public MainViewModel(ISteamService steam, IGoldbergService goldberg, IMvxLogProvider logProvider,
IMvxNavigationService navigationService) IMvxNavigationService navigationService)
{ {
_steam = steam; _steam = steam;
_goldberg = goldberg; _goldberg = goldberg;
_log = logProvider.GetLogFor(typeof(MainViewModel)); _logProvider = logProvider;
_log = logProvider.GetLogFor<MainViewModel>();
_navigationService = navigationService; _navigationService = navigationService;
} }
@ -59,9 +61,9 @@ namespace GoldbergGUI.Core.ViewModels
{ {
SteamLanguages = new ObservableCollection<string>(_goldberg.Languages()); SteamLanguages = new ObservableCollection<string>(_goldberg.Languages());
ResetForm(); ResetForm();
await _steam.Initialize(_log).ConfigureAwait(false); await _steam.Initialize(_logProvider.GetLogFor<SteamService>()).ConfigureAwait(false);
var (accountName, userSteamId, language) = var (accountName, userSteamId, language) =
await _goldberg.Initialize(_log).ConfigureAwait(false); await _goldberg.Initialize(_logProvider.GetLogFor<GoldbergService>()).ConfigureAwait(false);
AccountName = accountName; AccountName = accountName;
SteamId = userSteamId; SteamId = userSteamId;
SelectedLanguage = language; SelectedLanguage = language;