From a88da7f6d07b6867287073828dce73a9d0f986e9 Mon Sep 17 00:00:00 2001 From: Mario Steele Date: Thu, 31 Jul 2025 14:26:20 -0500 Subject: [PATCH] Updated Syncer Added GetLastUpdated() to ISyncer interface. Updated code to use new /ping endpoint. Added GetLastUpdated() REST API call to fetch when the database was last updated. Clarified logging for when an Update is from a file, or from the REST API Server. Added logging for when a nw File Entry is processed, or when a File Entry is Updated. --- FreeTubeSyncer/REST/Syncer.cs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/FreeTubeSyncer/REST/Syncer.cs b/FreeTubeSyncer/REST/Syncer.cs index ea7b1a2..71ace55 100644 --- a/FreeTubeSyncer/REST/Syncer.cs +++ b/FreeTubeSyncer/REST/Syncer.cs @@ -27,6 +27,7 @@ public interface ISyncer void UpdateBaseUrl(string baseUrl); void SetEnabled(bool enabled); Task PingApi(); + Task GetLastUpdated(); } public class Syncer : ISyncer where T : class, IDataModel, new() @@ -71,11 +72,18 @@ public class Syncer : ISyncer where T : class, IDataModel, new() Console.WriteLine($"Pinging API at {_client.BuildUri(new RestRequest("/ping"))}..."); try { - var res = await _client.ExecuteHeadAsync(new RestRequest("/ping")); - if (res.StatusCode == HttpStatusCode.NotFound) + var res = await _client.GetAsync(new RestRequest("/ping")); + if (res == null) { // TODO: Replace with Logger - Console.WriteLine($"Ping response 404 Not Found, Server Online!"); + Console.WriteLine($"Ping returned null, not the server we are looking for!"); + return false; + } + + if (res.AppVersion == "0.1.3") + { + // TODO: Replace with Logger + Console.WriteLine($"Server Online! {res.AppVersion}"); return true; } } @@ -91,6 +99,12 @@ public class Syncer : ISyncer where T : class, IDataModel, new() return false; } + public async Task GetLastUpdated() + { + var res = await _client.GetAsync("/ping/lastUpdated"); + return res?.LastUpdated ?? DateTime.MinValue; + } + public async Task ReadDatabase() { if (!_enabled) return; @@ -115,7 +129,7 @@ public class Syncer : ISyncer where T : class, IDataModel, new() _entries.RemoveAll(x => x.EqualId(item.Id())); _entries.Add(item); // TODO: Replace with Logger - Console.WriteLine($"Posting {item.Id()}"); + Console.WriteLine($"Posting to REST: {item.Id()}"); await _client.PostJsonAsync(_restEndpoint, item); } } @@ -134,13 +148,13 @@ public class Syncer : ISyncer where T : class, IDataModel, new() if (data.Equals(entry)) continue; // TODO: Replace with Logger - Console.WriteLine($"Updated Entry for {_dbName} - {entry.Id()}"); + Console.WriteLine($"Updated Entry from REST for {_dbName} - {entry.Id()}"); _entries.RemoveAll(x => x.EqualId(entry.Id())); } else { // TODO: Replace with Logger - Console.WriteLine($"New Entry for {_dbName} - {entry.Id()}"); + Console.WriteLine($"New Entry from REST for {_dbName} - {entry.Id()}"); } _entries.Add(entry); @@ -185,9 +199,15 @@ public class Syncer : ISyncer where T : class, IDataModel, new() { var data = _entries.First(x => x.EqualId(entry.Id())); if (data.Equals(entry)) return; - + // TODO: Replace with Logger + Console.WriteLine($"File Entry {entry.Id()} updated for {_dbName}"); _entries.RemoveAll(x => x.EqualId(entry.Id())); } + else + { + // TODO: Replace with Logger + Console.WriteLine($"New File Entry {entry.Id()} for {_dbName}"); + } _entries.Add(entry); await _client.PostJsonAsync(_restEndpoint, entry);