Updated Syncer
Added parameter for Rest Base URL to allow configurability of the Base URL for REST API. Updated ISyncer, to add Enable(), Disable(), IsEnabled(), UpdateBaseUrl(), SetEnabled() and PingApi() to allow for settings. PingApi() will test to see if the URL provided is reachable. UpdatebaseUrl() will set the URL for the server to communicate with. SetEnabled(), Enable() Disable() and IsEnabled() to allow for settings to toggle on and off syncing of specific parts of FreeTube.
This commit is contained in:
parent
ec55cf2f11
commit
e6236b8a5a
1 changed files with 41 additions and 3 deletions
|
|
@ -2,6 +2,7 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
|
|
@ -19,6 +20,13 @@ public interface ISyncer
|
|||
void HandleDatabaseChange(string dbName, string entryObject);
|
||||
void Sync();
|
||||
bool IsDirty();
|
||||
void Enable();
|
||||
void Disable();
|
||||
bool IsEnabled();
|
||||
|
||||
void UpdateBaseUrl(string baseUrl);
|
||||
void SetEnabled(bool enabled);
|
||||
Task<bool> PingApi();
|
||||
}
|
||||
|
||||
public class Syncer<T> : ISyncer where T : class, IDataModel, new()
|
||||
|
|
@ -32,23 +40,50 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
|
|||
|
||||
private bool _isDirty = false;
|
||||
private bool _syncing = false;
|
||||
private bool _enabled = true;
|
||||
|
||||
public Syncer(DBSyncWatcher watcher, string dbPath, string dbName, string restEndpoint)
|
||||
public Syncer(DBSyncWatcher watcher, string dbPath, string dbName, string restBaseUrl, string restEndpoint)
|
||||
{
|
||||
_watcher = watcher;
|
||||
_watcher.WatchFiles[dbName] = typeof(T);
|
||||
_watcher.OnDatabaseChange += HandleDatabaseChange;
|
||||
_client = new RestClient(new RestClientOptions("http://192.168.1.30:5050"));
|
||||
_client = new RestClient(new RestClientOptions(restBaseUrl));
|
||||
_dbPath = dbPath;
|
||||
_dbName = dbName;
|
||||
_restEndpoint = restEndpoint;
|
||||
FetchDatabase().Wait();
|
||||
}
|
||||
|
||||
public bool IsDirty() => _isDirty;
|
||||
public bool IsEnabled() => _enabled;
|
||||
public void Enable() => _enabled = true;
|
||||
public void Disable() => _enabled = false;
|
||||
public void SetEnabled(bool enabled) => _enabled = enabled;
|
||||
|
||||
public void UpdateBaseUrl(string baseUrl)
|
||||
{
|
||||
_client.Dispose();
|
||||
_client = new RestClient(new RestClientOptions(baseUrl));
|
||||
}
|
||||
|
||||
public async Task<bool> PingApi()
|
||||
{
|
||||
try
|
||||
{
|
||||
var res = await _client.ExecuteHeadAsync(new RestRequest("/ping"));
|
||||
if (res.StatusCode == HttpStatusCode.NotFound)
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task ReadDatabase()
|
||||
{
|
||||
if (!_enabled) return;
|
||||
var lines = File.ReadAllLines(_dbPath);
|
||||
foreach (var entry in lines)
|
||||
{
|
||||
|
|
@ -76,6 +111,7 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
|
|||
|
||||
public async Task FetchDatabase()
|
||||
{
|
||||
if (!_enabled) return;
|
||||
var entries = await _client.GetAsync<List<T>>(_restEndpoint);
|
||||
if (entries == null) return;
|
||||
foreach (var entry in entries)
|
||||
|
|
@ -98,6 +134,7 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
|
|||
|
||||
public async void HandleDatabaseChange(string dbName, string entryObject)
|
||||
{
|
||||
if (!_enabled) return;
|
||||
if (dbName != _dbName)
|
||||
return;
|
||||
|
||||
|
|
@ -133,6 +170,7 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
|
|||
|
||||
public void Sync()
|
||||
{
|
||||
if (!_enabled) return;
|
||||
if (!_isDirty)
|
||||
return;
|
||||
_syncing = true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue