using System; using System.IO; using System.Net; namespace NonaBGS.EDDB { public class API { private static readonly string EDDB_SYSTEMS_ARCHIVE = "https://eddb.io/archive/v6/systems_populated.json"; private string cache_folder = null; private readonly WebClient client = new WebClient(); private string systems_file = null; public string SystemsFile => systems_file; public string Cache { get => cache_folder; set => cache_folder = value; } public API(string cache_folder) { Initialise(cache_folder); } private void Initialise(string cache_folder) { this.cache_folder = cache_folder; systems_file = Path.Combine(this.cache_folder, "systems_populated.json"); client.DownloadDataCompleted += Client_DownloadDataCompleted; } private void DownloadFile(string url, string file) { client.DownloadFileAsync(new Uri(url), file); } public void Download(bool force) { if (!HaveSystemsFile || force) { DownloadFile(EDDB_SYSTEMS_ARCHIVE, systems_file); } } public void Download() { Download(false); } public bool HaveSystemsFile { get { return systems_file != null && File.Exists(systems_file); } } public PopulatedSystems MakePopulatedSystems() { if (!HaveSystemsFile) { throw new InvalidOperationException("no local systems file downloaded"); } return PopulatedSystems.FromFile(SystemsFile); } private void Client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) { } } }